From 58cac8bfb0beb4283c392264d47dc601dde5da68 Mon Sep 17 00:00:00 2001 From: Doug Orleans Date: Mon, 3 Sep 2012 02:15:56 -0400 Subject: [PATCH 1/5] Test that no records are new after synchronize, not all. Also fix the spelling of :synchronize_keys. --- test/import_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/import_test.rb b/test/import_test.rb index efc1ee9..d9440bf 100644 --- a/test/import_test.rb +++ b/test/import_test.rb @@ -134,8 +134,8 @@ 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.any?(&:new_record?), "Records should have been reloaded" end end end From 31ff20a3325a3afb53b17ddf855a6f6c0353931b Mon Sep 17 00:00:00 2001 From: Doug Orleans Date: Mon, 3 Sep 2012 02:24:00 -0400 Subject: [PATCH 2/5] Fix failing test - clear @new_record on synchronize. --- lib/activerecord-import/synchronize.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/activerecord-import/synchronize.rb b/lib/activerecord-import/synchronize.rb index 60408e5..cf97dbe 100644 --- a/lib/activerecord-import/synchronize.rb +++ b/lib/activerecord-import/synchronize.rb @@ -43,6 +43,7 @@ module ActiveRecord # :nodoc: instance.clear_aggregation_cache instance.clear_association_cache instance.instance_variable_set '@attributes', matched_instance.attributes + instance.instance_variable_set '@new_record', false end end end @@ -52,4 +53,4 @@ module ActiveRecord # :nodoc: self.class.synchronize(instances, key) end end -end \ No newline at end of file +end From 413e35feddd8f86d16bfdab6b8291986ba2a8014 Mon Sep 17 00:00:00 2001 From: Doug Orleans Date: Mon, 3 Sep 2012 02:29:42 -0400 Subject: [PATCH 3/5] Add failing test for destroyed records. Also, generalize the previous test to use persisted? rather than !new_record?. --- test/import_test.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/import_test.rb b/test/import_test.rb index d9440bf..cc06891 100644 --- a/test/import_test.rb +++ b/test/import_test.rb @@ -135,7 +135,17 @@ describe "#import" do it "reloads data for existing in-memory instances" do Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] ) - assert !new_topics.any?(&:new_record?), "Records should have been reloaded" + 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 From 9fd19a36bd93127d977ca46d5aab0350c966614a Mon Sep 17 00:00:00 2001 From: Doug Orleans Date: Mon, 3 Sep 2012 02:32:49 -0400 Subject: [PATCH 4/5] Also clear the destroyed flag after synchronizing. --- lib/activerecord-import/synchronize.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/activerecord-import/synchronize.rb b/lib/activerecord-import/synchronize.rb index cf97dbe..a5286d7 100644 --- a/lib/activerecord-import/synchronize.rb +++ b/lib/activerecord-import/synchronize.rb @@ -43,7 +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 From d0c1055b656c52b02f67c56c8bab63ac9c2587f0 Mon Sep 17 00:00:00 2001 From: Doug Orleans Date: Mon, 3 Sep 2012 02:40:51 -0400 Subject: [PATCH 5/5] Fix example in doc comment. --- lib/activerecord-import/import.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/activerecord-import/import.rb b/lib/activerecord-import/import.rb index 0646c1f..a914934 100644 --- a/lib/activerecord-import/import.rb +++ b/lib/activerecord-import/import.rb @@ -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) #