Permalink
Browse files

added #{association_name}_attributes reader

Signed-off-by: Martin Gamsjaeger <gamsnjaga@gmail.com>
  • Loading branch information...
1 parent 6d26213 commit 4c5e65662edb1991decbcb3783aedc3e065d4d09 @roovo roovo committed with Apr 14, 2009
View
15 lib/dm-accepts_nested_attributes/nested_attributes.rb
@@ -9,9 +9,14 @@ def self.included(base)
module ClassMethods
- # Defines an attributes writer for the specified association(s). If you
- # are using <tt>attr_protected</tt> or <tt>attr_accessible</tt>, then you
- # will need to add the attribute writer to the allowed list.
+ # Defines an attributes reader and writer for the specified association(s).
+ # If you are using <tt>attr_protected</tt> or <tt>attr_accessible</tt>,
+ # then you will need to add the attribute writer to the allowed list.
+ #
+ # After any params are passed to the attributes writer they are available
+ # via the attributes reader (they are stored in an instance variable of
+ # the same name). The attributes reader returns nil if the attributes
+ # writer has not been called.
#
# Supported options:
# [:allow_destroy]
@@ -26,8 +31,10 @@ module ClassMethods
# do not have a <tt>_delete</tt> that evaluates to true.
#
# Examples:
+ # # creates avatar_attributes
# # creates avatar_attributes=
# accepts_nested_attributes_for :avatar, :reject_if => proc { |attributes| attributes['name'].blank? }
+ # # creates avatar_attributes and posts_attributes
# # creates avatar_attributes= and posts_attributes=
# accepts_nested_attributes_for :avatar, :posts, :allow_destroy => true
def accepts_nested_attributes_for(association_name, options = {})
@@ -57,9 +64,11 @@ def accepts_nested_attributes_for(association_name, options = {})
class_eval %{
def #{association_name}_attributes
+ @#{association_name}_attributes
end
def #{association_name}_attributes=(attributes)
+ @#{association_name}_attributes = attributes
assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, #{options[:allow_destroy]})
end
View
15 spec/integration/belongs_to_spec.rb
@@ -112,7 +112,19 @@
Profile.all.size.should == 1
Person.all.size.should == 0
end
+
+ end
+ describe "every accessible belongs_to association with a nested attributes reader", :shared => true do
+
+ it "should return the attributes written to Profile#person_attributes from the Profile#person_attributes reader" do
+ @profile.person_attributes.should be_nil
+
+ @profile.person_attributes = { :name => 'Martin' }
+
+ @profile.person_attributes.should == { :name => 'Martin' }
+ end
+
end
describe "Profile.belongs_to(:person)" do
@@ -127,6 +139,7 @@
it_should_behave_like "every accessible belongs_to association with no reject_if proc"
it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
+ it_should_behave_like "every accessible belongs_to association with a nested attributes reader"
end
@@ -201,4 +214,4 @@
end
-end
+end
View
15 spec/integration/has_1_spec.rb
@@ -101,6 +101,18 @@
end
end
+
+ describe "every accessible has(1) association with a nested attributes reader", :shared => true do
+
+ it "should return the attributes written to Person#profile_attributes from the Person#profile_attributes reader" do
+ @person.profile_attributes.should be_nil
+
+ @person.profile_attributes = { :nick => 'snusnu' }
+
+ @person.profile_attributes.should == { :nick => 'snusnu' }
+ end
+
+ end
describe "Person.has(1, :profile)" do
@@ -114,6 +126,7 @@
it_should_behave_like "every accessible has(1) association with no reject_if proc"
it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
+ it_should_behave_like "every accessible has(1) association with a nested attributes reader"
end
@@ -190,4 +203,4 @@
end
-end
+end
View
15 spec/integration/has_n_spec.rb
@@ -91,6 +91,18 @@
end
end
+
+ describe "every accessible has(n) association with a nested attributes reader", :shared => true do
+
+ it "should return the attributes written to Project#task_attributes from the Project#task_attributes reader" do
+ @project.tasks_attributes.should be_nil
+
+ @project.tasks_attributes = { 'new_1' => { :name => 'write specs' } }
+
+ @project.tasks_attributes.should == { 'new_1' => { :name => 'write specs' } }
+ end
+
+ end
describe "Project.has(n, :tasks)" do
@@ -104,6 +116,7 @@
it_should_behave_like "every accessible has(n) association with no reject_if proc"
it_should_behave_like "every accessible has(n) association with :allow_destroy => false"
+ it_should_behave_like "every accessible has(n) association with a nested attributes reader"
end
@@ -178,4 +191,4 @@
end
-end
+end
View
14 spec/integration/has_n_through_renamed_spec.rb
@@ -31,7 +31,6 @@
Tagging.all.size.should == 0
Photo.all.size.should == 0
- # debugger
@tag.pictures_attributes = { 'new_1' => { :name => 'dm-accepts_nested_attributes' } }
@tag.pictures.should_not be_empty
@tag.pictures.first.name.should == 'dm-accepts_nested_attributes'
@@ -112,6 +111,18 @@
end
end
+
+ describe "every accessible has(n, :through) renamed association with a nested attributes reader", :shared => true do
+
+ it "should return the attributes written to Tag#pictures_attributes from the Tag#pictures_attributes reader" do
+ @tag.pictures_attributes.should be_nil
+
+ @tag.pictures_attributes = { 'new_1' => { :name => 'write specs' } }
+
+ @tag.pictures_attributes.should == { 'new_1' => { :name => 'write specs' } }
+ end
+
+ end
describe "Tag.has(n, :pictures, :through => :taggings) renamed" do
@@ -125,6 +136,7 @@
it_should_behave_like "every accessible has(n, :through) renamed association with no reject_if proc"
it_should_behave_like "every accessible has(n, :through) renamed association with :allow_destroy => false"
+ it_should_behave_like "every accessible has(n, :through) renamed association with a nested attributes reader"
end
View
16 spec/integration/has_n_through_spec.rb
@@ -110,6 +110,18 @@
end
end
+
+ describe "every accessible has(n, :through) association with a nested attributes reader", :shared => true do
+
+ it "should return the attributes written to Person#projects_attributes from the Person#projects_attributes reader" do
+ @person.projects_attributes.should be_nil
+
+ @person.projects_attributes = { 'new_1' => { :name => 'write specs' } }
+
+ @person.projects_attributes.should == { 'new_1' => { :name => 'write specs' } }
+ end
+
+ end
describe "Person.has(n, :projects, :through => :project_memberships)" do
@@ -123,6 +135,7 @@
it_should_behave_like "every accessible has(n, :through) association with no reject_if proc"
it_should_behave_like "every accessible has(n, :through) association with :allow_destroy => false"
+ it_should_behave_like "every accessible has(n, :through) association with a nested attributes reader"
end
@@ -197,4 +210,5 @@
end
-end
+end
+

0 comments on commit 4c5e656

Please sign in to comment.