22module 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
0 commit comments