Skip to content

Commit

Permalink
allow exclusion of fields on a model base
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Tello committed Aug 29, 2017
1 parent c8cd83a commit 27cfe2e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/logga/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ module ActiveRecord
EXCLUDED_KEYS_SUFFIXES = [:_id, :_filenames]

included do
class_attribute :log_fields, instance_writer: false
self.log_fields = {}
class_attribute :log_fields, instance_writer: false
class_attribute :excluded_fields, instance_writer: false
self.log_fields = {}
self.excluded_fields = {}
end

class_methods do
def add_log_entries_for(*actions, to: :self, fields: {})
def add_log_entries_for(*actions, to: :self, fields: {}, exclude_fields: []
after_create :log_model_creation if actions.include?(:create)
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.log_fields = fields
self.excluded_fields = Array(exclude_fields)
end
end

Expand All @@ -34,7 +37,8 @@ def log_model_deletion

def log_model_changes
field_changes = changes.reject do |k, _|
EXCLUDED_KEYS.include?(k.to_sym) ||
excluded_fields.include?(k.to_sym) ||
EXCLUDED_KEYS.include?(k.to_sym) ||
EXCLUDED_KEYS_SUFFIXES.any? { |suffix| k.to_s.end_with?(suffix.to_s) }
end
log_field_changes(field_changes)
Expand Down

0 comments on commit 27cfe2e

Please sign in to comment.