Module: Spree::ProductFilters

Extended by:
ActionView::Helpers::NumberHelper, BaseHelper
Included in:
Taxon
Defined in:
lib/spree/product_filters.rb

Overview

This module is included by Taxon. In development mode that inclusion does not happen until Taxon class is loaded. Ensure that Taxon class is loaded before you try something like Product.price_range_any

Class Method Summary collapse

Methods included from BaseHelper

available_countries, body_class, breadcrumbs, current_orders_product_count, current_spree_page?, flash_messages, format_price, gem_available?, link_to_cart, logo, meta_data_tags, method_missing, order_subtotal, seo_url, taxons_tree, todays_short_date, variant_options, yesterdays_short_date

Class Method Details

.all_taxonsObject

Filtering by the list of all taxons

Similar idea as above, but we don’t want the descendants’ products, hence it uses one of the auto-generated scopes from SearchLogic.

idea: expand the format to allow nesting of labels?



188
189
190
191
192
193
194
195
# File 'lib/spree/product_filters.rb', line 188

def ProductFilters.all_taxons
  taxons = Spree::Taxonomy.all.map {|t| [t.root] + t.root.descendants }.flatten
  { :name   => "All taxons",
    :scope  => :taxons_id_equals_any,
    :labels => taxons.sort_by(&:name).map {|t| [t.name, t.id]},
    :conds  => nil # not needed
  }
end

.brand_filterObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/spree/product_filters.rb', line 107

def ProductFilters.brand_filter
  brand_property = Spree::Property.find_by_name("brand")
  brands = Spree::ProductProperty.where(:property_id => brand_property).map(&:value).compact.uniq
  pp = Spree::ProductProperty.arel_table
  conds  = Hash[*brands.map {|b| [b, pp[:value].eq(b)]}.flatten]
  { :name   => "Brands",
    :scope  => :brand_any,
    :conds  => conds,
    :labels => (brands.sort).map {|k| [k,k]}
  }
end

.price_filterObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/spree/product_filters.rb', line 70

def ProductFilters.price_filter
  v = Spree::Variant.arel_table
  conds = [ [ I18n.t(:under_price, :price => format_price(10))   , v[:price].lteq(10)],
            [ "#{format_price(10)} - #{format_price(15)}"        , v[:price].in(10..15)],
            [ "#{format_price(15)} - #{format_price(18)}"        , v[:price].in(15..18)],
            [ "#{format_price(18)} - #{format_price(20)}"        , v[:price].in(18..20)],
            [ I18n.t(:or_over_price, :price => format_price(20)) , v[:price].gteq(20)]]
  { :name   => I18n.t(:price_range),
    :scope  => :price_range_any,
    :conds  => Hash[*conds.flatten],
    :labels => conds.map {|k,v| [k,k]}
  }
end

.selective_brand_filter(taxon = nil) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/spree/product_filters.rb', line 144

def ProductFilters.selective_brand_filter(taxon = nil)
  if taxon.nil?
    taxon = Spree::Taxonomy.first.root
  end
  brand_property = Spree::Property.find_by_name("brand")
  scope = Spree::ProductProperty.scoped(:conditions => ["property_id = ?", brand_property]).
                                 scoped(:joins      => {:product => :taxons},
                                        :conditions => ["#{Spree::Taxon.table_name}.id in (?)", [taxon] + taxon.descendants])
  brands = scope.map {|p| p.value}.uniq

  { :name   => "Applicable Brands",
    :scope  => :selective_brand_any,
    :labels => brands.sort.map {|k| [k,k]}
  }
end

.taxons_below(taxon) ⇒ Object

Provide filtering on the immediate children of a taxon

This doesn’t fit the pattern of the examples above, so there’s a few changes. Firstly, it uses an existing scope which was not built for filtering - and so has no need of a conditions mapping, and secondly, it has a mapping of name to the argument type expected by the other scope.

This technique is useful for filtering on objects (by passing ids) or with a scope that can be used directly (eg. testing only ever on a single property).

This scope selects products in any of the active taxons or their children.



173
174
175
176
177
178
179
180
# File 'lib/spree/product_filters.rb', line 173

def ProductFilters.taxons_below(taxon)
  return Spree::ProductFilters.all_taxons if taxon.nil?
  { :name   => "Taxons under " + taxon.name,
    :scope  => :taxons_id_in_tree_any,
    :labels => taxon.children.sort_by(&:position).map {|t| [t.name, t.id]},
    :conds  => nil
  }
end