Class: Spree::Imports::RowProcessors::ProductTranslation

Inherits:
Base
  • Object
show all
Defined in:
app/services/spree/imports/row_processors/product_translation.rb

Constant Summary collapse

TRANSLATABLE_FIELDS =
%w[name description meta_title meta_description].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #import, #row

Instance Method Summary collapse

Constructor Details

#initialize(row) ⇒ ProductTranslation

Returns a new instance of ProductTranslation.



7
8
9
10
# File 'app/services/spree/imports/row_processors/product_translation.rb', line 7

def initialize(row, **)
  super
  @store = row.store
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



12
13
14
# File 'app/services/spree/imports/row_processors/product_translation.rb', line 12

def store
  @store
end

Instance Method Details

#process!Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/spree/imports/row_processors/product_translation.rb', line 14

def process!
  locale = attributes['locale'].to_s.strip
  raise ArgumentError, 'Locale is required' if locale.blank?

  slug = attributes['slug'].to_s.strip
  raise ArgumentError, 'Slug is required' if slug.blank?

  product = product_scope.find_by!(slug: slug)

  translation_attrs = TRANSLATABLE_FIELDS.each_with_object({}) do |field, hash|
    value = attributes[field]
    hash[field.to_sym] = value.to_s.strip if value.present?
  end

  return product if translation_attrs.empty?

  Mobility.with_locale(locale) do
    product.update!(translation_attrs)
  end

  product
end