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



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

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



52
53
54
55
56
# File 'app/models/spree/import_row.rb', line 52

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

#process!Object



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

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



90
91
92
# File 'app/models/spree/import_row.rb', line 90

def publish_import_row_completed_event
  publish_event('import_row.completed')
end

#publish_import_row_failed_eventObject



94
95
96
# File 'app/models/spree/import_row.rb', line 94

def publish_import_row_failed_event
  publish_event('import_row.failed')
end

#to_schema_hashObject



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

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