Permalink
Browse files

Copy changed_attributes across to newly become'd records

Without this, the original record's values won't get saved, since the partial insertions support (144e869) checks for changed values and thinks there are none.
  • Loading branch information...
1 parent df09ce9 commit b7bf025374324c2b73b3ab270cd4153f18a942b0 @jdelStrother jdelStrother committed Dec 24, 2013
Showing with 15 additions and 0 deletions.
  1. +1 −0 activerecord/lib/active_record/persistence.rb
  2. +14 −0 activerecord/test/cases/persistence_test.rb
@@ -181,6 +181,7 @@ def becomes(klass)
became = klass.new
became.instance_variable_set("@attributes", @attributes)
became.instance_variable_set("@attributes_cache", @attributes_cache)
+ became.instance_variable_set("@changed_attributes", @changed_attributes)
became.instance_variable_set("@new_record", new_record?)
became.instance_variable_set("@destroyed", destroyed?)
became.instance_variable_set("@errors", errors)
@@ -152,6 +152,20 @@ def test_becomes_includes_errors
assert_equal original_errors, client.errors
end
+ def test_dupd_becomes_persists_changes_from_the_original
+ original = topics(:first)
+ copy = original.dup.becomes(Reply)
+ copy.save!
+ assert_equal "The First Topic", Topic.find(copy.id).title
+ end
+
+ def test_becomes_includes_changed_attributes
+ company = Company.new(name: "37signals")
+ client = company.becomes(Client)
+ assert_equal "37signals", client.name
+ assert_equal %w{name}, client.changed
+ end
+
def test_delete_many
original_count = Topic.count
Topic.delete(deleting = [1, 2])

0 comments on commit b7bf025

Please sign in to comment.