Skip to content

Instantly share code, notes, and snippets.

@hachpai
Forked from Ranger-X/product_filters.rb
Last active December 14, 2015 19:39
Show Gist options
  • Save hachpai/5138459 to your computer and use it in GitHub Desktop.
Save hachpai/5138459 to your computer and use it in GitHub Desktop.
In order to make search on range of numerical option values, I've implemented this version. As spree stocks option value in a string, I've to cast it to compare. In the original version of this gist, I was unable to make or join between the created scope. With arel table, no problem.
def ProductFilters.ov_range_test(range1, range2)
ov = Arel::Table.new("spree_option_values")
cast = Arel::Nodes::NamedFunction.new "CAST", [ ov[:presentation].as("integer")]
comparaisons = cast.in(range1..range2)
comparaisons
end
Spree::Product.add_search_scope :screenSize_range_any do |*opts|
conds = opts.map {|o| Spree::ProductFilters.screenSize_filter[:conds][o]}.reject {|c| c.nil?}
scope = conds.shift
conds.each do |new_scope|
scope = scope.or(new_scope)
end
option_values=Spree::OptionValue.where(scope).joins(:option_type).where(OptionType.table_name => {:name => "tailleEcran"}).pluck("#{OptionValue.table_name}.id")
Spree::Product.where("#{Product.table_name}.id in (select product_id from #{Variant.table_name} v left join spree_option_values_variants ov on ov.variant_id = v.id where ov.option_value_id in (?))", option_values)
end
def ProductFilters.screenSize_filter
conds = [ [ "20p ou moins",ov_range_test(0,20)],
[ "20p - 30p",ov_range_test(20,30)],
[ "30p - 40p" ,ov_range_test(30,40)],
[ "40p ou plus",ov_range_test(40,190)]]
{ :name => "taille",
:scope => :screenSize_range_any,
:options => :tailleEcran,
:conds => Hash[*conds.flatten],
:labels => conds.map {|k,v| [k,k]}
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment