module Spree::Core::ProductFilters

See specific filters below for concrete examples.

Public Class Methods

brand_filter() click to toggle source
# File lib/spree/core/product_filters.rb, line 107
def self.brand_filter
  brand_property = Spree::Property.find_by(name: 'brand')
  brands = brand_property ? Spree::ProductProperty.where(property_id: brand_property.id).pluck(:value).uniq.map(&:to_s) : []
  pp = Spree::ProductProperty.arel_table
  conds = Hash[*brands.flat_map { |brand| [brand, pp[:value].eq(brand)] }]
  {
    name:   'Brands',
    scope:  :brand_any,
    conds:  conds,
    labels: brands.sort.map { |key| [key, key] }
  }
end
format_price(amount) click to toggle source
# File lib/spree/core/product_filters.rb, line 67
def self.format_price(amount)
  Spree::Money.new(amount)
end
price_filter() click to toggle source
# File lib/spree/core/product_filters.rb, line 71
def self.price_filter
  value = Spree::Price.arel_table
  conds = [[I18n.t('spree.under_price', price: format_price(10)), value[:amount].lteq(10)],
           ["#{format_price(10)} - #{format_price(15)}", value[:amount].between(10..15)],
           ["#{format_price(15)} - #{format_price(18)}", value[:amount].between(15..18)],
           ["#{format_price(18)} - #{format_price(20)}", value[:amount].between(18..20)],
           [I18n.t('spree.or_over_price', price: format_price(20)), value[:amount].gteq(20)]]
  {
    name:   I18n.t('spree.price_range'),
    scope:  :price_range_any,
    conds:  Hash[*conds.flatten],
    labels: conds.map { |key, _value| [key, key] }
  }
end
selective_brand_filter(taxon = nil) click to toggle source
# File lib/spree/core/product_filters.rb, line 143
def self.selective_brand_filter(taxon = nil)
  taxon ||= Spree::Taxonomy.first.root
  brand_property = Spree::Property.find_by(name: 'brand')
  scope = Spree::ProductProperty.where(property: brand_property).
    joins(product: :taxons).
    where("#{Spree::Taxon.table_name}.id" => [taxon] + taxon.descendants)
  brands = scope.pluck(:value).uniq
  {
    name:   'Applicable Brands',
    scope:  :selective_brand_any,
    labels: brands.sort.map { |key| [key, key] }
  }
end