Sha256: 513b2e8e118bcea5e396962fbe0040d0e32101ce6ef125a4af96ebabd4b94ce7

Contents?: true

Size: 1.72 KB

Versions: 42

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Spree
  class Variant < Spree::Base
    # FIXME: WARNING tested only under sqlite and postgresql
    scope :descend_by_popularity, -> {
      order(Arel.sql("COALESCE((SELECT COUNT(*) FROM  #{Spree::LineItem.quoted_table_name} GROUP BY #{Spree::LineItem.quoted_table_name}.variant_id HAVING #{Spree::LineItem.quoted_table_name}.variant_id = #{Spree::Variant.quoted_table_name}.id), 0) DESC"))
    }

    class << self
      # Returns variants that match a given option value
      #
      # Example:
      #
      # product.variants_including_master.has_option(OptionType.find_by(name: 'shoe-size'), OptionValue.find_by(name: '8'))
      def has_option(option_type, *option_values)
        option_types = Spree::OptionType.table_name

        option_type_conditions = case option_type
                                 when OptionType then { "#{option_types}.name" => option_type.name }
                                 when String     then { "#{option_types}.name" => option_type }
        else { "#{option_types}.id" => option_type }
        end

        relation = joins(option_values: :option_type).where(option_type_conditions)

        option_values.each do |option_value|
          option_value_conditions = case option_value
                                    when OptionValue then { "#{Spree::OptionValue.table_name}.name" => option_value.name }
                                    when String      then { "#{Spree::OptionValue.table_name}.name" => option_value }
          else { "#{Spree::OptionValue.table_name}.id" => option_value }
          end
          relation = relation.where(option_value_conditions)
        end

        relation
      end

      alias_method :has_options, :has_option
    end
  end
end

Version data entries

42 entries across 42 versions & 2 rubygems

Version Path
solidus_core-2.6.3 app/models/spree/variant/scopes.rb
solidus_core-2.8.0 app/models/spree/variant/scopes.rb
solidus_core-2.7.1 app/models/spree/variant/scopes.rb
solidus_core-2.6.2 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.4 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.3 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.1 app/models/spree/variant/scopes.rb
solidus_core_devise_token_auth-2.8.0.alpha.0 app/models/spree/variant/scopes.rb
solidus_core-2.7.0 app/models/spree/variant/scopes.rb
solidus_core-2.6.1 app/models/spree/variant/scopes.rb