Permalink
Browse files

simplifying the parameter format for singular associations

  • Loading branch information...
1 parent 92b9483 commit 5f3b15f22d4401399b366a14b41aca831eaf5bfd @cainlevy committed Dec 12, 2008
Showing with 35 additions and 34 deletions.
  1. +18 −5 lib/nested_assignment.rb
  2. +17 −29 test/unit/nested_assignment_test.rb
View
@@ -15,24 +15,37 @@ module ClassMethods
def accessible_associations(*associations)
associations.each do |name|
- define_method("#{name}_params=") do |hash|
- assoc = self.send(name)
- hash.values.each do |row|
+ if [:belongs_to, :has_one].include? self.reflect_on_association(name).macro
+ define_method("#{name}_params=") do |row|
+ assoc = self.send(name)
+
# TODO: need to bypass the replace() call inside singular associations (has_one and belongs_to). but they
# do serve a purpose: disassociating or destroying an existing record. if that is not to happen during
# assignment, then those records need to be collected for later disassociation (or removal, if :dependent
# => :destroy). that would need to be part of the saving process. ALSO, this makes sense to handle while
# deleting from plural associations. so perhaps instead of setting #_delete, i should add to a
# disassociation hash for later.
- record = row[:id].blank? ? assoc.build : [assoc].flatten.detect{|r| r.id == row[:id].to_i}
+ record = row[:id].blank? ? assoc.build : [assoc].detect{|r| r.id == row[:id].to_i}
if row[:_delete]
record._delete = true
else
record.attributes = row
end
end
+ else
+ define_method("#{name}_params=") do |hash|
+ assoc = self.send(name)
+ hash.values.each do |row|
+ record = row[:id].blank? ? assoc.build : assoc.detect{|r| r.id == row[:id].to_i}
+ if row[:_delete]
+ record._delete = true
+ else
+ record.attributes = row
+ end
+ end
+ end
end
-
+
end
end
@@ -6,36 +6,30 @@ def setup
@avatar = avatars(:bobs_avatar)
end
- def test_updating_a_avatar
+ def test_updating_the_avatar
@user.avatar_params = {
- "1" => {
- :id => @avatar.id,
- :name => "Bobtastic"
- }
+ :id => @avatar.id,
+ :name => "Bobtastic"
}
assert !@user.avatar.new_record?, "the association was not rebuilt"
assert_equal "Bobtastic", @user.avatar.name, "the existing associated record's name has changed"
assert_equal "mugshot", @avatar.reload.name, "the name change has not been saved"
end
- def test_assigning_a_replacement_avatar
+ def test_replacing_the_avatar
@user.avatar_params = {
- "1" => {
- :name => "Bobtastic"
- }
+ :name => "Bobtastic"
}
assert @user.avatar.new_record?, "the association is a new object"
assert_equal "Bobtastic", @user.avatar.name, "the new record has the specified name"
assert !@avatar.reload.user.nil?, "the previously associated object has not been disassociated yet"
end
- def test_assigning_a_removed_avatar
+ def test_removing_the_avatar
@user.avatar_params = {
- "1" => {
- :id => @avatar.id,
- :name => "Bobtastic",
- :_delete => "1"
- }
+ :id => @avatar.id,
+ :name => "Bobtastic",
+ :_delete => "1"
}
assert @user.avatar._delete, "the association is marked for deletion"
assert_nothing_raised("the associated object has not been deleted yet") do @avatar.reload end
@@ -51,34 +45,28 @@ def setup
def test_updating_the_manager
@user.manager_params = {
- "1" => {
- :id => @manager.id,
- :name => "Susan"
- }
+ :id => @manager.id,
+ :name => "Susan"
}
assert !@user.manager.new_record?, "the association was not rebuilt"
assert_equal "Susan", @user.manager.name, "the existing associated record's name has changed"
assert_equal "Sue", @manager.reload.name, "the name change has not been saved"
end
- def test_assigning_a_replacement_manager
+ def test_replacing_the_manager
@user.manager_params = {
- "1" => {
- :name => "Susan"
- }
+ :name => "Susan"
}
assert @user.manager.new_record?, "the association is a new object"
assert_equal "Susan", @user.manager.name, "the new record has the specified name"
assert !@user.reload.manager.nil?, "the previously associated object has not been disassociated yet"
end
- def test_assigning_a_removed_manager
+ def test_removing_the_manager
@user.manager_params = {
- "1" => {
- :id => @manager.id,
- :name => "Susan",
- :_delete => "1"
- }
+ :id => @manager.id,
+ :name => "Susan",
+ :_delete => "1"
}
assert @user.manager._delete, "the association is marked for deletion"
assert_nothing_raised("the associated object has not been deleted yet") do @manager.reload end

0 comments on commit 5f3b15f

Please sign in to comment.