Permalink
Browse files

tests for save_with_associated

  • Loading branch information...
1 parent c4ff910 commit 657810daec470de139d3ae1b3cd67da7a20bdf0b @cainlevy committed Dec 12, 2008
Showing with 40 additions and 1 deletion.
  1. +5 −0 lib/nested_assignment.rb
  2. +35 −1 test/unit/nested_assignment_saving_test.rb
View
@@ -71,6 +71,11 @@ def save_with_associated
end
end
+ # Without this, we may not save deeply nested and changed records.
+ # For example, suppose that User -> Task -> Tags, and that we change
+ # an attribute on a tag but not on the task. Then when we are saving
+ # the user, we would want to say that the task had changed so we
+ # could then recurse and discover that the tag had changed.
def changed_with_associated?
changed_without_associated? or instantiated_associated.any?(&:changed?)
end
@@ -1,5 +1,39 @@
require File.dirname(__FILE__) + '/../test_helper'
class NestedAssignmentSavingTest < ActiveSupport::TestCase
-
+ def test_saving_with_new_associated_records
+ @user = users(:bob)
+ @user.tasks.build(:name => "research")
+ assert_difference "Task.count" do
+ @user.save
+ end
+ end
+
+ def test_saving_with_modified_existing_associated_records
+ @user = users(:bob)
+ @user.tasks[0].name = "research"
+ assert_no_difference "Task.count" do
+ @user.save
+ end
+ assert_equal "research", @user.reload.tasks[0].name
+ end
+
+ def test_saving_with_existing_associated_records_marked_for_deletion
+ @user = users(:bob)
+ @user.tasks[0]._delete = true
+ assert_difference "Task.count", -1 do
+ @user.save
+ end
+ end
+
+ def test_saving_with_modified_existing_deeply_associated_records
+ @user = users(:bob)
+ @user.tasks[0].tags[0].name = "difficult"
+ assert_no_difference "Task.count" do
+ assert_no_difference "Tag.count" do
+ @user.save
+ end
+ end
+ assert_equal "difficult", @user.reload.tasks[0].tags[0].name
+ end
end

0 comments on commit 657810d

Please sign in to comment.