Class: EffectiveLogging::ActiveRecordLogger

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#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


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


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/effective_logging/active_record_logger.rb', line 45

def log(message, 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: [options[:prefix].presence, message].compact.join,
    details: (details.presence || {})
  )
end

#updated!Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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} &rarr; #{after.presence || BLANK}"
  end).join("\n")

  log(message, resource_attributes.merge(changes: changes))
end