Module: Mongoid::Dirty::ClassMethods

Defined in:
lib/mongoid/dirty.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_dirty_methods(name) ⇒ Object

Add the dynamic dirty methods. These are custom methods defined on a field by field basis that wrap the dirty attribute methods.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.title_change # [ "Sir", "Madam" ]
person.title_changed? # true
person.title_was # "Sir"
person.reset_title!


245
246
247
248
249
250
# File 'lib/mongoid/dirty.rb', line 245

def add_dirty_methods(name)
  define_method("#{name}_change") { attribute_change(name) } unless instance_methods.include?("#{name}_change") || instance_methods.include?(:"#{name}_change")
  define_method("#{name}_changed?") { attribute_changed?(name) } unless instance_methods.include?("#{name}_changed?") || instance_methods.include?(:"#{name}_changed?")
  define_method("#{name}_was") { attribute_was(name) } unless instance_methods.include?("#{name}_was") || instance_methods.include?(:"#{name}_was")
  define_method("reset_#{name}!") { reset_attribute!(name) } unless instance_methods.include?("reset_#{name}!") || instance_methods.include?(:"reset_#{name}!")
end