Skip to content
Browse files

Merge pull request #8 from adelyte/master

typecast extracted keys
  • Loading branch information...
2 parents 4bc774a + 3fc222b commit 0a42e3f133f39e451acbb24558bbcdb7cccb33de @snusnu committed Aug 11, 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
View
1 lib/dm-accepts_nested_attributes/relationship.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
11 spec/one_to_one_spec.rb
@@ -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
View
86 spec/shared/one_to_one_spec.rb
@@ -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 0a42e3f

Please sign in to comment.
Something went wrong with that request. Please try again.