Sha256: c75c4905ba5d141924e77b8a7a8fe6d2d6b7270bd3c47e7ce4d7865f69f92a30

Contents?: true

Size: 1.59 KB

Versions: 28

Compression:

Stored size: 1.59 KB

Contents

module Spree
  class Variant < ActiveRecord::Base
    #FIXME WARNING tested only under sqlite and postgresql
    scope :descend_by_popularity, -> { order("COALESCE((SELECT COUNT(*) FROM  #{LineItem.quoted_table_name} GROUP BY #{LineItem.quoted_table_name}.variant_id HAVING #{LineItem.quoted_table_name}.variant_id = #{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 = 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_conditions = option_values.each do |option_value|
          option_value_conditions = case option_value
          when OptionValue then { "#{OptionValue.table_name}.name" => option_value.name }
          when String      then { "#{OptionValue.table_name}.name" => option_value }
          else                  { "#{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

28 entries across 28 versions & 1 rubygems

Version Path
spree_core-2.2.14 app/models/spree/variant/scopes.rb
spree_core-2.2.13 app/models/spree/variant/scopes.rb
spree_core-2.2.12 app/models/spree/variant/scopes.rb
spree_core-2.2.11 app/models/spree/variant/scopes.rb
spree_core-2.2.10 app/models/spree/variant/scopes.rb
spree_core-2.2.9 app/models/spree/variant/scopes.rb
spree_core-2.1.12 app/models/spree/variant/scopes.rb
spree_core-2.2.8 app/models/spree/variant/scopes.rb
spree_core-2.1.11 app/models/spree/variant/scopes.rb
spree_core-2.2.7 app/models/spree/variant/scopes.rb
spree_core-2.1.10 app/models/spree/variant/scopes.rb
spree_core-2.1.9 app/models/spree/variant/scopes.rb
spree_core-2.2.6 app/models/spree/variant/scopes.rb
spree_core-2.2.5 app/models/spree/variant/scopes.rb
spree_core-2.1.8 app/models/spree/variant/scopes.rb
spree_core-2.2.4 app/models/spree/variant/scopes.rb
spree_core-2.2.3 app/models/spree/variant/scopes.rb
spree_core-2.2.2 app/models/spree/variant/scopes.rb
spree_core-2.1.7 app/models/spree/variant/scopes.rb
spree_core-2.1.6 app/models/spree/variant/scopes.rb