Class: ActiveAdminImporter::Import
- Inherits:
-
Object
- Object
- ActiveAdminImporter::Import
- Defined in:
- lib/active_admin_importer/import.rb
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#csv_file ⇒ Object
readonly
Returns the value of attribute csv_file.
-
#current_row ⇒ Object
readonly
Returns the value of attribute current_row.
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #failed_rows ⇒ Object
- #headers ⇒ Object
-
#initialize(csv_file, definition:, controller:) ⇒ Import
constructor
A new instance of Import.
- #md5 ⇒ Object
- #result ⇒ Object
- #run ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(csv_file, definition:, controller:) ⇒ Import
Returns a new instance of Import.
5 6 7 8 9 10 11 12 |
# File 'lib/active_admin_importer/import.rb', line 5 def initialize(csv_file, definition:, controller:) @csv_file = ::ActiveAdminImporter::CsvFile.new(csv_file) @controller = controller @definition = definition @model = definition[:model] @required_headers = definition[:required_headers] @current_row = 0 end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
3 4 5 |
# File 'lib/active_admin_importer/import.rb', line 3 def controller @controller end |
#csv_file ⇒ Object (readonly)
Returns the value of attribute csv_file.
3 4 5 |
# File 'lib/active_admin_importer/import.rb', line 3 def csv_file @csv_file end |
#current_row ⇒ Object (readonly)
Returns the value of attribute current_row.
3 4 5 |
# File 'lib/active_admin_importer/import.rb', line 3 def current_row @current_row end |
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
3 4 5 |
# File 'lib/active_admin_importer/import.rb', line 3 def definition @definition end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/active_admin_importer/import.rb', line 3 def model @model end |
Instance Method Details
#failed_rows ⇒ Object
14 15 16 |
# File 'lib/active_admin_importer/import.rb', line 14 def failed_rows @failed_rows ||= [] end |
#headers ⇒ Object
18 19 20 |
# File 'lib/active_admin_importer/import.rb', line 18 def headers @headers ||= @csv_file.headers end |
#md5 ⇒ Object
22 23 24 |
# File 'lib/active_admin_importer/import.rb', line 22 def md5 @md5 ||= @csv_file.md5 end |
#result ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/active_admin_importer/import.rb', line 43 def result @result ||= begin _result = [] _result << "Bad column headers, expected #{@required_headers} got #{@csv_file.headers}" unless self.valid? _result << "Failed to import rows: #{failed_rows.join(',')}" if failed_rows.any? _result << "#{current_row - failed_rows.length} / #{current_row} imported successfully" if current_row > 0 _result.join("\n") end end |
#run ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_admin_importer/import.rb', line 26 def run run_before_callback if run_before_callback? log_info("STARTING IMPORT") ::CSV.parse(@csv_file, :headers => true, :header_converters => :symbol) do |row| begin process_row(row) rescue => e record_failure(row, e) end end log_error("FAILED TO PARSE ROWS #{failed_rows}") if failed_rows.any? log_info("FINISHED IMPORT") run_after_callback if run_after_callback? end |
#valid? ⇒ Boolean
53 54 55 |
# File 'lib/active_admin_importer/import.rb', line 53 def valid? @required_headers.all?{ |header| self.headers.include?(header) } end |