Class: ActiveAdminImport::Importer
- Inherits:
-
Object
- Object
- ActiveAdminImport::Importer
- Defined in:
- lib/active_admin_import/importer.rb
Instance Attribute Summary collapse
-
#csv_lines ⇒ Object
readonly
Returns the value of attribute csv_lines.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #cycle(lines) ⇒ Object
- #file ⇒ Object
- #import ⇒ Object
-
#initialize(resource, model, options) ⇒ Importer
constructor
A new instance of Importer.
- #prepare_headers(headers) ⇒ Object
- #store ⇒ Object
Constructor Details
#initialize(resource, model, options) ⇒ Importer
Returns a new instance of Importer.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_admin_import/importer.rb', line 30 def initialize(resource, model, ) @resource = resource @model = model @options = {batch_size: 1000, validate: true}.merge() @headers = model.respond_to?(:csv_headers) ? model.csv_headers : [] @result= {failed: [], imported: 0} if @options.has_key?(:col_sep) || @options.has_key?(:row_sep) ActiveSupport::Deprecation.warn "row_sep and col_sep options are deprecated, use csv_options to override default CSV options" @csv_options = @options.slice(:col_sep, :row_sep) else @csv_options = @options[:csv_options] || {} end #override csv options from model if it respond_to csv_options @csv_options = model. if model.respond_to?(:csv_options) @csv_options.reject! {| key, value | value.blank? } end |
Instance Attribute Details
#csv_lines ⇒ Object (readonly)
Returns the value of attribute csv_lines.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def csv_lines @csv_lines end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def headers @headers end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def @options end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def resource @resource end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def result @result end |
Instance Method Details
#cycle(lines) ⇒ Object
52 53 54 55 |
# File 'lib/active_admin_import/importer.rb', line 52 def cycle(lines) @csv_lines = CSV.parse(lines.join, @csv_options) @result.merge!(self.store) { |key, val1, val2| val1+val2 } end |
#file ⇒ Object
48 49 50 |
# File 'lib/active_admin_import/importer.rb', line 48 def file @model.file end |
#import ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_admin_import/importer.rb', line 57 def import [:before_import].call(self) if [:before_import].is_a?(Proc) lines = [] batch_size = [:batch_size].to_i File.open(file.path) do |f| # capture headers if not exist prepare_headers(headers.any? ? headers : CSV.parse(f.readline, @csv_options).first) f.each_line do |line| next if line.blank? lines << line if lines.size == batch_size || f.eof? cycle lines lines = [] end end end cycle(lines) unless lines.blank? [:after_import].call(self) if [:after_import].is_a?(Proc) result end |
#prepare_headers(headers) ⇒ Object
24 25 26 27 28 |
# File 'lib/active_admin_import/importer.rb', line 24 def prepare_headers(headers) @headers = Hash[headers.zip(headers.map { |el| el.underscore.gsub(/\s+/, '_') })] @headers.merge!([:headers_rewrites]) @headers end |
#store ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/active_admin_import/importer.rb', line 7 def store result = @resource.transaction do [:before_batch_import].call(self) if [:before_batch_import].is_a?(Proc) result = resource.import headers.values, csv_lines, { validate: [:validate], on_duplicate_key_update: [:on_duplicate_key_update], ignore: [:ignore], timestamps: [:timestamps] } [:after_batch_import].call(self) if [:after_batch_import].is_a?(Proc) result end {imported: csv_lines.count - result.failed_instances.count, failed: result.failed_instances} end |