File tree Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -108,5 +108,22 @@ def attribute_changed?(attr)
108
108
return false unless changed_attributes . include? ( attr )
109
109
changed_attributes [ attr ] != attributes [ attr ]
110
110
end
111
+
112
+ # Override Active Model's behaviour here in order to stay away from
113
+ # infinite loops on getter/setter overrides.
114
+ #
115
+ # @example Flag an attribute as changing.
116
+ # document.attribute_will_change!(:name)
117
+ #
118
+ # @param [ Symbol ] attr The attribute.
119
+ #
120
+ # @return [ Object ] The value of the attribute.
121
+ #
122
+ # @since 2.3.0
123
+ def attribute_will_change! ( attr )
124
+ value = read_attribute ( attr )
125
+ value = value . duplicable? ? value . clone : value
126
+ changed_attributes [ attr ] = value unless changed_attributes . include? ( attr )
127
+ end
111
128
end
112
129
end
Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ class Account
4
4
field :balance , :type => String
5
5
field :nickname , :type => String
6
6
field :name , :type => String
7
+
8
+ field :overridden , :type => String
7
9
key :name
8
10
9
11
embeds_many :memberships
@@ -17,4 +19,8 @@ class Account
17
19
18
20
validates_presence_of :name
19
21
validates_length_of :name , :maximum => 10 , :on => :create
22
+
23
+ def overridden
24
+ self [ :overridden ] = "not recommended"
25
+ end
20
26
end
Original file line number Diff line number Diff line change 2
2
3
3
describe Mongoid ::Attributes do
4
4
5
+ describe "\# {attribute}" do
6
+
7
+ context "when setting the value in the getter" do
8
+
9
+ let ( :account ) do
10
+ Account . new
11
+ end
12
+
13
+ it "does not cause an infinite loop" do
14
+ account . overridden . should eq ( "not recommended" )
15
+ end
16
+ end
17
+ end
18
+
5
19
describe "#[]" do
6
20
7
21
context "when the document is a new record" do
You can’t perform that action at this time.
0 commit comments