Skip to content

Instantly share code, notes, and snippets.

@tobob
Last active December 27, 2015 05:18
Show Gist options
  • Save tobob/7272561 to your computer and use it in GitHub Desktop.
Save tobob/7272561 to your computer and use it in GitHub Desktop.
Spree filter by many properties (require squeel gem)
# encoding: utf-8
module Spree
module Core
module ProductFilters
if Spree::Property.table_exists?
Spree::Product.add_search_scope :color_filer do |*opts|
color_conds = opts.map {|o| ProductFilters.color_filter[:conds][o]}.reject { |c| c.nil? }
color_scope = color_conds.shift
color_conds.each do |new_scope|
color_scope = color_scope.or(new_scope)
end
Spree::Product.where{id.in(Spree::Product.with_property("Kolor").where(color_scope).pluck(:id))}.joins{product_properties}
end
Spree::Product.add_search_scope :rarity_filer do |*opts|
rarity_conds = opts.map {|o| ProductFilters.rarity_filter[:conds][o]}.reject { |c| c.nil? }
rarity_scope = rarity_conds.shift
rarity_conds.each do |new_scope|
rarity_scope = rarity_scope.or(new_scope)
end
Spree::Product.where{id.in(Spree::Product.with_property("Rarity").where(rarity_scope).pluck(:id))}.joins{product_properties}
#Spree::Product.where(rarity_scope).pluck(:id).joins{product_properties}
end
Spree::Product.add_search_scope :additional_filer do |*opts|
additional_conds = opts.map {|o| ProductFilters.additional_filter[:conds][o]}.reject { |c| c.nil? }
additional_scope = additional_conds.shift
additional_conds.each do |new_scope|
additional_scope = additional_scope.or(new_scope)
end
Spree::Product.where{id.in(Spree::Product.with_property("Dodatkowe").where(additional_scope).pluck(:id))}.joins{product_properties}
#Spree::Product.where(additional_scope).pluck(:id).joins{product_properties}
end
end
def ProductFilters.color_filter
color_property = Spree::Property.where(name: 'Kolor').first
colors = ["%Black%", "%Red%", "%White%", "%Blue%", "None"]
pp = Spree::ProductProperty.arel_table
conds = Hash[*colors.map { |b| [b, pp[:value].matches(b)] }.flatten]
{
name: 'Kolor',
scope: :color_filer,
conds: conds,
labels: (colors.sort).map { |k| [k.gsub("%",""), k] }
}
end
def ProductFilters.rarity_filter
rarity_property = Spree::Property.where(name: 'Rarity').first
rarities = Spree::ProductProperty.where(:property_id => rarity_property).pluck(:value).uniq
pp = Spree::ProductProperty.arel_table
conds = Hash[*rarities.map { |b| [b, pp[:value].eq(b)] }.flatten]
{
name: 'Rarity',
scope: :rarity_filer,
conds: conds,
labels: (rarities.sort).map { |k| [k, k] }
}
end
def ProductFilters.additional_filter
additional_property = Spree::Property.where(name: 'Dodatkowe').first
additionals = Spree::ProductProperty.where(:property_id => additional_property).pluck(:value).uniq
pp = Spree::ProductProperty.arel_table
conds = Hash[*additionals.map { |b| [b, pp[:value].eq(b)] }.flatten]
{
name: 'Dodatkowe cechy karty',
scope: :additional_filer,
conds: conds,
labels: (additionals.sort).map { |k| [k, k] }
}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment