Browse files

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

  • Loading branch information...
2 parents 082a95d + d0c1055 commit 5f0e220038be3d3a0b625265eb03bf7b2ceec809 @zdennis committed Dec 14, 2012
Showing with 19 additions and 5 deletions.
  1. +2 −2 lib/activerecord-import/import.rb
  2. +5 −1 lib/activerecord-import/synchronize.rb
  3. +12 −2 test/import_test.rb
View
4 lib/activerecord-import/import.rb
@@ -137,8 +137,8 @@ def supports_on_duplicate_key_update?
#
# # 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
6 lib/activerecord-import/synchronize.rb
@@ -43,6 +43,10 @@ def self.synchronize(instances, keys=[self.primary_key])
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 @@ def synchronize(instances, key=[ActiveRecord::Base.primary_key])
self.class.synchronize(instances, key)
end
end
-end
+end
View
14 test/import_test.rb
@@ -134,8 +134,18 @@
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

0 comments on commit 5f0e220

Please sign in to comment.