Module: Auditing::AuditRelationship

Defined in:
lib/auditing/audit_relationship.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#audit_relationship_enabled(opts = {}) ⇒ Object

AuditRelationship creates audits for a has_many relationship.



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

def audit_relationship_enabled(opts={})
  include InstanceMethods

  class_inheritable_accessor :audit_enabled_models
  class_inheritable_accessor :field_names

  self.audit_enabled_models = gather_models(opts)
  self.field_names          = gather_assoc_fields_for_auditing(opts[:fields])

  after_create   :audit_relationship_create
  before_update  :audit_relationship_update
  before_destroy :audit_relationship_destroy
end

#default_columnsObject



58
59
60
# File 'lib/auditing/audit_relationship.rb', line 58

def default_columns
  self.column_names - ["id", "created_at", "updated_at"]
end

#gather_assoc_fields_for_auditing(fields = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/auditing/audit_relationship.rb', line 37

def gather_assoc_fields_for_auditing(fields=nil)
  poly_array = []
  reflect_on_all_associations(:belongs_to).each do |assoc|
    poly_array << assoc.name if assoc.options[:polymorphic]
  end

  unless fields
    if poly_array.nil?
      return default_columns
    else
      tmp_names = default_columns
      poly_array.each do |poly|
        tmp_names = tmp_names.reject {|column| column.match(/#{poly.to_s}*/)}
      end
    end
    return tmp_names
  else
    fields.is_a?(Array) ? fields.map {|f| f.to_s} : [fields.to_s]
  end
end

#gather_models(opts = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/auditing/audit_relationship.rb', line 62

def gather_models(opts={})
  if opts[:only]
    if opts[:only].is_a?(Array)
      opts[:only].map {|c| c.to_s.underscore.to_sym}
    else
      [opts[:only].to_s.underscore.to_sym]
    end
  else
    array,tmp = [], []
    reflect_on_all_associations(:belongs_to).each do |assoc|
      array << assoc.name
    end

    if opts[:except]
      if opts[:except].is_a?(Array)
        tmp = opts[:except].map {|c| c.to_s.underscore.to_sym}
      else
        tmp = [opts[:except].to_s.underscore.to_sym]
      end
    end
    array - tmp
  end
end