Class: ActiveAdminImport::Importer
- Inherits:
-
Object
- Object
- ActiveAdminImport::Importer
- Defined in:
- lib/active_admin_import/importer.rb
Constant Summary collapse
- OPTIONS =
[ :validate, :on_duplicate_key_update, :ignore, :timestamps, :before_import, :after_import, :before_batch_import, :after_batch_import, :headers_rewrites, :batch_size, :batch_transaction, :csv_options ].freeze
Instance Attribute Summary collapse
-
#csv_lines ⇒ Object
Returns the value of attribute csv_lines.
-
#headers ⇒ Object
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
- #batch_replace(header_key, options) ⇒ Object
-
#batch_slice_columns(slice_columns) ⇒ Object
Use it when CSV file contain redundant columns.
- #cycle(lines) ⇒ Object
- #file ⇒ Object
- #header_index(header_key) ⇒ Object
- #import ⇒ Object
- #import_options ⇒ Object
- #import_result ⇒ Object
-
#initialize(resource, model, options) ⇒ Importer
constructor
A new instance of Importer.
- #values_at(header_key) ⇒ Object
Constructor Details
#initialize(resource, model, options) ⇒ Importer
Returns a new instance of Importer.
23 24 25 26 27 28 |
# File 'lib/active_admin_import/importer.rb', line 23 def initialize(resource, model, ) @resource = resource @model = model @headers = model.respond_to?(:csv_headers) ? model.csv_headers : [] () end |
Instance Attribute Details
#csv_lines ⇒ Object
Returns the value of attribute csv_lines.
6 7 8 |
# File 'lib/active_admin_import/importer.rb', line 6 def csv_lines @csv_lines end |
#headers ⇒ Object
Returns the value of attribute headers.
6 7 8 |
# File 'lib/active_admin_import/importer.rb', line 6 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 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
#batch_replace(header_key, options) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/active_admin_import/importer.rb', line 54 def batch_replace(header_key, ) index = header_index(header_key) csv_lines.map! do |line| from = line[index] line[index] = [from] if .key?(from) line end end |
#batch_slice_columns(slice_columns) ⇒ Object
Use it when CSV file contain redundant columns
Example:
ActiveAdmin.register Post
active_admin_import before_batch_import: lambda { |importer|
importer.batch_slice_columns(['name', 'birthday'])
}
end
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_admin_import/importer.rb', line 73 def batch_slice_columns(slice_columns) use_indexes = [] headers.values.each_with_index do |val, index| use_indexes << index if val.in?(slice_columns) end return csv_lines if use_indexes.empty? # slice CSV headers @headers = headers.to_a.values_at(*use_indexes).to_h # slice CSV values csv_lines.map! do |line| line.values_at(*use_indexes) end end |
#cycle(lines) ⇒ Object
38 39 40 41 |
# File 'lib/active_admin_import/importer.rb', line 38 def cycle(lines) @csv_lines = CSV.parse(lines.join, ) import_result.add(batch_import, lines.count) end |
#file ⇒ Object
34 35 36 |
# File 'lib/active_admin_import/importer.rb', line 34 def file @model.file end |
#header_index(header_key) ⇒ Object
91 92 93 |
# File 'lib/active_admin_import/importer.rb', line 91 def header_index(header_key) headers.values.index(header_key) end |
#import ⇒ Object
43 44 45 46 47 48 |
# File 'lib/active_admin_import/importer.rb', line 43 def import run_callback(:before_import) process_file run_callback(:after_import) import_result end |
#import_options ⇒ Object
50 51 52 |
# File 'lib/active_admin_import/importer.rb', line 50 def ||= .slice(:validate, :on_duplicate_key_update, :ignore, :timestamps, :batch_transaction) end |
#import_result ⇒ Object
30 31 32 |
# File 'lib/active_admin_import/importer.rb', line 30 def import_result @import_result ||= ImportResult.new end |
#values_at(header_key) ⇒ Object
87 88 89 |
# File 'lib/active_admin_import/importer.rb', line 87 def values_at(header_key) csv_lines.collect { |line| line[header_index(header_key)] }.uniq end |