Skip to content

Commit c165fb4

Browse files
committed
Auto Skip Update Without Changes
1 parent f520d6a commit c165fb4

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

lib/csv_importer/runner.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def call
4040
def abort_when_invalid?
4141
when_invalid == :abort
4242
end
43+
44+
def without_changes?(row)
45+
row.model.persisted? && !row.model.changed?
46+
end
4347

4448
def persist_rows!
4549
transaction do
@@ -52,7 +56,7 @@ def persist_rows!
5256
tags << :create
5357
end
5458

55-
if row.skip?
59+
if row.skip? || without_changes?(row)
5660
tags << :skip
5761
else
5862
if row.model.save

spec/csv_importer_spec.rb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
describe CSVImporter do
66
# Mimics an active record model
77
class User
8-
include Virtus.model
9-
include ActiveModel::Model
8+
include ActiveModel::Attributes
9+
include ActiveModel::Validations
10+
include ActiveModel::Dirty
1011

1112
attribute :id
1213
attribute :email
1314
attribute :f_name
1415
attribute :l_name
1516
attribute :confirmed_at
1617
attribute :created_by_user_id
17-
attribute :custom_fields, Hash
18+
attribute :custom_fields, default: -> { Hash.new }
1819

1920
validates_presence_of :email
2021
validates_format_of :email, with: /[^@]+@[^@]/ # contains one @ symbol
@@ -25,30 +26,34 @@ def self.transaction
2526
end
2627

2728
def persisted?
28-
!!id
29+
!!self.id
2930
end
3031

3132
def save
3233
return false unless valid?
3334

3435
unless persisted?
35-
@id = rand(100)
36+
self.id = rand(100)
3637
self.class.store << self
3738
end
3839

40+
changes_applied
41+
3942
true
4043
end
4144

4245
def self.find_by(attributes)
43-
store.find { |u| attributes.all? { |k, v| u.attributes[k] == v } }
46+
store.find { |u| attributes.all? { |k, v| u.public_send(k) == v } }
4447
end
4548

4649
def self.reset_store!
4750
@store = Set.new
4851

49-
User.new(
50-
email: "mark@example.com", f_name: "mark", l_name: "lee", confirmed_at: Time.new(2012)
51-
).save
52+
user = User.new
53+
{ email: "mark@example.com", f_name: "mark", l_name: "lee", confirmed_at: Time.new(2012) }.each do |k,v|
54+
user.public_send("#{k}=",v)
55+
end
56+
user.save
5257

5358
@store
5459
end
@@ -633,8 +638,20 @@ class ImportUserCSVByFirstName
633638
end
634639

635640
import.run!
641+
expect(import.report.valid_rows.size).to eq(1)
636642
expect(import.report.message).to eq "Import completed: 1 created, 1 update skipped"
637643
end
644+
645+
it "if no changes detected" do
646+
csv_content = "email,confirmed,first_name,last_name
647+
mark@example.com,true,mark,lee"
648+
import = ImportUserCSV.new(content: csv_content)
649+
650+
import.run!
651+
652+
expect(import.report.valid_rows.size).to eq(0)
653+
expect(import.report.message).to eq "Import completed: 1 update skipped"
654+
end
638655

639656
it "doesn't call skip! twice" do
640657
csv_content = "email,confirmed,first_name,last_name

0 commit comments

Comments
 (0)