Class: Topographer::Importer::Strategy::CreateOrUpdateRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/topographer/importer/strategy/create_or_update_record.rb

Instance Attribute Summary

Attributes inherited from Base

#dry_run, #mapper

Instance Method Summary collapse

Methods inherited from Base

#initialize, #should_persist_import?

Constructor Details

This class inherits a constructor from Topographer::Importer::Strategy::Base

Instance Method Details

#failure_messageObject



33
34
35
# File 'lib/topographer/importer/strategy/create_or_update_record.rb', line 33

def failure_message
  @failure_message
end

#import_record(source_data) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/topographer/importer/strategy/create_or_update_record.rb', line 3

def import_record (source_data)
  mapping_result = mapper.map_input(source_data)

  search_params = mapping_result.data.slice(*mapper.key_fields)
  model_instances = mapper.model_class.where(search_params)

  if model_instances.any?
    model_instance = model_instances.first
  else
    model_instance = mapper.model_class.new(search_params)
  end

  generate_messages(model_instance, search_params)

  model_instance.attributes = mapping_result.data
  model_instance.valid?

  model_errors = model_instance.errors.full_messages
  status = get_import_status(mapping_result, model_errors)

  model_instance.save if should_persist_import?(status)

  status
end

#success_messageObject



29
30
31
# File 'lib/topographer/importer/strategy/create_or_update_record.rb', line 29

def success_message
  @success_message
end