From 86192bdf758c399a5346f37634a47c52fb437c5f Mon Sep 17 00:00:00 2001 From: kodawill Date: Wed, 27 Sep 2023 15:15:56 +0800 Subject: [PATCH 1/2] Add failing spec to demonstrate bug on autosave: true in accepts_nested_attributes_for --- spec/mongoid/attributes/nested_spec.rb | 30 +++++++++++++++++++ spec/mongoid/attributes/nested_spec_models.rb | 8 +++++ 2 files changed, 38 insertions(+) diff --git a/spec/mongoid/attributes/nested_spec.rb b/spec/mongoid/attributes/nested_spec.rb index 8943d382e3..94ba903481 100644 --- a/spec/mongoid/attributes/nested_spec.rb +++ b/spec/mongoid/attributes/nested_spec.rb @@ -66,6 +66,36 @@ expect(association).to_not be_autosave end end + + context "when autosave is explicitly false in association" do + context "when autosave is true as nested attributes" do + + let(:post) do + NestedPost.new + end + + before do + NestedPost.accepts_nested_attributes_for :likes, autosave: true + end + + after do + NestedPost.send(:undef_method, :likes_attributes=) + NestedPost.nested_attributes.clear + end + + let(:association) do + NestedPost.reflect_on_association(:likes) + end + + it "sets autosave to true" do + expect(association).to be_autosave + end + + it "autosaves if the association is not embedded" do + expect(post).to respond_to(:autosave_documents_for_likes) + end + end + end end describe "#initialize" do diff --git a/spec/mongoid/attributes/nested_spec_models.rb b/spec/mongoid/attributes/nested_spec_models.rb index 843b34bce5..8d72f5adc5 100644 --- a/spec/mongoid/attributes/nested_spec_models.rb +++ b/spec/mongoid/attributes/nested_spec_models.rb @@ -16,6 +16,12 @@ class NestedComment belongs_to :post, class_name: "NestedPost" end +class NestedLike + include Mongoid::Document + + belongs_to :post, class_name: "NestedPost" +end + class NestedPost include Mongoid::Document @@ -23,6 +29,8 @@ class NestedPost belongs_to :author, class_name: "NestedAuthor" has_many :comments, class_name: "NestedComment" accepts_nested_attributes_for :comments + has_many :likes, class_name: "NestedLike", autosave: false + accepts_nested_attributes_for :likes, autosave: true end class NestedBook From 46373fad694c447726bcd4fa1df23ac3fca363ce Mon Sep 17 00:00:00 2001 From: kodawill Date: Mon, 2 Oct 2023 13:10:48 +0800 Subject: [PATCH 2/2] Another test demonstrating the expected behavior to fail --- spec/mongoid/attributes/nested_spec.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/mongoid/attributes/nested_spec.rb b/spec/mongoid/attributes/nested_spec.rb index 94ba903481..679e6d4e12 100644 --- a/spec/mongoid/attributes/nested_spec.rb +++ b/spec/mongoid/attributes/nested_spec.rb @@ -91,9 +91,15 @@ expect(association).to be_autosave end - it "autosaves if the association is not embedded" do + it "defines autosave_documents_for_likes" do expect(post).to respond_to(:autosave_documents_for_likes) end + + it "saves associated NestedLike documents" do + like = NestedLike.new + post = NestedPost.new(likes: [like]) + expect { post.save }.to change(NestedLike, :count).by(1) + end end end end