Class: Spree::OptionType

Inherits:
Object
  • Object
show all
Includes:
HasCustomFields, LabelTranslatable, Metadata, ParameterizableName, SingleStoreResource
Defined in:
app/models/spree/option_type.rb

Constant Summary collapse

KINDS =
%w[dropdown color_swatch buttons].freeze
TRANSLATABLE_FIELDS =
Spree::LabelTranslatable::TRANSLATABLE_FIELDS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LabelTranslatable

#presentation, #presentation=

Methods included from TranslatableResource

#get_field_with_locale, #translatable_store, #upsert_translations

Methods included from Metadata

#private_metadata, #private_metadata=

Class Method Details

.translatable_children ⇒ Object

Option values are translated alongside their type — the translations endpoint nests their matrices so the editor fetches both in one read.



22
23
24
# File 'app/models/spree/option_type.rb', line 22

def self.translatable_children
  :option_values
end

.translation_write_permission ⇒ Object

Option types are managed under the catalog, so the products permission guards them — see Admin::OptionTypesController.



16
17
18
# File 'app/models/spree/option_type.rb', line 16

def self.translation_write_permission
  'write_products'
end

.unknown_options(store, pairs) ⇒ Array<Hash>

The option names and values a write asks for that the store does not offer, matched as Variant#set_option_value matches them. A seller builds variants from these and never adds to them.

Parameters:

  • store (Spree::Store)
  • pairs (Array<Array(String, String)>) —

    option type names and value names

Returns:

  • (Array<Hash>) —

    one { name:, value:, option_type: } per unknown pair; option_type is nil when the type itself is unknown



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/spree/option_type.rb', line 34

def self.unknown_options(store, pairs)
  pairs = pairs.map { |name, value| [name.to_s, value.to_s] }.uniq { |name, value| [name.parameterize, value.parameterize] }
  return [] if pairs.empty?

  option_types = for_store(store).where(name: pairs.map { |name, _| name.parameterize }.uniq).index_by(&:name)
  # Only the values named, not every value of each type: Size can carry hundreds.
  known_values = Spree::OptionValue.where(option_type_id: option_types.values.map(&:id),
                                          name: pairs.map { |_, value| value.parameterize }.uniq).
                 pluck(:option_type_id, :name).to_set

  pairs.filter_map do |name, value|
    option_type = option_types[name.parameterize]
    next if option_type && known_values.include?([option_type.id, value.parameterize])

    { name: name, value: value, option_type: option_type }
  end
end

Instance Method Details

#color_swatch? ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/spree/option_type.rb', line 111

def color_swatch?
  kind == 'color_swatch'
end

#option_values=(option_values_params) ⇒ void

This method returns an undefined value.

Syncs option values from an array of hashes by mutating the in-memory option_values association — built/assigned children get persisted by autosave: true when the parent saves, and absent IDs get destroyed via mark_for_destruction. The single transaction is owned by the parent's save, so validation failures surface as errors and the whole thing rolls back together.

Falls back to ActiveRecord's collection writer when given OptionValue records (e.g. from accepts_nested_attributes_for used by the legacy admin).

Parameters:

  • option_values_params (Array<Hash>) —

    array of option value attribute hashes



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/spree/option_type.rb', line 127

def option_values=(option_values_params)
  return super if option_values_params.blank? || option_values_params.first.is_a?(Spree::OptionValue)

  # Load the association into the in-memory collection so subsequent
  # `option_values.build` / `mark_for_destruction` mutations stay on the
  # same instances `autosave` will traverse at parent-save time.
  existing_by_id = option_values.to_a.index_by(&:id)
  retained_ids = []

  option_values_params.each do |value_data|
    data = value_data.to_h.with_indifferent_access
    value_id = data.delete(:id)

    record = if value_id.present?
               existing_by_id[Spree::PrefixedId.decode_prefixed_id(value_id) || value_id] ||
                 raise(ActiveRecord::RecordNotFound.new("Couldn't find Spree::OptionValue with param=#{value_id}", 'Spree::OptionValue'))
             else
               option_values.build
             end
    record.assign_attributes(data)
    retained_ids << record.id if record.persisted?
  end

  existing_by_id.each_value do |existing|
    existing.mark_for_destruction unless retained_ids.include?(existing.id)
  end
end