Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

failing test for has_one/has_many nested attributes #978

Closed
wants to merge 8 commits into from

2 participants

@brewster1134

#977

has_one: sets as dirty but does not update
has_many: updates but does not set as dirty

@gregory-m

Hi. Can you please squash commits and reopen pull request.

Thank you.

@gregory-m gregory-m closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 11, 2011
  1. added new lines

    Brewster authored
Commits on Jun 13, 2011
  1. added test for has_many nested attributes

    Brewster authored
  2. removed topping model

    Brewster authored
  3. add dirty check for nested objects

    Brewster authored
Commits on Jun 14, 2011
  1. small fixes to dirty test

    Brewster authored
  2. added dirty check for has_one

    Brewster authored
  3. added tests for validation and errors

    Brewster authored
This page is out of date. Refresh to see the latest.
View
83 spec/functional/mongoid/nested_attributes_spec.rb
@@ -3370,5 +3370,88 @@
end
end
end
+
+ context "with 'has_one' association" do
+ let(:pizza) do
+ Pizza.create
+ end
+
+ context "that is valid" do
+ let!(:crust) do
+ pizza.create_crust(:type => "Thin")
+ end
+ let(:params) do
+ { :crust_attributes =>
+ { :type => "Thick" }
+ }
+ end
+ before do
+ pizza.update_attributes(params)
+ end
+ it "should set nested object to a dirty state" do
+ pizza.crust.should be_type_changed
+ end
+ it "should update nested attributes" do
+ pizza.reload.crust.type.should == "Thick"
+ end
+ end
+
+ context "that is invalid" do
+ Crust.validates_presence_of :type
+ let!(:crust) do
+ pizza.create_crust
+ end
+ it "should set nested object to be invalid" do
+ crust.should_not be_valid
+ end
+ it "should add error to nested object" do
+ crust.errors[:name].should be_present
+ end
+ end
+
+ end
+
+ context "with 'has_many' association" do
+
+ let(:pizza) do
+ Pizza.create
+ end
+
+ context "that is valid" do
+ let!(:topping) do
+ pizza.toppings.create(:name => "Sausage")
+ end
+
+ let(:params) do
+ { :toppings_attributes =>
+ { "0" => {:name => "Pepperoni", :id => topping.id.to_s} }
+ }
+ end
+
+ before do
+ pizza.attributes = params
+ end
+
+ it "should set nested object to be dirty" do
+ pizza.toppings.first.should be_name_changed
+ end
+ it "should update nested attributes" do
+ pizza.update
+ pizza.reload.toppings.first.name.should == "Pepperoni"
+ end
+ end
+ context "that is invalid" do
+ Topping.validates_presence_of :name
+ let!(:topping) do
+ pizza.toppings.create(:name => "Sausage")
+ end
+ it "should set nested object to be invalid" do
+ topping.should_not be_valid
+ end
+ it "should add error to nested object" do
+ topping.errors[:name].should be_present
+ end
+ end
+ end
end
end
View
16 spec/models/pizza.rb
@@ -0,0 +1,16 @@
+class Pizza
+ include Mongoid::Document
+ has_one :crust
+ has_many :toppings
+ accepts_nested_attributes_for :crust, :toppings
+end
+class Crust
+ include Mongoid::Document
+ field :type
+ belongs_to :pizza
+end
+class Topping
+ include Mongoid::Document
+ field :name
+ belongs_to :pizza
+end
Something went wrong with that request. Please try again.