Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

chain on create_or_update instead of save, to also work with save!

  • Loading branch information...
commit 408f47d446f6946e1938331b7665fe10dbd8c0cd 1 parent f09fc31
@cainlevy authored
View
8 lib/nested_assignment.rb
@@ -6,7 +6,7 @@ def self.included(base)
base.class_eval do
extend ClassMethods
- alias_method_chain :save, :associated
+ alias_method_chain :create_or_update, :associated
alias_method_chain :valid?, :associated
# alias_method_chain :changed?, :associated
end
@@ -68,10 +68,10 @@ def valid_with_associated?(*args)
end
# deep saving of any new, changed, or deleted records.
- def save_with_associated(*args)
+ def create_or_update_with_associated(*args)
self.class.transaction do
- save_without_associated(*args) &&
- without_recursion(:save){modified_associated.all?{|a| a.save}} &&
+ create_or_update_without_associated(*args) &&
+ without_recursion(:create_or_update){modified_associated.all?{|a| a.save}} &&
deletable_associated.all?{|a| a.destroy}
end
end
View
7 test/unit/nested_assignment_saving_test.rb
@@ -54,4 +54,11 @@ def test_saving_with_recursive_references
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
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.