Class: EffectiveLogging::ActiveRecordLogger
- Inherits:
-
Object
- Object
- EffectiveLogging::ActiveRecordLogger
- Defined in:
- lib/effective_logging/active_record_logger.rb
Constant Summary collapse
- BLANK =
"''"
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#include_associated ⇒ Object
Returns the value of attribute include_associated.
-
#include_nested ⇒ Object
Returns the value of attribute include_nested.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#object ⇒ Object
Returns the value of attribute object.
-
#options ⇒ Object
Returns the value of attribute options.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #created! ⇒ Object
- #destroyed! ⇒ Object
- #execute! ⇒ Object
-
#initialize(object, options = {}) ⇒ ActiveRecordLogger
constructor
A new instance of ActiveRecordLogger.
- #log_nested_resources! ⇒ Object
- #log_resource_changes! ⇒ Object
- #updated! ⇒ Object
Constructor Details
#initialize(object, options = {}) ⇒ ActiveRecordLogger
Returns a new instance of ActiveRecordLogger.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/effective_logging/active_record_logger.rb', line 7 def initialize(object, = {}) raise ArgumentError.new('options must be a Hash') unless .kind_of?(Hash) @object = object @resource = Effective::Resource.new(object) @logger = .delete(:logger) || object @depth = .delete(:depth) || 0 @include_associated = .fetch(:include_associated, true) @include_nested = .fetch(:include_nested, true) @options = raise ArgumentError.new('logger must respond to logged_changes') unless @logger.respond_to?(:logged_changes) end |
Instance Attribute Details
#depth ⇒ Object
Returns the value of attribute depth.
3 4 5 |
# File 'lib/effective_logging/active_record_logger.rb', line 3 def depth @depth end |
#include_associated ⇒ Object
Returns the value of attribute include_associated.
3 4 5 |
# File 'lib/effective_logging/active_record_logger.rb', line 3 def include_associated @include_associated end |
#include_nested ⇒ Object
Returns the value of attribute include_nested.
3 4 5 |
# File 'lib/effective_logging/active_record_logger.rb', line 3 def include_nested @include_nested end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/effective_logging/active_record_logger.rb', line 3 def logger @logger end |
#object ⇒ Object
Returns the value of attribute object.
3 4 5 |
# File 'lib/effective_logging/active_record_logger.rb', line 3 def object @object end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/effective_logging/active_record_logger.rb', line 3 def @options end |
#resource ⇒ Object
Returns the value of attribute resource.
3 4 5 |
# File 'lib/effective_logging/active_record_logger.rb', line 3 def resource @resource end |
Instance Method Details
#created! ⇒ Object
40 41 42 |
# File 'lib/effective_logging/active_record_logger.rb', line 40 def created! log('Created', details: applicable(instance_attributes)) end |
#destroyed! ⇒ Object
36 37 38 |
# File 'lib/effective_logging/active_record_logger.rb', line 36 def destroyed! log('Deleted', details: applicable(instance_attributes)) end |
#execute! ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/effective_logging/active_record_logger.rb', line 22 def execute! @logged = false if new_record?(object) created! elsif destroyed_record?(object) destroyed! else updated! end logged? end |
#log_nested_resources! ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/effective_logging/active_record_logger.rb', line 69 def log_nested_resources! return unless include_nested # Log changes on all accepts_as_nested_parameters has_many associations resource.nested_resources.each do |association| title = association.name.to_s.singularize.titleize Array(object.send(association.name)).each_with_index do |child, index| next unless child.present? = .merge(logger: logger, depth: depth+1, prefix: "#{title} #{index} - #{child} - ", include_associated: include_associated, include_nested: include_nested) = .merge(child.) if child.respond_to?(:log_changes_options) @logged = true if ::EffectiveLogging::ActiveRecordLogger.new(child, ).execute! end end end |
#log_resource_changes! ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/effective_logging/active_record_logger.rb', line 51 def log_resource_changes! applicable(resource.instance_changes).each do |attribute, (before, after)| if object.respond_to?(:log_changes_formatted_value) before = object.log_changes_formatted_value(attribute, before) || before after = object.log_changes_formatted_value(attribute, after) || after end before = before.to_s if before.kind_of?(ActiveRecord::Base) || before.kind_of?(FalseClass) after = after.to_s if after.kind_of?(ActiveRecord::Base) || after.kind_of?(FalseClass) attribute = if object.respond_to?(:log_changes_formatted_attribute) object.log_changes_formatted_attribute(attribute) end || attribute.titleize log("#{attribute}: #{before.presence || BLANK} → #{after.presence || BLANK}", details: { attribute: attribute, before: before, after: after }) end end |
#updated! ⇒ Object
44 45 46 47 48 49 |
# File 'lib/effective_logging/active_record_logger.rb', line 44 def updated! log_resource_changes! log_nested_resources! log('Updated', details: applicable(instance_attributes)) if logged? && depth == 0 end |