Sha256: ce2bb1ec455afcb50c79fbabbf8b290a236f0c883cfff00ca40a862a60bce087

Contents?: true

Size: 1.27 KB

Versions: 9

Compression:

Stored size: 1.27 KB

Contents

module Spree
  class OptionType < Spree::Base
    include UniqueName
    include Metadata
    include TranslatableResource
    if defined?(Spree::Webhooks)
      include Spree::Webhooks::HasWebhooks
    end

    TRANSLATABLE_FIELDS = %i[name presentation].freeze
    translates(*TRANSLATABLE_FIELDS)

    acts_as_list
    auto_strip_attributes :name, :presentation

    with_options dependent: :destroy, inverse_of: :option_type do
      has_many :option_values, -> { order(:position) }
      has_many :product_option_types
    end

    has_many :products, through: :product_option_types

    has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype'
    has_many :prototypes, through: :option_type_prototypes, class_name: 'Spree::Prototype'

    validates :presentation, presence: true

    default_scope { order(:position) }

    scope :filterable, -> { where(filterable: true) }

    accepts_nested_attributes_for :option_values, reject_if: ->(ov) { ov[:name].blank? || ov[:presentation].blank? }, allow_destroy: true

    after_touch :touch_all_products

    def filter_param
      name.parameterize
    end

    def self.color
      find_by(name: 'color')
    end

    private

    def touch_all_products
      products.update_all(updated_at: Time.current)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree_core-4.6.5 app/models/spree/option_type.rb
spree_core-4.7.2 app/models/spree/option_type.rb
spree_core-4.7.1 app/models/spree/option_type.rb
spree_core-4.6.4 app/models/spree/option_type.rb
spree_core-4.7.0 app/models/spree/option_type.rb
spree_core-4.6.3 app/models/spree/option_type.rb
spree_core-4.6.2 app/models/spree/option_type.rb
spree_core-4.6.1 app/models/spree/option_type.rb
spree_core-4.6.0 app/models/spree/option_type.rb