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, args = {}) ⇒ ActiveRecordLogger
constructor
to, prefix, only, except.
- #log(message, details) ⇒ Object
- #updated! ⇒ Object
Constructor Details
#initialize(object, args = {}) ⇒ ActiveRecordLogger
to, prefix, only, except
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/effective_logging/active_record_logger.rb', line 9 def initialize(object, args = {}) @object = object @resource = Effective::Resource.new(object) # Validate changes_to value if args[:to].present? && !@resource.belong_tos.map(&:name).include?(args[:to]) raise ArgumentError.new("unable to find existing belongs_to relationship matching #{args[:to]}. Expected a symbol matching a belongs_to.") end @options = { to: args[:to], prefix: args[:prefix], only: args[:only], except: Array(args[: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
26 27 28 |
# File 'lib/effective_logging/active_record_logger.rb', line 26 def created! log('Created', resource_attributes) end |
#destroyed! ⇒ Object
Effective::Log.where(message: ‘Deleted’).where(‘details ILIKE ?’, ‘%lab_test_id: 263%’)
22 23 24 |
# File 'lib/effective_logging/active_record_logger.rb', line 22 def destroyed! log('Deleted', resource_attributes) end |
#log(message, details) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/effective_logging/active_record_logger.rb', line 42 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
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/effective_logging/active_record_logger.rb', line 30 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 |