Permalink
Browse files

completely ignore new records that have :_delete => 1

  • Loading branch information...
1 parent 41f7a39 commit dcc76e33738e245a214fbc19a0d351e031f0017e @cainlevy committed Dec 13, 2008
Showing with 19 additions and 8 deletions.
  1. +9 −8 lib/nested_assignment.rb
  2. +10 −0 test/unit/nested_assignment_test.rb
View
@@ -21,23 +21,24 @@ def accessible_associations(*associations)
define_method("#{name}_params=") do |row|
assoc = self.send(name)
- record = row[:id].blank? ? assoc.build : [assoc].detect{|r| r.id == row[:id].to_i}
- if row[:_delete]
- record._delete = true
+ if row[:_delete].to_s == "1"
+ [assoc].detect{|r| r.id == row[:id].to_i}._delete = true if row[:id]
else
- record.attributes = row
+ record = row[:id].blank? ? assoc.build : [assoc].detect{|r| r.id == row[:id].to_i}
+ record.attributes = row.except(:id, :_delete)
end
end
# plural collections
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
+ if row[:_delete].to_s == "1"
+ assoc.detect{|r| r.id == row[:id].to_i}._delete = true if row[:id]
else
- record.attributes = row
+ record = row[:id].blank? ? assoc.build : assoc.detect{|r| r.id == row[:id].to_i}
+ record.attributes = row.except(:id, :_delete)
end
end
end
@@ -121,6 +121,16 @@ def test_removing_a_task
assert @user.tasks.detect{|s| s == @task}._delete, "the associated record is marked for deletion"
assert_equal "review", @user.tasks.detect{|s| s == @task}.name, "the association attribute did not update"
end
+
+ def test_adding_a_deletable_task
+ @user.tasks_params = {
+ "1" => {
+ :name => "refactor",
+ :_delete => "1"
+ }
+ }
+ assert !@user.tasks.any?(&:new_record?), "the association wasn't even built"
+ end
end
class NestedAssignmentHasAndBelongsToManyTest < ActiveSupport::TestCase

0 comments on commit dcc76e3

Please sign in to comment.