Class: EffectiveLogging::ActiveRecordLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/effective_logging/active_record_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, options = {}) ⇒ ActiveRecordLogger

Returns a new instance of ActiveRecordLogger.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
# File 'lib/effective_logging/active_record_logger.rb', line 5

def initialize(resource, options = {})
  raise ArgumentError.new('options must be a Hash') unless options.kind_of?(Hash)
  raise ArgumentError.new('logger must respond to logged_changes') unless (options[:logger] || resource).respond_to?(:logged_changes)

  @resource = resource
  @logger = options.delete(:logger) || resource
  @depth = options.delete(:depth) || 0
  @options = options
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

#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

#options ⇒ Object

Returns the value of attribute options.



3
4
5
# File 'lib/effective_logging/active_record_logger.rb', line 3

def options
  @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

#attributes ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/effective_logging/active_record_logger.rb', line 61

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|
    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] = {}

    resource.send(association.name).each_with_index do |child, index|
      attributes[association.name][index+1] = ActiveRecordLogger.new(child, options.merge(logger: logger)).attributes
    end
  end

  attributes
end

#changed! ⇒ Object

before_save



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/effective_logging/active_record_logger.rb', line 42

def changed!
  applicable(changes).each do |attribute, (before, after)|
    if after.present?
      log("#{attribute.titleize} changed from '#{before}' to '#{after}'", { attribute: attribute, before: before, after: after })
    else
      log("#{attribute.titleize} set to '#{before}'", { 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

    resource.send(association.name).each_with_index do |child, index|
      ActiveRecordLogger.new(child, options.merge(logger: logger, depth: (depth + 1), prefix: "#{child_name} ##{index+1}: ")).execute!
    end
  end
end

#changes ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/effective_logging/active_record_logger.rb', line 86

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



32
33
34
# File 'lib/effective_logging/active_record_logger.rb', line 32

def created!
  log('Created', applicable(attributes))
end

#destroyed! ⇒ Object

before_destroy



27
28
29
# File 'lib/effective_logging/active_record_logger.rb', line 27

def destroyed!
  log('Deleted', applicable(attributes))
end

#execute! ⇒ Object

execute! is called when we recurse, otherwise the following methods are best called individually



16
17
18
19
20
21
22
23
24
# File 'lib/effective_logging/active_record_logger.rb', line 16

def execute!
  if resource.new_record?
    created!
  elsif resource.marked_for_destruction?
    destroyed!
  else
    changed!
  end
end

#updated! ⇒ Object

after_commit



37
38
39
# File 'lib/effective_logging/active_record_logger.rb', line 37

def updated!
  log('Updated', applicable(attributes))
end