Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: ruby
rvm:
- 2.3.0
- 2.5.3
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed by AM

addons:
code_climate:
repo_token: bcecbf1b229a2ddd666a2c3830f26a0113fd56ae1586d30d2d3fb1af837bf0e4
Expand Down
6 changes: 5 additions & 1 deletion lib/csv_importer/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 26 additions & 9 deletions spec/csv_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
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
Copy link
Author

@joel joel Dec 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it was hard to integrate dirty with virtus, switched to full AM


attribute :id
attribute :email
attribute :f_name
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
Expand All @@ -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: "[email protected]", f_name: "mark", l_name: "lee", confirmed_at: Time.new(2012)
).save
user = User.new
{ email: "[email protected]", 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
Expand Down Expand Up @@ -633,8 +638,20 @@ class ImportUserCSVByFirstName
end

import.run!
expect(import.report.valid_rows.size).to eq(1)
Copy link
Author

@joel joel Dec 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really confusing, should have 2 valid rows. We defined a rule to skip the import or not, that doesn't make input values invalid, just make this import not suitable to go in. I mean no need to find out what is wrong with the CSV.

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
[email protected],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
Expand Down