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(object, options = {}) ⇒ ActiveRecordLogger

Returns a new instance of ActiveRecordLogger.

Raises:

  • (ArgumentError)


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

def initialize(object, options = {})
  raise ArgumentError.new('options must be a Hash') unless options.kind_of?(Hash)

  @object = object
  @resource = Effective::Resource.new(object)
  @logged = false # If work was done

  @logger = options.delete(:logger) || object
  @depth = options.delete(:depth) || 0
  @include_associated = options.fetch(:include_associated, true)
  @options = options

  raise ArgumentError.new('logger must respond to logged_changes') unless @logger.respond_to?(:logged_changes)
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

#include_associatedObject

Returns the value of attribute include_associated.



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

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

#objectObject

Returns the value of attribute object.



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

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

#created!Object

after_commit



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

def created!
  log('Created', details: applicable(resource.instance_attributes(include_associated: include_associated)))
end

#destroyed!Object

before_destroy



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

def destroyed!
  log('Deleted')
end

#execute!Object

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



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/effective_logging/active_record_logger.rb', line 23

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

  log_nested_resources! if include_associated

  @logged
end

#updated!Object

after_commit



48
49
50
# File 'lib/effective_logging/active_record_logger.rb', line 48

def updated!
  log('Updated', details: applicable(resource.instance_attributes(include_associated: include_associated)))
end