Module: DirtyAssociations::ClassMethods

Defined in:
lib/dirty_associations.rb

Instance Method Summary collapse

Instance Method Details

#monitor_association_changes(association) ⇒ Object

Creates methods that allows an association to be monitored.

The association parameter should be a string or symbol representing the name of an association.



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
# File 'lib/dirty_associations.rb', line 15

def monitor_association_changes(association)
  define_method "#{association}=" do |value|
    attribute_will_change!(association.to_s) if _association_will_change?(association, value)
    super(value)
  end

  ids = "#{association.to_s.singularize}_ids"

  define_method "#{ids}=" do |value|
    attribute_will_change!(association.to_s) if _ids_will_change?(ids, value)
    super(value)
  end

  [association, ids].each do |name|
    define_method "#{name}_change" do
      changes[name]
    end

    define_method "#{name}_changed?" do
      changes.has_key?(association.to_s)
    end

    define_method "#{name}_previously_changed?" do
      previous_changes.has_key?(association.to_s)
    end
  end
end