Class: ModelAuditor::Changes

Inherits:
Object
  • Object
show all
Defined in:
lib/model_auditor/changes.rb

Constant Summary collapse

TRUE_ARRAY =
[true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
IGNORED_ATTRIBUTES =
[:created_at, :updated_at].freeze

Instance Method Summary collapse

Constructor Details

#initialize(model, changes = nil, options = {}) ⇒ Changes

Returns a new instance of Changes.



8
9
10
11
12
# File 'lib/model_auditor/changes.rb', line 8

def initialize(model, changes = nil, options = {})
  @model = model
  @changes = changes || @model.previous_changes
  @options = options
end

Instance Method Details

#auditObject



24
25
26
27
28
29
30
31
32
# File 'lib/model_auditor/changes.rb', line 24

def audit
  return unless changed?

  changes = @changes.keys.inject({}) do |hash, key|
    hash.merge!(human_name(key) => human_value(key))
  end

  changes.presence
end

#filter(filtered = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/model_auditor/changes.rb', line 14

def filter(filtered = nil)
  filtered ||= IGNORED_ATTRIBUTES

  @changes = @changes.reject do |key, _value|
    Array(filtered).include?(key.to_sym)
  end

  self
end