Class: Spree::OptionType

Inherits:
Object
  • Object
show all
Includes:
Metadata, Metafields, ParameterizableName, PresentationTranslatable, UniqueName
Defined in:
app/models/spree/option_type.rb

Constant Summary collapse

COLOR_NAMES =
%w[color colour].freeze
KINDS =
%w[dropdown color_swatch buttons].freeze
TRANSLATABLE_FIELDS =
Spree::PresentationTranslatable::TRANSLATABLE_FIELDS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PresentationTranslatable

#label, #label=

Methods included from TranslatableResource

#get_field_with_locale, #translatable_store, #upsert_translations

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

Class Method Details

.colorObject



81
82
83
# File 'app/models/spree/option_type.rb', line 81

def self.color
  colors.first
end

.translatable_childrenObject

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



19
20
21
# File 'app/models/spree/option_type.rb', line 19

def self.translatable_children
  :option_values
end

Instance Method Details

#color?Boolean

Returns:



89
90
91
92
93
94
# File 'app/models/spree/option_type.rb', line 89

def color?
  Spree::Deprecation.warn(
    'Spree::OptionType#color? is deprecated. Use #color_swatch? instead. Will be removed in Spree 6.0.'
  )
  color_swatch?
end

#color_swatch?Boolean

Returns:



85
86
87
# File 'app/models/spree/option_type.rb', line 85

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/spree/option_type.rb', line 108

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