Skip to content

Commit 981e372

Browse files
authored
MONGOID-2951 Mark attributes changed when accessed via #read_attribute / subscript operator (#5284)
* MONGOID-4726 proof of concept for indication of change using [] * MONGOID-4726 add test * reformat
1 parent 7bc68c1 commit 981e372

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/mongoid/attributes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ def has_attribute_before_type_cast?(name)
8686
def read_attribute(name)
8787
field = fields[name.to_s]
8888
raw = read_raw_attribute(name)
89-
field ? field.demongoize(raw) : raw
89+
value = field ? field.demongoize(raw) : raw
90+
attribute_will_change!(name.to_s) if value.resizable?
91+
value
9092
end
9193
alias :[] :read_attribute
9294

spec/mongoid/attributes_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,4 +2684,18 @@
26842684
end
26852685
end
26862686
end
2687+
2688+
context "when modifiying a hash referenced with the [] notation" do
2689+
let(:church) { Church.create!(location: { x: 1 }) }
2690+
2691+
before do
2692+
church[:location].merge!(y: 2)
2693+
church.save!
2694+
church.reload
2695+
end
2696+
2697+
it "persists the updated hash" do
2698+
church.location.should == { "x" => 1, "y" => 2 }
2699+
end
2700+
end
26872701
end

0 commit comments

Comments
 (0)