Skip to content

Commit

Permalink
Now only attempting to log messages if the log_reciever responds to l…
Browse files Browse the repository at this point in the history
…og_entries
  • Loading branch information
schinery committed Mar 27, 2019
1 parent 7c48008 commit a35312a
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions lib/logga/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,80 +20,97 @@ def add_log_entries_for(*actions, to: :self, fields: {}, exclude_fields: [])
after_destroy :log_model_deletion if actions.include?(:delete)
after_update :log_model_changes if actions.include?(:update)
define_method(:log_receiver) { to == :self ? self : send(to) }

self.log_fields = fields
self.excluded_fields = Array(exclude_fields)
end
end

def log_model_creation
return if should_not_log?

body_generator = ->(record) { default_creation_log_body(record) }
body = log_fields.fetch(:created_at, body_generator).call(self)
log_receiver&.log_entries&.create(author_data.merge(body: body, created_at: creation_at))
create_log_entry(author_data.merge(body: body, created_at: creation_at))
end

def log_model_deletion
body_generator = ->(record) {default_deletion_log_body(record)}
return if should_not_log?

body_generator = ->(record) { default_deletion_log_body(record) }
body = log_fields.fetch(:deleted_at, body_generator).call(self)
log_receiver&.log_entries&.create(author_data.merge(body: body))
create_log_entry(author_data.merge(body: body))
end

def log_model_changes
field_changes = previous_changes.reject do |k, _|
EXCLUDED_KEYS.include?(k.to_sym) ||
(!log_fields.include?(k.to_sym) &&
(excluded_fields.include?(k.to_sym) ||
EXCLUDED_KEYS_SUFFIXES.any? { |suffix| k.to_s.end_with?(suffix.to_s) }))
end
log_field_changes(field_changes)
end
return if should_not_log?

field_changes = previous_changes.reject { |k, _| reject_change?(k) }
return if field_changes.blank?

def log_field_changes(changes)
return if changes.blank?
body = field_changes_to_message(changes)
log_receiver&.log_entries&.create(author_data.merge(body: body)) if body.present?
body = field_changes_to_message(field_changes)
create_log_entry(author_data.merge(body: body)) if body.present?
end

private

def author_data
data = Hash(log_receiver.try(:author) || try(:author)).with_indifferent_access
{
author_id: data[:id],
author_type: data[:type],
author_name: data[:name]
author_id: data[:id],
author_type: data[:type],
author_name: data[:name]
}
end

def create_log_entry(entry)
log_receiver&.log_entries&.create(entry)
end

def creation_at
return Time.current unless log_receiver == self

(log_receiver&.log_entries&.order(:created_at)&.first&.created_at || Time.current) - 0.1.seconds
end

def default_creation_log_body(record)
[
"#{titleized_model_class_name(record)} created",
("(#{record.state})" if record.try(:state))
].compact.join(' ')
].compact.join(" ")
end

def default_change_log_body(record, field, old_value, new_value)
def default_change_log_body(record, field, _old_value, new_value)
"#{titleized_model_class_name(record)} #{field.humanize(capitalize: false)} set to #{new_value}"
end

def default_deletion_log_body(record)
[
"#{titleized_model_class_name(record)} removed",
("(#{record.name})" if record.try(:name))
].compact.join(' ')
].compact.join(" ")
end

def field_changes_to_message(changes)
body_generator = ->(record, field, old_value, new_value) { default_change_log_body(record, field, old_value, new_value) }
body_generator = lambda { |record, field, old_value, new_value|
default_change_log_body(record, field, old_value, new_value)
}
changes.inject([]) do |result, (field, (old_value, new_value))|
result << log_fields.fetch(field.to_sym, body_generator).call(self, field, old_value, new_value)
end.compact.join("\n")
end

def reject_change?(key)
EXCLUDED_KEYS.include?(key.to_sym) ||
(!log_fields.include?(key.to_sym) &&
(excluded_fields.include?(key.to_sym) ||
EXCLUDED_KEYS_SUFFIXES.any? { |suffix| key.to_s.end_with?(suffix.to_s) }))
end

def should_not_log?
!log_receiver.respond_to?(:log_entries)
end

def titleized_model_class_name(record)
record.class.name.demodulize.titleize
end
Expand Down

0 comments on commit a35312a

Please sign in to comment.