Class: ActAsImportable::Importer
- Inherits:
-
Object
- Object
- ActAsImportable::Importer
- Defined in:
- lib/act_as_importable/importer.rb
Direct Known Subclasses
Instance Method Summary collapse
- #delete_missing_records ⇒ Object
- #existing_record_ids ⇒ Object
- #existing_record_scope ⇒ Object
- #failed_imports ⇒ Object
- #filter_columns(row) ⇒ Object
- #import_data(data) ⇒ Object
- #import_record(row) ⇒ Object
- #imported_records ⇒ Object
-
#initialize(options = {}) ⇒ Importer
constructor
A new instance of Importer.
- #missing_uid_values(row) ⇒ Object
- #model_class ⇒ Object
- #options ⇒ Object
- #record_ids_to_delete ⇒ Object
- #successful_imports ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Importer
Returns a new instance of Importer.
4 5 6 7 8 |
# File 'lib/act_as_importable/importer.rb', line 4 def initialize( = {}) @default_options = @imported_records = [] @errors = [] end |
Instance Method Details
#delete_missing_records ⇒ Object
34 35 36 |
# File 'lib/act_as_importable/importer.rb', line 34 def delete_missing_records model_class.delete(record_ids_to_delete) end |
#existing_record_ids ⇒ Object
25 26 27 |
# File 'lib/act_as_importable/importer.rb', line 25 def existing_record_ids existing_record_scope.map(&:id) end |
#existing_record_scope ⇒ Object
21 22 23 |
# File 'lib/act_as_importable/importer.rb', line 21 def existing_record_scope [:existing_record_scope] || model_class.all end |
#failed_imports ⇒ Object
68 69 70 71 |
# File 'lib/act_as_importable/importer.rb', line 68 def failed_imports # Note: We don't want to re-validate the objects. imported_records.reject { |r| r.errors.empty? } end |
#filter_columns(row) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/act_as_importable/importer.rb', line 77 def filter_columns(row) except = Array([:except]).map { |i| i.to_s } only = Array([:only]).map { |i| i.to_s } row.reject! { |key, value| except.include? key.to_s } if except.present? row.select! { |key, value| only.include? key.to_s } if only.present? row end |
#import_data(data) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/act_as_importable/importer.rb', line 14 def import_data(data) data.map do |row| import_record(row) end delete_missing_records if [:delete_missing_records] end |
#import_record(row) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/act_as_importable/importer.rb', line 38 def import_record(row) row = prepare_row_for_import(row) record = find_or_create_record(row) record.update_attributes(row) record.save imported_records << record record rescue Exception => e record = model_class.new # Assign the valid attributes (without saving) record.assign_attributes(row.select { |k,v| record.attributes.keys.include? k }) record.errors.add :base, e. record.import_data = row if record.respond_to? :import_data= imported_records << record record end |
#imported_records ⇒ Object
59 60 61 |
# File 'lib/act_as_importable/importer.rb', line 59 def imported_records @imported_records ||= [] end |
#missing_uid_values(row) ⇒ Object
55 56 57 |
# File 'lib/act_as_importable/importer.rb', line 55 def missing_uid_values(row) uid_values(row).select { |k, v| v.blank? } end |
#model_class ⇒ Object
73 74 75 |
# File 'lib/act_as_importable/importer.rb', line 73 def model_class [:model_class] end |
#options ⇒ Object
10 11 12 |
# File 'lib/act_as_importable/importer.rb', line 10 def @default_options end |
#record_ids_to_delete ⇒ Object
29 30 31 32 |
# File 'lib/act_as_importable/importer.rb', line 29 def record_ids_to_delete imported_ids = successful_imports.map(&:id) existing_record_ids.reject { |id| imported_ids.include? id } end |
#successful_imports ⇒ Object
63 64 65 66 |
# File 'lib/act_as_importable/importer.rb', line 63 def successful_imports # Note: We don't want to re-validate the objects. imported_records.select { |r| r.errors.empty? } end |