Merge branch 'dougo_clear-new-record-on-synchronize'

master
Zach Dennis 2012-12-14 10:03:51 -05:00
commit 5f0e220038
3 changed files with 19 additions and 5 deletions

View File

@ -137,8 +137,8 @@ class ActiveRecord::Base
#
# # Example synchronizing unsaved/new instances in memory by using a uniqued imported field
# posts = [BlogPost.new(:title => "Foo"), BlogPost.new(:title => "Bar")]
# BlogPost.import posts, :synchronize => posts
# puts posts.first.new_record? # => false
# BlogPost.import posts, :synchronize => posts, :synchronize_keys => [:title]
# puts posts.first.persisted? # => true
#
# == On Duplicate Key Update (MySQL only)
#

View File

@ -43,6 +43,10 @@ module ActiveRecord # :nodoc:
instance.clear_aggregation_cache
instance.clear_association_cache
instance.instance_variable_set '@attributes', matched_instance.attributes
# Since the instance now accurately reflects the record in
# the database, ensure that instance.persisted? is true.
instance.instance_variable_set '@new_record', false
instance.instance_variable_set '@destroyed', false
end
end
end
@ -52,4 +56,4 @@ module ActiveRecord # :nodoc:
self.class.synchronize(instances, key)
end
end
end
end

View File

@ -134,8 +134,18 @@ describe "#import" do
let(:new_topics) { Build(3, :topics) }
it "reloads data for existing in-memory instances" do
Topic.import(new_topics, :synchronize => new_topics, :synchronize_key => [:title] )
assert new_topics.all?(&:new_record?), "Records should have been reloaded"
Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] )
assert new_topics.all?(&:persisted?), "Records should have been reloaded"
end
end
context "synchronizing on destroyed records with explicit conditions" do
let(:new_topics) { Generate(3, :topics) }
it "reloads data for existing in-memory instances" do
new_topics.each &:destroy
Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] )
assert new_topics.all?(&:persisted?), "Records should have been reloaded"
end
end
end