Class: EffectiveLogging::ActiveRecordLogger

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

Constant Summary collapse

BLANK =
"''"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ActiveRecordLogger.

Raises:

  • (ArgumentError)


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

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

#depthObject

Returns the value of attribute depth.



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

def depth
  @depth
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#resourceObject

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

#attributesObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/effective_logging/active_record_logger.rb', line 77

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|
    next if association.name == :trash && resource.respond_to?(:acts_as_trashable_options) # We skip our own 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] = {}

    Array(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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/effective_logging/active_record_logger.rb', line 49

def changed!
  applicable(changes).each do |attribute, (before, after)|
    if resource.respond_to?(:log_changes_formatted_value)
      before = resource.log_changes_formatted_value(attribute, before) || before
      after = resource.log_changes_formatted_value(attribute, after) || after
    end

    attribute = if resource.respond_to?(:log_changes_formatted_attribute)
      resource.log_changes_formatted_attribute(attribute)
    end || attribute.titleize

    if after.present?
      log("#{attribute} changed from #{before.presence || BLANK} to #{after.presence || BLANK}", details: { attribute: attribute, before: before, after: after })
    else
      log("#{attribute} set to #{before || BLANK}", details: { 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

    Array(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

#changesObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/effective_logging/active_record_logger.rb', line 103

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



39
40
41
# File 'lib/effective_logging/active_record_logger.rb', line 39

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

#destroyed!Object

before_destroy



34
35
36
# File 'lib/effective_logging/active_record_logger.rb', line 34

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

#execute!Object

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



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

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

#trashed!Object

before_destroy



29
30
31
# File 'lib/effective_logging/active_record_logger.rb', line 29

def trashed!
  log((resource.to_s rescue ''), status: EffectiveLogging.trashable_status, details: applicable(attributes))
end

#updated!Object

after_commit



44
45
46
# File 'lib/effective_logging/active_record_logger.rb', line 44

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