Skip to content

Commit 0199d52

Browse files
MONGOID-4528 Add more dirty methods (#5440)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent e02ae2c commit 0199d52

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

source/release-notes/mongoid-8.1.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,74 @@ The complete list of releases is available `on GitHub
1717
please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

20+
Added ``attribute_before_last_save``, ``saved_change_to_attribute``, ``saved_change_to_attribute?``, and ``will_save_change_to_attribute?`` methods
21+
----------------------------------------------------------------------------------------------------------------------------------------------------
22+
23+
These new methods behave identically to corresponding methods
24+
from ``ActiveRecord::AttributeMethods::Dirty``. The methods are particularly useful in
25+
callbacks:
26+
27+
.. code-block:: ruby
28+
29+
class Person
30+
include Mongoid::Document
31+
32+
field :name, type: String
33+
34+
before_save do
35+
puts "attribute_was(:name): #{attribute_was(:name)}"
36+
puts "attribute_before_last_save(:name): #{attribute_before_last_save(:name)}"
37+
puts "will_save_change_to_attribute?(:name): #{will_save_change_to_attribute?(:name)}"
38+
end
39+
40+
after_save do
41+
puts "attribute_was(:name): #{attribute_was(:name)}"
42+
puts "attribute_before_last_save(:name): #{attribute_before_last_save(:name)}"
43+
puts "saved_change_to_attribute(:name): #{saved_change_to_attribute(:name)}"
44+
puts "attribute_changed?(:name): #{attribute_changed?(:name)}"
45+
puts "saved_change_to_attribute?(:name): #{saved_change_to_attribute?(:name)}"
46+
end
47+
end
48+
49+
person = Person.create(name: 'John')
50+
#
51+
# before_save
52+
#
53+
## attribute_was(:name): nil
54+
## attribute_before_last_save(:name): nil
55+
## will_save_change_to_attribute?(:name): true
56+
#
57+
# after_save
58+
#
59+
## attribute_was(:name): John => New value
60+
## attribute_before_last_save(:name): nil => Value before save
61+
## saved_change_to_attribute(:name): [nil, "John"] => Both values
62+
## attribute_changed?(:name): false
63+
## saved_change_to_attribute?(:name): true => Correctly indicates that the change for :name was saved
64+
65+
person.name = 'Jane'
66+
person.save
67+
#
68+
# before_save
69+
#
70+
## attribute_was(:name): John => attribute_was not look back before the last save
71+
## attribute_before_last_save(:name): nil => value before the last save
72+
## will_save_change_to_attribute?(:name): true
73+
#
74+
# after_save
75+
#
76+
## attribute_was(:name): Jane => New value
77+
## attribute_before_last_save(:name): John => Value before save
78+
## saved_change_to_attribute(:name): ["John", "Jane"] => Both values
79+
## attribute_changed?(:name): false
80+
## saved_change_to_attribute?(:name): true => Correctly indicates that the change for :name was saved
81+
82+
For all of the new methods there are also shorter forms created dynamically, e.g.
83+
``attribute_before_last_save(:name)`` is equivalent to ``name_before_last_save``,
84+
``saved_change_to_attribute(:name)`` is equivalent to ``saved_change_to_name``,
85+
``saved_change_to_attribute?(:name)`` is equivalent to ``saved_change_to_name?``,
86+
and ``will_save_change_to_attribute?(:name)`` is equivalent to ``will_save_change_to_name?``.
87+
2088

2189
Configuration DSL No Longer Requires an Argument to its Block
2290
-------------------------------------------------------------

0 commit comments

Comments
 (0)