Module: Mongoid::Dirty::InstanceMethods

Defined in:
lib/mongoid/dirty.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#attribute_change(name) ⇒ Object

Gets the changes for a specific field.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.attribute_change("title") # [ "Sir", "Madam" ]

Returns:

An Array containing the old and new values.



17
18
19
# File 'lib/mongoid/dirty.rb', line 17

def attribute_change(name)
  modifications[name]
end

#attribute_changed?(name) ⇒ Boolean

Determines if a specific field has chaged.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.attribute_changed?("title") # true

Returns:

true if changed, false if not.

Returns:



32
33
34
# File 'lib/mongoid/dirty.rb', line 32

def attribute_changed?(name)
  modifications.include?(name)
end

#attribute_was(name) ⇒ Object

Gets the old value for a specific field.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.attribute_was("title") # "Sir"

Returns:

The old field value.



47
48
49
50
# File 'lib/mongoid/dirty.rb', line 47

def attribute_was(name)
  change = modifications[name]
  change ? change[0] : nil
end

#changedObject

Gets the names of all the fields that have changed in the document.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.changed # returns [ "title" ]

Returns:

An Array of changed field names.



63
64
65
# File 'lib/mongoid/dirty.rb', line 63

def changed
  modifications.keys
end

#changed?Boolean

Alerts to whether the document has been modified or not.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.changed? # returns true

Returns:

true if changed, false if not.

Returns:



78
79
80
# File 'lib/mongoid/dirty.rb', line 78

def changed?
  !modifications.empty?
end

#changesObject

Gets all the modifications that have happened to the object as a Hash with the keys being the names of the fields, and the values being an Array with the old value and new value.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.changes # returns { "title" => [ "Sir", "Madam" ] }

Returns:

A Hash of changes.



95
96
97
# File 'lib/mongoid/dirty.rb', line 95

def changes
  modifications
end

#move_changesObject

Call this method after save, so the changes can be properly switched.

Example:

person.move_changes



104
105
106
107
# File 'lib/mongoid/dirty.rb', line 104

def move_changes
  @previous_modifications = modifications.dup
  @modifications = {}
end

#previous_changesObject

Gets all the modifications that have happened to the object before the object was saved.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.save!
person.previous_changes # returns { "title" => [ "Sir", "Madam" ] }

Returns:

A Hash of changes before save.



141
142
143
# File 'lib/mongoid/dirty.rb', line 141

def previous_changes
  @previous_modifications
end

#reset_attribute!(name) ⇒ Object

Resets a changed field back to its old value.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.reset_attribute!("title")
person.title # "Sir"

Returns:

The old field value.



157
158
159
160
161
162
163
# File 'lib/mongoid/dirty.rb', line 157

def reset_attribute!(name)
  value = attribute_was(name)
  if value
    @attributes[name] = value
    modifications.delete(name)
  end
end

#reset_modificationsObject

Reset all modifications for the document. This will wipe all the marked changes, but not reset the values.

Example:

document.reset_modifications



183
184
185
186
# File 'lib/mongoid/dirty.rb', line 183

def reset_modifications
  @accessed = {}
  @modifications = {}
end

#settersObject

Gets all the new values for each of the changed fields, to be passed to a MongoDB $set modifier.

Example:

person = Person.new(:title => "Sir")
person.title = "Madam"
person.setters # returns { "title" => "Madam" }

Returns:

A Hash of new values.



121
122
123
124
125
126
# File 'lib/mongoid/dirty.rb', line 121

def setters
  modifications.inject({}) do |sets, (field, changes)|
    key = embedded? ? "#{_position}.#{field}" : field
    sets[key] = changes[1]; sets
  end
end

#setup_modificationsObject

Sets up the modifications hash. This occurs just after the document is instantiated.

Example:

document.setup_notifications



171
172
173
174
175
# File 'lib/mongoid/dirty.rb', line 171

def setup_modifications
  @accessed ||= {}
  @modifications ||= {}
  @previous_modifications ||= {}
end