Browse files

linking the broken test to a rubyonrails-core thread

  • Loading branch information...
1 parent 5f3b15f commit 1585173445129078f0a502fd07e016a6758e234d @cainlevy committed Dec 12, 2008
Showing with 5 additions and 7 deletions.
  1. +4 −7 lib/nested_assignment.rb
  2. +1 −0 test/unit/nested_assignment_test.rb
View
11 lib/nested_assignment.rb
@@ -11,27 +11,24 @@ def self.included(base)
end
module ClassMethods
- # Parallels attr_accessible. Could easily trigger from :accessible => true instead.
+ # Parallels attr_accessible. Could easily trigger from an association option (e.g. :accessible => true)
+ # or even from attr_accessible itself (cool!).
def accessible_associations(*associations)
associations.each do |name|
+ # singular associations
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].detect{|r| r.id == row[:id].to_i}
if row[:_delete]
record._delete = true
else
record.attributes = row
end
end
+ # plural collections
else
define_method("#{name}_params=") do |hash|
assoc = self.send(name)
View
1 test/unit/nested_assignment_test.rb
@@ -22,6 +22,7 @@ def test_replacing_the_avatar
}
assert @user.avatar.new_record?, "the association is a new object"
assert_equal "Bobtastic", @user.avatar.name, "the new record has the specified name"
+ # NOTE: see http://groups.google.com/group/rubyonrails-core/browse_thread/thread/18cf8b059f5ded4a
assert !@avatar.reload.user.nil?, "the previously associated object has not been disassociated yet"
end

0 comments on commit 1585173

Please sign in to comment.