Ruby on rails Spree产品价格范围筛选器不工作
在实习期间,我需要处理一个现有的RubyonRails项目。 这项工程是疯狂建造的。我之前的开发者调整了价格过滤器,使范围变为该分类群产品的最大值。 这里的代码是“lib/spree/core/product_filter.rb”Ruby on rails Spree产品价格范围筛选器不工作,ruby-on-rails,ruby,spree,Ruby On Rails,Ruby,Spree,在实习期间,我需要处理一个现有的RubyonRails项目。 这项工程是疯狂建造的。我之前的开发者调整了价格过滤器,使范围变为该分类群产品的最大值。 这里的代码是“lib/spree/core/product_filter.rb” Spree::Product.add_search_scope:price_range_any do|*选项| opts_arr,max_prod,min_prod=[],nil,nil 选择。每个都做|选择| tmp_arr=opt.split(“+”) 选择 Sp
Spree::Product.add_search_scope:price_range_any do|*选项|
opts_arr,max_prod,min_prod=[],nil,nil
选择。每个都做|选择|
tmp_arr=opt.split(“+”)
选择
Spree::Product.add_search_scope :price_range_any do |*opts|
opts_arr, max_prod, min_prod = [], nil, nil
opts.each do |opt|
tmp_arr = opt.split("+")
opts_arr << tmp_arr[0]
max_prod = tmp_arr[1]
min_prod = tmp_arr[2]
end
conds = opts_arr.map {|o| Spree::Core::ProductFilters.price_filter(max_prod.to_i, min_prod.to_i)[:conds][o]}.reject { |c| c.nil? }
scope = conds.shift
conds.each do |new_scope|
scope = scope.or(new_scope)
end
Spree::Product.joins(master: :default_price).where(scope)
end
def ProductFilters.format_price(amount)
Spree::Money.new(amount)
end
def ProductFilters.price_filter(max_price = nil, min_price = nil)
v = Spree::Price.arel_table
if max_price < 200
if max_price < 100
highest_price = max_price - max_price.modulo(10)
else
highest_price = max_price - max_price.modulo(25)
end
interval = (highest_price)/5
second = interval + interval
third = second + interval
conds = [ [ "#{format_price(0)} - #{format_price(interval)}" , v[:amount].in(0..interval)],
[ "#{format_price(interval)} - #{format_price(second)}" , v[:amount].in(interval..second)],
[ "#{format_price(second)} - #{format_price(third)}" , v[:amount].in(second..third)],
[ "#{format_price(third)} - #{format_price(highest_price)}" , v[:amount].in(third..highest_price)],
[ Spree.t(:or_over_price, price: format_price(highest_price)) , v[:amount].gteq(highest_price)]]
{
name: Spree.t(:price_range),
scope: :price_range_any,
conds: Hash[*conds.flatten],
labels: conds.map { |k,v| [k, k] }
}
else
conds = [ [ "#{format_price(0)} - #{format_price(50)}" , v[:amount].in(0..50)],
[ "#{format_price(50)} - #{format_price(100)}" , v[:amount].in(50..100)],
[ "#{format_price(100)} - #{format_price(150)}" , v[:amount].in(100..150)],
[ "#{format_price(150)} - #{format_price(200)}" , v[:amount].in(150..200)],
[ Spree.t(:or_over_price, price: format_price(200)) , v[:amount].gteq(200)]]
{
name: Spree.t(:price_range),
scope: :price_range_any,
conds: Hash[*conds.flatten],
labels: conds.map { |k,v| [k, k] }
}
end
end