Module: Spree::Core::ProductFilters
- Defined in:
- lib/spree/core/product_filters.rb
Overview
See specific filters below for concrete examples.
Class Method Summary collapse
-
.all_taxons ⇒ Object
Filtering by the list of all taxons.
- .brand_filter ⇒ Object
- .format_price(amount) ⇒ 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 Ransack.
idea: expand the format to allow nesting of labels?
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/spree/core/product_filters.rb', line 185 def self.all_taxons Spree::Deprecation.warn "all_taxons is deprecated in solidus_core. Please add it to your own application to continue using it." taxons = Spree::Taxonomy.all.map { |element| [element.root] + element.root.descendants }.flatten { name: 'All taxons', scope: :taxons_id_equals_any, labels: taxons.sort_by(&:name).map { |element| [element.name, element.id] }, conds: nil # not needed } end |
.brand_filter ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/spree/core/product_filters.rb', line 106 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.map { |brand| [brand, pp[:value].eq(brand)] }.flatten] { name: 'Brands', scope: :brand_any, conds: conds, labels: brands.sort.map { |key| [key, key] } } end |
.format_price(amount) ⇒ Object
66 67 68 |
# File 'lib/spree/core/product_filters.rb', line 66 def self.format_price(amount) Spree::Money.new(amount) end |
.price_filter ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/spree/core/product_filters.rb', line 70 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].in(10..15)], ["#{format_price(15)} - #{format_price(18)}", value[:amount].in(15..18)], ["#{format_price(18)} - #{format_price(20)}", value[:amount].in(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) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/spree/core/product_filters.rb', line 142 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 |
.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.
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/spree/core/product_filters.rb', line 168 def self.taxons_below(taxon) Spree::Deprecation.warn "taxons_below is deprecated in solidus_core. Please add it to your own application to continue using it." return Spree::Core::ProductFilters.all_taxons if taxon.nil? { name: 'Taxons under ' + taxon.name, scope: :taxons_id_in_tree_any, labels: taxon.children.sort_by(&:position).map { |element| [element.name, element.id] }, conds: nil } end |