class EffectiveLogging::ActiveRecordLogger
Constants
- BLACKLIST
- BLANK
Attributes
object[RW]
options[RW]
resource[RW]
Public Class Methods
new(object, args = {})
click to toggle source
to, prefix, only, except
# 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
Public Instance Methods
created!()
click to toggle source
# File lib/effective_logging/active_record_logger.rb, line 26 def created! log('Created', resource_attributes) end
destroyed!()
click to toggle source
Effective::Log.where(message: ‘Deleted’).where(‘details ILIKE ?’, ‘%lab_test_id: 263%’)
# File lib/effective_logging/active_record_logger.rb, line 22 def destroyed! log('Deleted', resource_attributes) end
log(message, details)
click to toggle source
# File lib/effective_logging/active_record_logger.rb, line 45 def log(message, details) EffectiveLogging.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: [options[:prefix].presence, message].compact.join, details: (details.presence || {}) ) end
updated!()
click to toggle source
# 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. message = (['Updated'] + changes.map do |attribute, (before, after)| before = "HTML content (#{before.length})" if before.kind_of?(String) && before.include?('<div') after = "HTML content (#{after.length})" if after.kind_of?(String) && after.include?('<div') "#{attribute}: #{before.presence || BLANK} → #{after.presence || BLANK}" end).join("\n") log(message, resource_attributes.merge(changes: changes)) end
Private Instance Methods
log_changes_to()
click to toggle source
# File lib/effective_logging/active_record_logger.rb, line 59 def log_changes_to logger = object while(logger.log_changes_options[:to].present?) belongs_to = logger.public_send(logger.log_changes_options[:to]) break unless belongs_to.respond_to?(:log_changes_options) logger = belongs_to end logger end
resource_attributes()
click to toggle source
# File lib/effective_logging/active_record_logger.rb, line 71 def resource_attributes # effective_resources gem resource.instance_attributes(only: options[:only], except: options[:except], associations: false) end
resource_changes()
click to toggle source
# File lib/effective_logging/active_record_logger.rb, line 75 def resource_changes # effective_resources gem resource.instance_changes(only: options[:only], except: options[:except]).inject({}) do |h, (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.to_s.titleize h[attribute] = [before, after]; h end end