Class: AbstractImporter::Strategies::ReplaceStrategy
- Inherits:
-
DefaultStrategy
- Object
- Base
- DefaultStrategy
- AbstractImporter::Strategies::ReplaceStrategy
- Defined in:
- lib/abstract_importer/strategies/replace_strategy.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #create_or_update_record(hash) ⇒ Object
- #process_record(hash) ⇒ Object
- #update_record(hash) ⇒ Object
Methods inherited from DefaultStrategy
#build_record, #clean_record, #create_record
Methods inherited from Base
#already_imported?, #flush, #initialize, #prepare_attributes
Constructor Details
This class inherits a constructor from AbstractImporter::Strategies::Base
Instance Method Details
#create_or_update_record(hash) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/abstract_importer/strategies/replace_strategy.rb', line 28 def create_or_update_record(hash) if already_imported?(hash) update_record(hash) else create_record(hash) end end |
#process_record(hash) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/abstract_importer/strategies/replace_strategy.rb', line 8 def process_record(hash) summary.total += 1 remap_foreign_keys!(hash) if redundant_record?(hash) summary.redundant += 1 return end if create_or_update_record(hash) summary.created += 1 else summary.invalid += 1 end rescue ::AbstractImporter::Skip summary.skipped += 1 end |
#update_record(hash) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/abstract_importer/strategies/replace_strategy.rb', line 37 def update_record(hash) hash = invoke_callback(:before_build, hash) || hash record = scope.find_by(legacy_id: hash.delete(:id)) record.attributes = hash return true if dry_run? invoke_callback(:before_update, record) invoke_callback(:before_save, record) # rescue_callback has one shot to fix things invoke_callback(:rescue, record) unless record.valid? if record.valid? && record.save invoke_callback(:after_update, hash, record) invoke_callback(:after_save, hash, record) reporter.record_created(record) true else reporter.record_failed(record, hash) false end end |