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, :on_duplicate_key_ignore, :ignore, :timestamps, :before_import, :after_import, :before_batch_import, :after_batch_import, :headers_rewrites, :batch_size, :batch_transaction, :csv_options, :result_class ].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 this method when CSV file contains unnecessary 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.
25 26 27 28 29 30 |
# File 'lib/active_admin_import/importer.rb', line 25 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 @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
#batch_replace(header_key, options) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/active_admin_import/importer.rb', line 65 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 this method when CSV file contains unnecessary columns
Example:
ActiveAdmin.register Post
active_admin_import before_batch_import: lambda { |importer|
importer.batch_slice_columns(['name', 'birthday'])
}
end
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/active_admin_import/importer.rb', line 84 def batch_slice_columns(slice_columns) # Only set @use_indexes for the first batch so that @use_indexes are in correct # position for subsequent batches unless defined?(@use_indexes) @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 end # slice CSV values csv_lines.map! do |line| line.values_at(*@use_indexes) end end |
#cycle(lines) ⇒ Object
40 41 42 43 |
# File 'lib/active_admin_import/importer.rb', line 40 def cycle(lines) @csv_lines = CSV.parse(lines.join, **@csv_options) import_result.add(batch_import, lines.count) end |
#file ⇒ Object
36 37 38 |
# File 'lib/active_admin_import/importer.rb', line 36 def file @model.file end |
#header_index(header_key) ⇒ Object
108 109 110 |
# File 'lib/active_admin_import/importer.rb', line 108 def header_index(header_key) headers.values.index(header_key) end |
#import ⇒ Object
45 46 47 48 49 50 |
# File 'lib/active_admin_import/importer.rb', line 45 def import run_callback(:before_import) process_file run_callback(:after_import) import_result end |
#import_options ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/active_admin_import/importer.rb', line 52 def @import_options ||= .slice( :validate, :validate_uniqueness, :on_duplicate_key_update, :on_duplicate_key_ignore, :ignore, :timestamps, :batch_transaction, :batch_size ) end |
#import_result ⇒ Object
32 33 34 |
# File 'lib/active_admin_import/importer.rb', line 32 def import_result @import_result ||= ([:result_class] || ImportResult).new end |
#values_at(header_key) ⇒ Object
104 105 106 |
# File 'lib/active_admin_import/importer.rb', line 104 def values_at(header_key) csv_lines.collect { |line| line[header_index(header_key)] }.uniq end |