Module: NoBrainer::Document::Dirty::ClassMethods

Defined in:
lib/no_brainer/document/dirty.rb

Instance Method Summary collapse

Instance Method Details

#field(attr, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/no_brainer/document/dirty.rb', line 83

def field(attr, options={})
  super
  attr = attr.to_s

  inject_in_layer :dirty_tracking do
    define_method("#{attr}_change") do
      if @_old_attributes.key?(attr)
        result = [@_old_attributes[attr], _read_attribute(attr)]
        result if result.first != result.last || !@_old_attributes_keys.include?(attr)
      end
    end

    define_method("#{attr}_changed?") do
      !!__send__("#{attr}_change")
    end

    define_method("#{attr}_was") do
      @_old_attributes.key?(attr) ? @_old_attributes[attr] : _read_attribute(attr)
    end
  end
end

#remove_field(attr, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/no_brainer/document/dirty.rb', line 105

def remove_field(attr, options={})
  super
  inject_in_layer :dirty_tracking do
    remove_method("#{attr}_change")
    remove_method("#{attr}_changed?")
    remove_method("#{attr}_was")
  end
end