Skip to content

Commit

Permalink
Fix embedded elements memoization following #changed?
Browse files Browse the repository at this point in the history
- Fix #3991
- Close mongodb#3992
  • Loading branch information
durran committed Jun 17, 2015
1 parent f56b96b commit b7bea2d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ For instructions on upgrading to newer versions, visit

* \#3993 Fixes issue where `dup`/`clone` fails for embedded documents that use store_as without using Mongoid::Atributes::Dynamic

* \#3991 Fixed emebdded documents not flagging as changed after calling #changed? and modifying the
child elements.

* \#3874 Adding snapshot option to context.

* \#3868 Loading models in rake tasks now expands the rails path.
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/relations/embedded/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def build(attributes = {}, type = nil)
doc.apply_post_processed_defaults
yield(doc) if block_given?
doc.run_callbacks(:build) { doc }
base._reset_memoized_children!
doc
end
alias :new :build
Expand Down
15 changes: 15 additions & 0 deletions lib/mongoid/traversable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ def reset_persisted_children
child.move_changes
child.new_record = false
end
_reset_memoized_children!
end

# Resets the memoized children on the object. Called internally when an
# embedded array changes size.
#
# @api semiprivate
#
# @example Reset the memoized children.
# document._reset_memoized_children!
#
# @return [ nil ] nil.
#
# @since 5.0.0
def _reset_memoized_children!
@__children = nil
end

Expand Down
52 changes: 52 additions & 0 deletions spec/mongoid/changeable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,58 @@
end
end

context "when changed? has been called before child elements size change" do

let(:person) do
Person.create
end

let(:address) do
person.addresses.create(street: "hobrecht")
end

let!(:location) do
address.locations.create(name: "home")
end

before do
person.changed?
end

context "when adding via new" do

before do
address.locations.new
end

it "returns true" do
expect(person).to be_changed
end
end

context "when adding via build" do

before do
address.locations.build
end

it "returns true" do
expect(person).to be_changed
end
end

context "when adding via create" do

before do
address.locations.create
end

it "returns false" do
expect(person).to_not be_changed
end
end
end

context "when a deeply embedded child has changed" do

let(:person) do
Expand Down

0 comments on commit b7bea2d

Please sign in to comment.