From 796f8e7b8452ad6d46b83db2fb97a62943dd100b Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Tue, 9 Oct 2018 12:20:17 -0400 Subject: [PATCH] Add option to include uniqueness validators --- lib/activerecord-import/import.rb | 2 +- test/import_test.rb | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/activerecord-import/import.rb b/lib/activerecord-import/import.rb index 786b79f6..0e6a1ca5 100644 --- a/lib/activerecord-import/import.rb +++ b/lib/activerecord-import/import.rb @@ -42,7 +42,7 @@ def valid_model?(model) model._validate_callbacks.each do |callback| filter = callback.raw_filter - if filter.is_a?(ActiveRecord::Validations::UniquenessValidator) || + if (!@options[:validate_uniqueness] && filter.is_a?(ActiveRecord::Validations::UniquenessValidator)) || (defined?(ActiveRecord::Validations::PresenceValidator) && filter.is_a?(ActiveRecord::Validations::PresenceValidator) && associations.include?(filter.attributes.first)) validate_callbacks.delete(callback) end diff --git a/test/import_test.rb b/test/import_test.rb index aec7fbf9..c3b28c1d 100644 --- a/test/import_test.rb +++ b/test/import_test.rb @@ -210,9 +210,9 @@ end it "should ignore uniqueness validators" do - Topic.import columns, valid_values, validate: true + Topic.import columns, valid_values assert_difference "Topic.count", +2 do - Topic.import columns, valid_values, validate: true + Topic.import columns, valid_values end end @@ -294,6 +294,15 @@ end end end + + context "with uniqueness validators included" do + it "should not import duplicate records" do + Topic.import columns, valid_values + assert_no_difference "Topic.count" do + Topic.import columns, valid_values, validate_uniqueness: true + end + end + end end context "without :validation option" do