@@ -17,6 +17,74 @@ The complete list of releases is available `on GitHub
17
17
please consult GitHub releases for detailed release notes and JIRA for
18
18
the complete list of issues fixed in each release, including bug fixes.
19
19
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
+
20
88
21
89
Configuration DSL No Longer Requires an Argument to its Block
22
90
-------------------------------------------------------------
0 commit comments