Class: Spree::ImportRow

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/import_row.rb

Instance Method Summary collapse

Instance Method Details

#attribute_by_schema_field(schema_field, mappings = nil) ⇒ Object



69
70
71
72
73
74
75
76
# File 'app/models/spree/import_row.rb', line 69

def attribute_by_schema_field(schema_field, mappings = nil)
  mappings ||= import.mappings.mapped

  mapping = mappings.find { |m| m.schema_field == schema_field }
  return unless mapping&.mapped?

  data_json[mapping.file_column]
end

#data_jsonObject



50
51
52
53
54
# File 'app/models/spree/import_row.rb', line 50

def data_json
  @data_json ||= JSON.parse(data)
rescue JSON::ParserError
  {}
end

#process!Object



78
79
80
81
82
83
84
85
86
# File 'app/models/spree/import_row.rb', line 78

def process!
  start_processing!
  self.item = import.row_processor_class.new(self).process!
  complete!
rescue StandardError => e
  Rails.error.report(e, handled: true, context: { import_row_id: id }, source: 'spree.core')
  self.validation_errors = e.message
  fail!
end

#publish_import_row_completed_eventObject



88
89
90
# File 'app/models/spree/import_row.rb', line 88

def publish_import_row_completed_event
  publish_event('import_row.completed')
end

#publish_import_row_failed_eventObject



92
93
94
# File 'app/models/spree/import_row.rb', line 92

def publish_import_row_failed_event
  publish_event('import_row.failed')
end

#to_schema_hashObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/spree/import_row.rb', line 56

def to_schema_hash
  @to_schema_hash ||= begin
    mappings = import.mappings.mapped
    schema_fields = import.schema_fields

    attributes = {}
    schema_fields.each do |field|
      attributes[field[:name]] = attribute_by_schema_field(field[:name], mappings)
    end
    attributes
  end
end