Skip to content

Commit d9ade95

Browse files
committed
rm sql_transaction config
1 parent 1a8cd10 commit d9ade95

File tree

3 files changed

+25
-54
lines changed

3 files changed

+25
-54
lines changed

lib/csv_importer/config.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ class Config
99
attribute :when_invalid, Symbol, default: proc { :skip }
1010
attribute :after_build_blocks, Array[Proc], default: []
1111
attribute :after_save_blocks, Array[Proc], default: []
12-
attribute :sql_transaction, Symbol, default: proc { :all_rows }
1312

1413
def initialize_copy(orig)
1514
super
1615
self.column_definitions = orig.column_definitions.dup
1716
self.identifiers = orig.identifiers.dup
1817
self.after_save_blocks = orig.after_save_blocks.dup
1918
self.after_build_blocks = orig.after_build_blocks.dup
20-
self.sql_transaction = orig.sql_transaction.dup
2119
end
2220

2321
def after_build(block)

lib/csv_importer/dsl.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,5 @@ def after_build(&block)
2727
def after_save(&block)
2828
config.after_save(block)
2929
end
30-
31-
def sql_transaction(behavior)
32-
config.sql_transaction = behavior
33-
end
3430
end
3531
end

lib/csv_importer/runner.rb

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def self.call(*args)
1212
attribute :rows, Array[Row]
1313
attribute :when_invalid, Symbol
1414
attribute :after_save_blocks, Array[Proc], default: []
15-
attribute :sql_transaction, Symbol
1615

1716
attribute :report, Report, default: proc { Report.new }
1817

@@ -42,48 +41,38 @@ def abort_when_invalid?
4241
when_invalid == :abort
4342
end
4443

45-
def sql_transaction_all_rows?
46-
sql_transaction == :all_rows
47-
end
48-
49-
def sql_transaction_one_row?
50-
sql_transaction == :each_row
51-
end
52-
5344
def persist_rows!
54-
full_transaction do
45+
transaction do
5546
rows.each do |row|
56-
transaction do
57-
tags = []
47+
tags = []
5848

59-
if row.model.persisted?
60-
tags << :update
61-
else
62-
tags << :create
63-
end
49+
if row.model.persisted?
50+
tags << :update
51+
else
52+
tags << :create
53+
end
6454

65-
if row.skip?
66-
tags << :skip
67-
elsif row.errors.size > 0
68-
tags << :failure
55+
if row.skip?
56+
tags << :skip
57+
elsif row.errors.size > 0
58+
tags << :failure
59+
else
60+
if row.model.save
61+
tags << :success
6962
else
70-
if row.model.save
71-
tags << :success
72-
else
73-
tags << :failure
74-
end
63+
tags << :failure
7564
end
65+
end
7666

77-
add_to_report(row, tags)
67+
add_to_report(row, tags)
7868

79-
after_save_blocks.each do |block|
80-
case block.arity
81-
when 0 then block.call
82-
when 1 then block.call(row.model)
83-
when 2 then block.call(row.model, row.csv_attributes)
84-
else
85-
raise ArgumentError, "after_save block of arity #{ block.arity } is not supported"
86-
end
69+
after_save_blocks.each do |block|
70+
case block.arity
71+
when 0 then block.call
72+
when 1 then block.call(row.model)
73+
when 2 then block.call(row.model, row.csv_attributes)
74+
else
75+
raise ArgumentError, "after_save block of arity #{ block.arity } is not supported"
8776
end
8877
end
8978
end
@@ -113,20 +102,8 @@ def add_to_report(row, tags)
113102
raise ImportAborted if abort_when_invalid? && tags[1] == :failure
114103
end
115104

116-
def full_transaction(&block)
117-
if sql_transaction_all_rows?
118-
rows.first.model.class.transaction(&block)
119-
else
120-
block.call
121-
end
122-
end
123-
124105
def transaction(&block)
125-
if sql_transaction_one_row?
126-
rows.first.model.class.transaction(&block)
127-
else
128-
block.call
129-
end
106+
rows.first.model.class.transaction(&block)
130107
end
131108
end
132109
end

0 commit comments

Comments
 (0)