Sha256: ec407ab27ea0e430247211775874edcd5de60fa54d44765231dfed65003b5ddf

Contents?: true

Size: 1.35 KB

Versions: 9

Compression:

Stored size: 1.35 KB

Contents

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

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

    belongs_to :option_type, class_name: 'Spree::OptionType', touch: true, inverse_of: :option_values

    acts_as_list scope: :option_type
    auto_strip_attributes :name, :presentation

    has_many :option_value_variants, class_name: 'Spree::OptionValueVariant'
    has_many :variants, through: :option_value_variants, class_name: 'Spree::Variant'

    with_options presence: true do
      validates :name, uniqueness: { scope: :option_type_id, case_sensitive: false }
      validates :presentation
    end

    scope :filterable, lambda {
      joins(:option_type).
        where(OptionType.table_name => { filterable: true }).
        distinct
    }

    scope :for_products, lambda { |products|
      joins(:variants).
        where(Variant.table_name => { product_id: products.map(&:id) })
    }

    after_touch :touch_all_variants

    delegate :name, :presentation, to: :option_type, prefix: true, allow_nil: true

    self.whitelisted_ransackable_attributes = ['presentation']

    private

    def touch_all_variants
      variants.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_value.rb
spree_core-4.7.2 app/models/spree/option_value.rb
spree_core-4.7.1 app/models/spree/option_value.rb
spree_core-4.6.4 app/models/spree/option_value.rb
spree_core-4.7.0 app/models/spree/option_value.rb
spree_core-4.6.3 app/models/spree/option_value.rb
spree_core-4.6.2 app/models/spree/option_value.rb
spree_core-4.6.1 app/models/spree/option_value.rb
spree_core-4.6.0 app/models/spree/option_value.rb