Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

writing test coverage for two conflicting saving patterns this plugin…

… needs to support.
  • Loading branch information...
commit 5828485d657c1a8f468f4ea64f3ba273fff532b0 1 parent ed595a7
@cainlevy authored
View
4 lib/nested_assignment.rb
@@ -69,11 +69,13 @@ def valid_with_associated?(*args)
# deep saving of any new, changed, or deleted records.
def create_or_update_with_associated(*args)
+ without_recursion(:create_or_update){
self.class.transaction do
create_or_update_without_associated(*args) &&
- without_recursion(:create_or_update){modified_associated.all?{|a| a.save(*args)}} &&
+ modified_associated.all?{|a| a.save(*args)} &&
deletable_associated.all?{|a| a.destroy}
end
+ }
end
# Without this, we may not save deeply nested and changed records.
View
4 test/db/models.rb
@@ -52,4 +52,8 @@ class Group < ActiveRecord::Base
validates_presence_of :name
end
+
+ class Event < ActiveRecord::Base
+ belongs_to :entity, :polymorphic => true
+ end
end
View
5 test/db/schema.rb
@@ -34,4 +34,9 @@
t.column :user_id, :integer
end
+ create_table :events, :force => true do |t|
+ t.column :entity_id, :integer
+ t.column :entity_type, :string
+ t.column :name, :string
+ end
end
View
45 test/unit/nested_assignment_saving_test.rb
@@ -37,28 +37,41 @@ def test_saving_with_modified_existing_deeply_associated_records
assert_equal "difficult", @user.reload.tasks[0].tags[0].name
end
- def test_saving_with_recursive_references
- # This recursive situation is a little contrived. A more likely example would be
- # a new associated record that refers back to the first. For example, suppose you
- # store events, and after the user modifies his name you wish to store the fact.
- # You may do something like `Event.create(:user => self, :change => 'name')`. This
- # would create a recursive reference such as here.
+ def test_saving!
@user = users(:bob)
- @user.name = "william"
@user.tasks[0].name = "research"
- @user.tasks[0].user = @user
- assert_nothing_raised do
- @user.save
+ @user.save!
+ assert_equal "research", @user.reload.tasks[0].name
+ end
+
+ class UserWithEvent < User
+ after_save do |user|
+ PluginTestModels::Event.create(:entity => user)
+ end
+ end
+
+ def test_saving_a_modified_record_that_spawns_an_associated_record
+ @user = UserWithEvent.find(:first)
+ @user.name = "william"
+ assert_difference "Event.count", 1 do
+ assert_nothing_raised do
+ @user.save
+ end
end
@user.reload
assert_equal "william", @user.name
- assert_equal "research", @user.tasks[0].name
end
- def test_saving!
- @user = users(:bob)
- @user.tasks[0].name = "research"
- @user.save!
- assert_equal "research", @user.reload.tasks[0].name
+ class UserWithResave < User
+ after_create do |user|
+ user.update_attribute(:name, "#{user.name} (verified)")
+ end
+ end
+
+ def test_saving_a_new_record_with_a_single_resave
+ @user = UserWithResave.new
+ @user.name = "george"
+ @user.save
+ assert_equal "george (verified)", @user.reload.name
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.