Sha256: 966720b9801510b91d3e386f978b4434afeedfdcebd95601ea6c5452a06681cc

Contents?: true

Size: 1.23 KB

Versions: 14

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Spree
  # Option types denote the different options for a variant. A typical option
  # type would be a size, with that option type’s values being something such
  # as "Small", "Medium" and "Large". Another typical option type could be a
  # color, such as "Red", "Green", or "Blue".
  #
  # A product can be assigned many option types, but must be assigned at least
  # one if you wish to create variants for that product.
  class OptionType < Spree::Base
    acts_as_list

    has_many :option_values, -> { order(:position) }, dependent: :destroy, inverse_of: :option_type
    has_many :product_option_types, dependent: :destroy, inverse_of: :option_type
    has_many :products, through: :product_option_types

    validates :name, presence: true, uniqueness: { allow_blank: true }
    validates :presentation, presence: true

    default_scope -> { order(:position) }

    accepts_nested_attributes_for :option_values, reject_if: lambda { |ov| ov[:name].blank? && ov[:presentation].blank? }, allow_destroy: true

    after_touch :touch_all_products
    after_save :touch_all_products

    self.whitelisted_ransackable_attributes = %w[name]

    def touch_all_products
      products.find_each(&:touch)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
solidus_core-2.10.5 app/models/spree/option_type.rb
solidus_core-2.10.3 app/models/spree/option_type.rb
solidus_core-2.10.2 app/models/spree/option_type.rb
solidus_core-2.9.6 app/models/spree/option_type.rb
solidus_core-2.10.1 app/models/spree/option_type.rb
solidus_core-2.9.5 app/models/spree/option_type.rb
solidus_core-2.10.0 app/models/spree/option_type.rb
solidus_core-2.9.4 app/models/spree/option_type.rb
solidus_core-2.9.3 app/models/spree/option_type.rb
solidus_core-2.9.2 app/models/spree/option_type.rb
solidus_core-2.10.0.beta1 app/models/spree/option_type.rb
solidus_core-2.9.1 app/models/spree/option_type.rb
solidus_core-2.9.0 app/models/spree/option_type.rb
solidus_core-2.9.0.rc.1 app/models/spree/option_type.rb