Module: AuditAssociations::AssociationsDefinitions

Included in:
ApplicationRecord
Defined in:
app/models/concerns/audit_associations.rb

Instance Method Summary collapse

Instance Method Details

#audit_associations(*associations) ⇒ Object


46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/audit_associations.rb', line 46

def audit_associations(*associations)
  new_associations = normalize_associations(associations)
  if respond_to?(:audited_options)
    configure_dirty_associations(new_associations)
    audited_options[:associations] = audited_options[:associations] | new_associations
  else
    logger.warn "ignoring associations #{new_associations.join(', ')} audit definition for #{self}, the resource is not audited"
  end
end

#audited(options = {}) ⇒ Object


37
38
39
40
41
42
43
44
# File 'app/models/concerns/audit_associations.rb', line 37

def audited(options = {})
  options[:associations] = normalize_associations(options[:associations])
  if options[:associations].present?
    configure_dirty_associations(options[:associations])
  end

  super
end

#configure_dirty_associations(associations) ⇒ Object


62
63
64
65
# File 'app/models/concerns/audit_associations.rb', line 62

def configure_dirty_associations(associations)
  include DirtyAssociations unless included_modules.include?(DirtyAssociations)
  dirty_has_many_associations(*associations)
end

#normalize_associations(associations) ⇒ Object


56
57
58
59
60
# File 'app/models/concerns/audit_associations.rb', line 56

def normalize_associations(associations)
  Array(associations).map do |association|
    "#{association.to_s.singularize}_ids"
  end
end