Module: ProductFilters
- Included in:
- Taxon
- Defined in:
- lib/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
-
.all_taxons ⇒ Object
Filtering by the list of all taxons.
- .brand_filter ⇒ Object
- .price_filter ⇒ Object
- .selective_brand_filter(taxon = nil) ⇒ Object
-
.taxons_below(taxon) ⇒ Object
Provide filtering on the immediate children of a taxon.
Class Method Details
.all_taxons ⇒ Object
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?
170 171 172 173 174 175 176 177 |
# File 'lib/product_filters.rb', line 170 def ProductFilters.all_taxons taxons = 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_filter ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/product_filters.rb', line 90 def ProductFilters.brand_filter brands = ProductProperty.where(:property_id => @@brand_property).map(&:value).compact.uniq conds = Hash[*brands.map {|b| [b, "product_properties.value = '#{b}'"]}.flatten] { :name => "Brands", :scope => :brand_any, :conds => conds, :labels => (brands.sort).map {|k| [k,k]} } end |
.price_filter ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/product_filters.rb', line 56 def ProductFilters.price_filter conds = [ [ "Under $10", "price <= 10" ], [ "$10 - $15", "price between 10 and 15" ], [ "$15 - $18", "price between 15 and 18" ], [ "$18 - $20", "price between 18 and 20" ], [ "$20 or over", "price >= 20" ] ] { :name => "Price Range", :scope => :price_range_any, :conds => Hash[*conds.flatten], :labels => conds.map {|k,v| [k,k]} } end |
.selective_brand_filter(taxon = nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/product_filters.rb', line 124 def ProductFilters.selective_brand_filter(taxon = nil) if taxon.nil? taxon = Taxonomy.first.root end all_brands = ProductProperty.where(:property_id => @@brand_property).map(&:value).uniq scope = ProductProperty.scoped(:conditions => ["property_id = ?", @@brand_property]). scoped(:joins => {:product => :taxons}, :conditions => ["taxons.id in (?)", [taxon] + taxon.descendants]) brands = scope.map {|p| p.value} { :name => "Applicable Brands", :scope => :selective_brand_any, :conds => Hash[*all_brands.map {|m| [m, "p_colour.value like '%#{m}%'"]}.flatten], :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.
155 156 157 158 159 160 161 162 |
# File 'lib/product_filters.rb', line 155 def ProductFilters.taxons_below(taxon) return 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 |