Class: EffectiveLogging::ActiveRecordLogger
- Inherits:
-
Object
- Object
- EffectiveLogging::ActiveRecordLogger
- Defined in:
- lib/effective_logging/active_record_logger.rb
Constant Summary collapse
- BLANK =
"''"- BLACKLIST =
Don't log changes or attributes
[:updated_at, :created_at, :encrypted_password, :status_steps]
Instance Attribute Summary collapse
-
#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
Effective::Log.where(message: 'Deleted').where('details ILIKE ?', '%lab_test_id: 263%').
-
#initialize(object, to: nil, prefix: nil, only: nil, except: nil) ⇒ ActiveRecordLogger
constructor
A new instance of ActiveRecordLogger.
- #log(message, details) ⇒ Object
- #updated! ⇒ Object
Constructor Details
#initialize(object, to: nil, prefix: nil, only: nil, except: nil) ⇒ ActiveRecordLogger
Returns a new instance of ActiveRecordLogger.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/effective_logging/active_record_logger.rb', line 8 def initialize(object, to: nil, prefix: nil, only: nil, except: nil) @object = object @resource = Effective::Resource.new(object) # Validate changes_to value if to.present? && !@resource.belong_tos.map(&:name).include?(to) raise ArgumentError.new("unable to find existing belongs_to relationship matching #{to}. Expected a symbol matching a belongs_to.") end @options = { to: to, prefix: prefix, only: only, except: Array(except) + BLACKLIST }.compact end |
Instance Attribute Details
#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
25 26 27 |
# File 'lib/effective_logging/active_record_logger.rb', line 25 def created! log('Created', resource_attributes) end |
#destroyed! ⇒ Object
Effective::Log.where(message: 'Deleted').where('details ILIKE ?', '%lab_test_id: 263%')
21 22 23 |
# File 'lib/effective_logging/active_record_logger.rb', line 21 def destroyed! log('Deleted', resource_attributes) end |
#log(message, details) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/effective_logging/active_record_logger.rb', line 41 def log(, details) Effective::Log.create!( changes_to: log_changes_to, associated: object, associated_to_s: (object.to_s rescue nil), user: EffectiveLogging.current_user, status: EffectiveLogging.log_changes_status, message: [[:prefix].presence, ].compact.join, details: (details.presence || {}) ) end |
#updated! ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/effective_logging/active_record_logger.rb', line 29 def updated! changes = resource_changes return true if changes.blank? # If you just click save and change nothing, don't log it. = (['Updated'] + changes.map do |attribute, (before, after)| "#{attribute}: #{before.presence || BLANK} → #{after.presence || BLANK}" end).join("\n") log(, resource_attributes.merge(changes: changes)) end |