Class: EffectiveTrash::ActiveRecordLogger
- Inherits:
-
Object
- Object
- EffectiveTrash::ActiveRecordLogger
- Defined in:
- lib/effective_trash/active_record_logger.rb
Constant Summary collapse
- BLANK =
"''"
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
Returns the value of attribute options.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #attributes ⇒ Object
-
#changed! ⇒ Object
before_save.
- #changes ⇒ Object
-
#created! ⇒ Object
after_commit.
-
#destroyed! ⇒ Object
before_destroy.
-
#execute! ⇒ Object
execute! is called when we recurse, otherwise the following methods are best called individually.
-
#initialize(resource, options = {}) ⇒ ActiveRecordLogger
constructor
A new instance of ActiveRecordLogger.
-
#trashed! ⇒ Object
before_destroy.
-
#updated! ⇒ Object
after_commit.
Constructor Details
#initialize(resource, options = {}) ⇒ ActiveRecordLogger
Returns a new instance of ActiveRecordLogger.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/effective_trash/active_record_logger.rb', line 7 def initialize(resource, = {}) raise ArgumentError.new('options must be a Hash') unless .kind_of?(Hash) @resource = resource @logger = .delete(:logger) || resource @depth = .delete(:depth) || 0 @options = unless @logger.respond_to?(:logged_changes) || @logger.respond_to?(:trash) raise ArgumentError.new('logger must respond to logged_changes or trash') end end |
Instance Attribute Details
#depth ⇒ Object
Returns the value of attribute depth.
3 4 5 |
# File 'lib/effective_trash/active_record_logger.rb', line 3 def depth @depth end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/effective_trash/active_record_logger.rb', line 3 def logger @logger end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/effective_trash/active_record_logger.rb', line 3 def @options end |
#resource ⇒ Object
Returns the value of attribute resource.
3 4 5 |
# File 'lib/effective_trash/active_record_logger.rb', line 3 def resource @resource end |
Instance Method Details
#attributes ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/effective_trash/active_record_logger.rb', line 80 def attributes attributes = { attributes: resource.attributes } # Collect to_s representations of all belongs_to associations (resource.class.try(:reflect_on_all_associations, :belongs_to) || []).each do |association| attributes[association.name] = resource.send(association.name).to_s.presence || 'nil' end # Collect to_s representations for all has_one associations (resource.class.try(:reflect_on_all_associations, :has_one) || []).each do |association| next if association.name == :trash && resource.respond_to?(:acts_as_trashable_options) # We skip our own association attributes[association.name] = resource.send(association.name).to_s.presence || 'nil' end # Collects attributes for all accepts_as_nested_parameters has_many associations (resource.class.try(:reflect_on_all_autosave_associations) || []).each do |association| attributes[association.name] = {} Array(resource.send(association.name)).each_with_index do |child, index| attributes[association.name][index+1] = ActiveRecordLogger.new(child, .merge(logger: logger)).attributes end end attributes end |
#changed! ⇒ Object
before_save
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/effective_trash/active_record_logger.rb', line 52 def changed! applicable(changes).each do |attribute, (before, after)| if resource.respond_to?(:log_changes_formatted_value) before = resource.log_changes_formatted_value(attribute, before) || before after = resource.log_changes_formatted_value(attribute, after) || after end attribute = if resource.respond_to?(:log_changes_formatted_attribute) resource.log_changes_formatted_attribute(attribute) end || attribute.titleize if after.present? log("#{attribute} changed from #{before.presence || BLANK} to #{after.presence || BLANK}", details: { attribute: attribute, before: before, after: after }) else log("#{attribute} set to #{before || BLANK}", details: { attribute: attribute, value: before }) end end # Log changes on all accepts_as_nested_parameters has_many associations (resource.class.try(:reflect_on_all_autosave_associations) || []).each do |association| child_name = association.name.to_s.singularize.titleize Array(resource.send(association.name)).each_with_index do |child, index| ActiveRecordLogger.new(child, .merge(logger: logger, depth: (depth + 1), prefix: "#{child_name} ##{index+1}: ")).execute! end end end |
#changes ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/effective_trash/active_record_logger.rb', line 106 def changes changes = resource.changes # Log to_s changes on all belongs_to associations (resource.class.try(:reflect_on_all_associations, :belongs_to) || []).each do |association| if (change = changes.delete(association.foreign_key)).present? changes[association.name] = [association.klass.find_by_id(change.first), resource.send(association.name)] end end changes end |
#created! ⇒ Object
after_commit
42 43 44 |
# File 'lib/effective_trash/active_record_logger.rb', line 42 def created! log('Created', details: applicable(attributes)) end |
#destroyed! ⇒ Object
before_destroy
37 38 39 |
# File 'lib/effective_trash/active_record_logger.rb', line 37 def destroyed! log('Deleted', details: applicable(attributes)) end |
#execute! ⇒ Object
execute! is called when we recurse, otherwise the following methods are best called individually
21 22 23 24 25 26 27 28 29 |
# File 'lib/effective_trash/active_record_logger.rb', line 21 def execute! if resource.new_record? created! elsif resource.marked_for_destruction? destroyed! else changed! end end |
#trashed! ⇒ Object
before_destroy
32 33 34 |
# File 'lib/effective_trash/active_record_logger.rb', line 32 def trashed! log_trash((resource.to_s rescue ''), details: applicable(attributes)) end |
#updated! ⇒ Object
after_commit
47 48 49 |
# File 'lib/effective_trash/active_record_logger.rb', line 47 def updated! log('Updated', details: applicable(attributes)) end |