Skip to content

Commit 0553cab

Browse files
committed
refactoring
1 parent bd6c831 commit 0553cab

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

lib/active_admin_import/dsl.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,20 @@ def active_admin_import(options = {}, &block)
4545
options.assert_valid_keys(*Options::VALID_OPTIONS)
4646

4747
options = Options.options_for(config, options)
48-
params_key = ActiveModel::Naming.param_key(options[:template_object] || ActiveAdminImport::Model.new)
48+
params_key = ActiveModel::Naming.param_key(options[:template_object])
4949

5050
collection_action :import, method: :get do
51-
5251
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
53-
54-
@active_admin_import_model = options[:template_object] || ActiveAdminImport::Model.new
52+
@active_admin_import_model = options[:template_object]
5553
render template: options[:template]
5654
end
5755

58-
5956
action_item :import, only: :index do
6057
if authorized?(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
6158
link_to(I18n.t('active_admin_import.import_model', plural_model: options[:plural_resource_label]), action: :import)
6259
end
6360
end
6461

65-
6662
collection_action :do_import, method: :post do
6763
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
6864

lib/active_admin_import/import_result.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize
99

1010
def add(result, qty)
1111
@failed += result.failed_instances
12-
@total+=qty
12+
@total+=qty
1313
end
1414

1515
def imported_qty

lib/active_admin_import/importer.rb

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
module ActiveAdminImport
33
class Importer
44

5-
65
attr_reader :resource, :options, :result, :headers, :csv_lines, :model
76

8-
97
OPTIONS = [
108
:validate,
119
:on_duplicate_key_update,
@@ -20,7 +18,6 @@ class Importer
2018
:csv_options
2119
].freeze
2220

23-
2421
def initialize(resource, model, options)
2522
@resource = resource
2623
@model = model
@@ -43,32 +40,35 @@ def cycle(lines)
4340

4441
def import
4542
run_callback(:before_import)
46-
lines = []
47-
batch_size = options[:batch_size].to_i
43+
process_file
44+
run_callback(:after_import)
45+
import_result
46+
end
47+
48+
def import_options
49+
@import_options ||= options.slice(:validate, :on_duplicate_key_update, :ignore, :timestamps)
50+
end
51+
52+
protected
53+
54+
def process_file
55+
lines, batch_size = [], options[:batch_size].to_i
4856
File.open(file.path) do |f|
4957
# capture headers if not exist
50-
prepare_headers(headers.any? ? headers : CSV.parse(f.readline, @csv_options).first)
58+
prepare_headers{ CSV.parse(f.readline, @csv_options).first }
5159
f.each_line do |line|
52-
next if line.blank?
53-
lines << line
60+
lines << line if line.present?
5461
if lines.size == batch_size || f.eof?
5562
cycle(lines)
5663
lines = []
5764
end
5865
end
5966
end
6067
cycle(lines) unless lines.blank?
61-
run_callback(:after_import)
62-
import_result
6368
end
6469

65-
def import_options
66-
@import_options ||= options.slice(:validate, :on_duplicate_key_update, :ignore, :timestamps)
67-
end
68-
69-
protected
70-
71-
def prepare_headers(headers)
70+
def prepare_headers
71+
headers = self.headers.present? ? self.headers : yield
7272
@headers = Hash[headers.zip(headers.map { |el| el.underscore.gsub(/\s+/, '_') })].with_indifferent_access
7373
@headers.merge!(options[:headers_rewrites])
7474
@headers
@@ -87,9 +87,6 @@ def batch_import
8787
end
8888
end
8989

90-
91-
private
92-
9390
def assign_options(options)
9491
@options = {batch_size: 1000, validate: true}.merge(options.slice(*OPTIONS))
9592
detect_csv_options

lib/active_admin_import/model.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Model
1616
validate :correct_content_type, if: proc { |me| me.file.present? }
1717
validate :file_contents_present, if: proc { |me| me.file.present? }
1818

19-
2019
before_validation :uncompress_file, if: proc { |me| me.archive? && me.allow_archive? }
2120
before_validation :encode_file, if: proc { |me| me.force_encoding? && me.file.present? }
2221

@@ -45,19 +44,15 @@ def default_attributes
4544
end
4645

4746
def allow_archive?
48-
!!@attributes[:allow_archive]
47+
!!attributes[:allow_archive]
4948
end
5049

5150
def new_record?
5251
!!@new_record
5352
end
5453

5554
def force_encoding?
56-
!!@attributes[:force_encoding]
57-
end
58-
59-
def to_hash
60-
@attributes
55+
!!attributes[:force_encoding]
6156
end
6257

6358
def persisted?
@@ -68,6 +63,8 @@ def archive?
6863
file_type == 'application/zip'
6964
end
7065

66+
alias :to_hash :attributes
67+
7168
protected
7269

7370
def file_path

0 commit comments

Comments
 (0)