Permalink
Browse files

Fixed failure to typecast associated keys when updating a one-to-one …

…association.
  • Loading branch information...
1 parent f1ed3e8 commit 3fc222bf6baf15a0e78892fe5f45e85e65af9421 @adelyte-chris adelyte-chris committed Aug 2, 2011
Showing with 74 additions and 24 deletions.
  1. +1 −0 lib/dm-accepts_nested_attributes/relationship.rb
  2. +7 −4 spec/one_to_one_spec.rb
  3. +66 −20 spec/shared/one_to_one_spec.rb
@@ -32,6 +32,7 @@ def extract_keys_for_nested_attributes(model, attributes)
attributes[key.name]
end
end
+ key_values = target_model_key.typecast(key_values)
verify_key_values_for_nested_attributes(key_values)
end
View
@@ -14,6 +14,8 @@ class ::Person
has 1, :profile, constraint(:destroy)
has 1, :address, :through => :profile
+
+ accepts_nested_attributes_for(:profile)
end
@@ -26,15 +28,16 @@ class ::Profile
belongs_to :person
has 1, :address
+
+ accepts_nested_attributes_for(:address)
end
-
+
class ::Address
include DataMapper::Resource
property :id, Serial
- property :profile_id, Integer, :required => true, :unique => true, :unique_index => true, :min => 0
property :body, String, :required => true
belongs_to :profile
@@ -50,8 +53,8 @@ class ::Address
Profile.all.destroy!
Person.all.destroy!
end
-
-
+
+
describe "Person.accepts_nested_attributes_for(:profile)" do
before(:all) do
@@ -1,26 +1,72 @@
describe "every accessible one_to_one association", :shared => true do
- it "should allow to update an existing profile via Person#profile_attributes" do
- person = Person.create :name => 'Martin'
- profile = Profile.create(:person => person, :nick => 'snusnu')
- person.reload
-
- Person.all.size.should == 1
- Profile.all.size.should == 1
-
- person.profile_attributes = { :id => profile.id, :nick => 'still snusnu somehow' }
- person.save.should be_true
-
- Person.all.size.should == 1
- Profile.all.size.should == 1
- Profile.first.nick.should == 'still snusnu somehow'
+ describe "Person#profile_attributes" do
+ it "should allow to update an existing profile" do
+ person = Person.create :name => 'Martin'
+ profile = Profile.create(:person => person, :nick => 'snusnu')
+ person.reload
+
+ Person.all.size.should == 1
+ Profile.all.size.should == 1
+
+ person.profile_attributes = { :id => profile.id, :nick => 'still snusnu somehow' }
+ person.save.should be_true
+
+ Person.all.size.should == 1
+ Profile.all.size.should == 1
+ Profile.first.nick.should == 'still snusnu somehow'
+ end
+
+ it "should typecast the profile key" do
+ person = Person.create :name => 'Martin'
+ profile = Profile.create(:person => person, :nick => 'snusnu')
+ person.reload
+
+ Person.all.size.should == 1
+ Profile.all.size.should == 1
+
+ person.profile_attributes = { :id => profile.id.to_s, :nick => 'still snusnu somehow' }
+ person.profile.id.should == profile.id
+ person.save.should be_true
+
+ Person.all.size.should == 1
+ Profile.all.size.should == 1
+ Profile.first.nick.should == 'still snusnu somehow'
+ end
+
+ it "should return the attributes written from the reader" do
+ person = Person.new :name => 'Martin'
+ person.profile_attributes.should be_nil
+ person.profile_attributes = { :nick => 'snusnu' }
+ person.profile_attributes.should == { :nick => 'snusnu' }
+ end
end
-
- it "should return the attributes written to Person#profile_attributes from the Person#profile_attributes reader" do
- person = Person.new :name => 'Martin'
- person.profile_attributes.should be_nil
- person.profile_attributes = { :nick => 'snusnu' }
- person.profile_attributes.should == { :nick => 'snusnu' }
+
+ describe "with deeply-nested associations" do
+ describe "Profile.accepts_nested_attributes_for(:address)" do
+ before(:all) do
+ Profile.accepts_nested_attributes_for(:address)
+ end
+
+ it "should allow to update an existing address via Person#profile_attributes" do
+ person = Person.create :name => 'Barak Obama'
+ profile = Profile.create(:person => person, :nick => 'Renegade')
+ address = Address.create(:profile => profile, :body => '1600 Pennsylvania Ave, Washington DC')
+ person.reload
+
+ Person.all.size.should == 1
+ Profile.all.size.should == 1
+ Address.all.size.should == 1
+
+ person.update(:profile_attributes => { :id => profile.id, :address_attributes => { :id => address.id, :body => 'Camp David, Thurmont MA' } })
+ person.save.should be_true
+
+ Person.all.size.should == 1
+ Profile.all.size.should == 1
+ Address.all.size.should == 1
+ Address.first.body.should == 'Camp David, Thurmont MA'
+ end
+ end
end
end

0 comments on commit 3fc222b

Please sign in to comment.