diff --git a/.travis.yml b/.travis.yml index a451fb1..ee2d3bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: ruby rvm: - - 2.3.0 + - 2.5.3 addons: code_climate: repo_token: bcecbf1b229a2ddd666a2c3830f26a0113fd56ae1586d30d2d3fb1af837bf0e4 diff --git a/lib/csv_importer/runner.rb b/lib/csv_importer/runner.rb index e4a473f..e786bd7 100644 --- a/lib/csv_importer/runner.rb +++ b/lib/csv_importer/runner.rb @@ -40,6 +40,10 @@ def call def abort_when_invalid? when_invalid == :abort end + + def without_changes?(row) + row.model.persisted? && !row.model.changed? + end def persist_rows! transaction do @@ -52,7 +56,7 @@ def persist_rows! tags << :create end - if row.skip? + if row.skip? || without_changes?(row) tags << :skip else if row.model.save diff --git a/spec/csv_importer_spec.rb b/spec/csv_importer_spec.rb index d21f84a..ff4bf8a 100644 --- a/spec/csv_importer_spec.rb +++ b/spec/csv_importer_spec.rb @@ -5,8 +5,9 @@ describe CSVImporter do # Mimics an active record model class User - include Virtus.model - include ActiveModel::Model + include ActiveModel::Attributes + include ActiveModel::Validations + include ActiveModel::Dirty attribute :id attribute :email @@ -14,7 +15,7 @@ class User attribute :l_name attribute :confirmed_at attribute :created_by_user_id - attribute :custom_fields, Hash + attribute :custom_fields, default: -> { Hash.new } validates_presence_of :email validates_format_of :email, with: /[^@]+@[^@]/ # contains one @ symbol @@ -25,30 +26,34 @@ def self.transaction end def persisted? - !!id + !!self.id end def save return false unless valid? unless persisted? - @id = rand(100) + self.id = rand(100) self.class.store << self end + changes_applied + true end def self.find_by(attributes) - store.find { |u| attributes.all? { |k, v| u.attributes[k] == v } } + store.find { |u| attributes.all? { |k, v| u.public_send(k) == v } } end def self.reset_store! @store = Set.new - User.new( - email: "mark@example.com", f_name: "mark", l_name: "lee", confirmed_at: Time.new(2012) - ).save + user = User.new + { email: "mark@example.com", f_name: "mark", l_name: "lee", confirmed_at: Time.new(2012) }.each do |k,v| + user.public_send("#{k}=",v) + end + user.save @store end @@ -633,8 +638,20 @@ class ImportUserCSVByFirstName end import.run! + expect(import.report.valid_rows.size).to eq(1) expect(import.report.message).to eq "Import completed: 1 created, 1 update skipped" end + + it "if no changes detected" do + csv_content = "email,confirmed,first_name,last_name +mark@example.com,true,mark,lee" + import = ImportUserCSV.new(content: csv_content) + + import.run! + + expect(import.report.valid_rows.size).to eq(0) + expect(import.report.message).to eq "Import completed: 1 update skipped" + end it "doesn't call skip! twice" do csv_content = "email,confirmed,first_name,last_name