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 ].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.
24 25 26 27 28 29 |
# File 'lib/active_admin_import/importer.rb', line 24 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
64 65 66 67 68 69 70 71 |
# File 'lib/active_admin_import/importer.rb', line 64 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_admin_import/importer.rb', line 83 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
39 40 41 42 |
# File 'lib/active_admin_import/importer.rb', line 39 def cycle(lines) @csv_lines = CSV.parse(lines.join, @csv_options) import_result.add(batch_import, lines.count) end |
#file ⇒ Object
35 36 37 |
# File 'lib/active_admin_import/importer.rb', line 35 def file @model.file end |
#header_index(header_key) ⇒ Object
107 108 109 |
# File 'lib/active_admin_import/importer.rb', line 107 def header_index(header_key) headers.values.index(header_key) end |
#import ⇒ Object
44 45 46 47 48 49 |
# File 'lib/active_admin_import/importer.rb', line 44 def import run_callback(:before_import) process_file run_callback(:after_import) import_result end |
#import_options ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_admin_import/importer.rb', line 51 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
31 32 33 |
# File 'lib/active_admin_import/importer.rb', line 31 def import_result @import_result ||= ImportResult.new end |
#values_at(header_key) ⇒ Object
103 104 105 |
# File 'lib/active_admin_import/importer.rb', line 103 def values_at(header_key) csv_lines.collect { |line| line[header_index(header_key)] }.uniq end |