Module: ActiveRecord::Acts::AuditedCollection::ClassMethods

Defined in:
lib/active_record/acts/audited_collection.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_audited_collection(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_record/acts/audited_collection.rb', line 5

def acts_as_audited_collection(options = {})
  unless self.included_modules.include?(InstanceMethods)
    # First time use in this class, we have some extra work to do.
    send :include, InstanceMethods 

    class_attribute :audited_collections
    send :audited_collections=, {}
    attr_accessor :collection_audit_object_is_soft_deleted

    after_create :collection_audit_create
    before_update :collection_audit_update
    before_destroy :collection_audit_destroy

    has_many :child_collection_audits, :as => :child_record,
      :class_name => 'CollectionAudit'
  end

  options = {
    :name => self.name.tableize.to_sym,
    :cascade => false,
    :track_modifications => false,
    :only => nil,
    :except => nil,
    :soft_delete => nil
  }.merge(options)

  options[:only] &&= [options[:only]].flatten.collect(&:to_s)
  options[:except] &&= [options[:except]].flatten.collect(&:to_s)

  unless options.has_key? :parent
    raise ActiveRecord::ConfigurationError.new "Must specify parent for an acts_as_audited_collection (:parent => :object)"
  end

  parent_association = reflect_on_association(options[:parent])
  unless parent_association && parent_association.belongs_to?
    raise ActiveRecord::ConfigurationError.new "Parent association '#{options[:parent]}' must be a belongs_to relationship"
  end

  # Try explicit first, then default
  options[:foreign_key] ||= parent_association.options[:foreign_key]
  options[:foreign_key] ||= parent_association.foreign_key

  # TODO Remove this when polymorphic is supported.
  if parent_association.options[:polymorphic]
    raise ActiveRecord::ConfigurationError.new "Sorry, acts_as_audited_collection polymorphic associations haven't been added yet."
  end

  options[:parent_type] ||= parent_association.klass.name

  define_acts_as_audited_collection options do |config|
    config.merge! options
  end
end

#acts_as_audited_collection_parent(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_record/acts/audited_collection.rb', line 59

def acts_as_audited_collection_parent(options = {})
  unless options.has_key? :for
    raise ActiveRecord::ConfigurationError.new "Must specify relationship for an acts_as_audited_collection_parent (:for => :objects)"
  end

  child_association = reflect_on_association(options[:for])
  if child_association.nil? || child_association.belongs_to?
    raise ActiveRecord::ConfigurationError.new "Association '#{options[:for]}' must be a valid parent (i.e. not belongs_to) relationship"
  end

  has_many :"#{options[:for]}_audits", :as => :parent_record,
      :class_name => 'CollectionAudit',
      :conditions => ['audited_association = ?', options[:for].to_s]
end

#define_acts_as_audited_collection(options) {|audited_collections[key] ||= {}| ... } ⇒ Object

Yields:

  • (audited_collections[key] ||= {})


74
75
76
77
# File 'lib/active_record/acts/audited_collection.rb', line 74

def define_acts_as_audited_collection(options)
  key = "#{options[:parent_type]}##{options[:name]}"
  yield(audited_collections[key] ||= {})
end

#without_collection_auditObject



79
80
81
82
83
84
85
86
87
# File 'lib/active_record/acts/audited_collection.rb', line 79

def without_collection_audit
  result = nil
  Thread.current[:collection_audit_enabled] = Thread.current[:collection_audit_enabled].tap do
    Thread.current[:collection_audit_enabled] = false
    result = yield if block_given?
  end

  result
end