Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autosave for accepts_nested_attributes_for #5725

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions spec/mongoid/attributes/nested_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@
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 "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

describe "#initialize" do
Expand Down
8 changes: 8 additions & 0 deletions spec/mongoid/attributes/nested_spec_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ 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

field :title, type: String
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
Expand Down