Module: ImportableAttachments::Importers::Importer
- Defined in:
- lib/importable_attachments/importers/importer.rb
Instance Attribute Summary collapse
-
#attachment_as_ruby ⇒ Object
stores the parsed-file for later processing.
-
#converted_headers ⇒ Object
ImportInto suitable attributes translated from a ImportInto::RECORD_HEADERS inversion, based on RECORD_HEADERS.
-
#destructive_import ⇒ Object
Returns the value of attribute destructive_import.
-
#validate_headers ⇒ Object
Returns the value of attribute validate_headers.
-
#validate_on_import ⇒ Object
Returns the value of attribute validate_on_import.
Instance Method Summary collapse
-
#attachment=(params) ⇒ Object
:call-seq: attachment= params.
-
#bootstrap ⇒ Object
:call-seq: bootstrap.
-
#import_attachment ⇒ Object
: call-seq: import_attachment.
-
#import_rows(*params) ⇒ Object
:call-seq: import_rows *params.
- #initialize(attributes = nil, options = {}) ⇒ Object
Instance Attribute Details
#attachment_as_ruby ⇒ Object
stores the parsed-file for later processing
13 14 15 |
# File 'lib/importable_attachments/importers/importer.rb', line 13 def @attachment_as_ruby end |
#converted_headers ⇒ Object
ImportInto suitable attributes translated from a ImportInto::RECORD_HEADERS inversion, based on RECORD_HEADERS
10 11 12 |
# File 'lib/importable_attachments/importers/importer.rb', line 10 def converted_headers @converted_headers end |
#destructive_import ⇒ Object
Returns the value of attribute destructive_import.
6 7 8 |
# File 'lib/importable_attachments/importers/importer.rb', line 6 def destructive_import @destructive_import end |
#validate_headers ⇒ Object
Returns the value of attribute validate_headers.
6 7 8 |
# File 'lib/importable_attachments/importers/importer.rb', line 6 def validate_headers @validate_headers end |
#validate_on_import ⇒ Object
Returns the value of attribute validate_on_import.
6 7 8 |
# File 'lib/importable_attachments/importers/importer.rb', line 6 def validate_on_import @validate_on_import end |
Instance Method Details
#attachment=(params) ⇒ Object
:call-seq: attachment= params
imports an attachment upon assignment if the record is persisted (if not, after_create hook will import)
43 44 45 46 |
# File 'lib/importable_attachments/importers/importer.rb', line 43 def (params) super params if persisted? && .try(:valid?) end |
#bootstrap ⇒ Object
:call-seq: bootstrap
:validate_headers - ensures :spreadsheet_columns exist within file :validate_on_import - validates :import_into records upon import (much slower) :timestamp_import - sets timestamps of :import_into records upon import (mildly slower) :destructive_import - makes :import_into reflect most recent file contents (slow)
28 29 30 31 32 33 34 35 |
# File 'lib/importable_attachments/importers/importer.rb', line 28 def bootstrap @import_rows_to_class = association_symbol_for_rows.to_s.classify.constantize @validate_headers = true @validate_on_import = ::Configuration.for('attachments').validate_on_import @destructive_import = true @timestamp_import = true @converted_headers = set_converted_headers end |
#import_attachment ⇒ Object
: call-seq: import_attachment
imports an attachment of a given mime-type (data-stream to ruby), calls import_rows with a ruby data-store
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/importable_attachments/importers/importer.rb', line 54 def return unless .present? return unless read_spreadsheet return if validate_headers && !importable_class_headers_ok? transaction do send(association_symbol_for_rows).destroy_all if destructive_import #send import_method, Hash[importable_columns.zip(importable_columns)].symbolize_keys! raise ActiveRecord::Rollback unless import_rows Hash[importable_columns.zip(importable_columns)].symbolize_keys! end end |
#import_rows(*params) ⇒ Object
:call-seq: import_rows *params
imports a CSV file into @import_rows_to_class
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/importable_attachments/importers/importer.rb', line 70 def import_rows(*params) sanitize_data! importer_opts = {} importer_opts.merge! timestamps: true # adds data to converted_headers and spreadsheet importer_opts.merge! validate: validate_on_import # .dup else .import modifies converted_headers and spreadsheet if respond_to? :sanitize_data_callback headers, sheet = sanitize_data_callback(@converted_headers, spreadsheet) else headers, sheet = @converted_headers.dup, spreadsheet.dup end results = @import_rows_to_class.import headers, sheet, importer_opts reload if persisted? if results && !results.try(:failed_instances).try(:empty?) opts = {} opts.merge! import_errors_valid: false fail_msg = "failed to import #{results.failed_instances.count} record(s)" logger.warn "#{@import_rows_to_class.to_s} #{fail_msg}" @row_errors = results.failed_instances.map {|failed_row| "#{failed_row.errors.}: #{failed_row.inspect}"} return nil else @row_errors = [] return results end end |
#initialize(attributes = nil, options = {}) ⇒ Object
15 16 17 18 |
# File 'lib/importable_attachments/importers/importer.rb', line 15 def initialize(attributes = nil, = {}) bootstrap super(attributes, ) end |