Ruby on rails 属性和变体的Spree搜索筛选器
我正在运行Spree1.3.1,我正在尝试自定义分类显示页面 我希望它返回包含在当前分类中的产品,最终通过属性或期权价值进行过滤 比如说,我看到了一个内衣系列的分类。 我想通过提供特定的尺寸(选项类型)来过滤显示的产品。 在这种情况下,我应该只列出具有所需尺寸变体的产品 我也想能够过滤的“适合”属性的产品。 通过slip-fit筛选,我应该能够只列出当前分类中具有所需属性的产品 这是分类单元控制器显示操作:Ruby on rails 属性和变体的Spree搜索筛选器,ruby-on-rails,spree,Ruby On Rails,Spree,我正在运行Spree1.3.1,我正在尝试自定义分类显示页面 我希望它返回包含在当前分类中的产品,最终通过属性或期权价值进行过滤 比如说,我看到了一个内衣系列的分类。 我想通过提供特定的尺寸(选项类型)来过滤显示的产品。 在这种情况下,我应该只列出具有所需尺寸变体的产品 我也想能够过滤的“适合”属性的产品。 通过slip-fit筛选,我应该能够只列出当前分类中具有所需属性的产品 这是分类单元控制器显示操作: Spree::TaxonsController.class_eval do d
Spree::TaxonsController.class_eval do
def show
@taxon = Spree::Taxon.find_by_permalink!(params[:id])
return unless @taxon
@searcher = Spree::Config.searcher_class.new(params)
@searcher.current_user = try_spree_current_user
@searcher.current_currency = current_currency
@products = @searcher.retrieve_products
respond_with(@taxon)
end
end
我应该如何修改它以满足我的需要?我部分解决了这个问题 我发现我需要让控制器保持原样,神奇之处在于lib/spree/product_filters.rb文件中,我在其中添加了这个新的产品过滤器:
if Spree::Property.table_exists?
Spree::Product.add_search_scope :fit_any do |*opts|
conds = opts.map {|o| ProductFilters.fit_filter[:conds][o]}.reject {|c| c.nil?}
scope = conds.shift
conds.each do |new_scope|
scope = scope.or(new_scope)
end
Spree::Product.with_property("fit").where(scope)
end
def ProductFilters.fit_filter
fit_property = Spree::Property.find_by_name("fit")
fits = Spree::ProductProperty.where(:property_id => fit_property).pluck(:value).uniq
pp = Spree::ProductProperty.arel_table
conds = Hash[*fits.map { |b| [b, pp[:value].eq(b)] }.flatten]
{ :name => "Fits",
:scope => :fit_any,
:conds => conds,
:labels => (fits.sort).map { |k| [k, k] }
}
end
end
然后,我将新过滤器添加到Taxon模型装饰器中,如下所示:
Spree::Taxon.class_eval do
def applicable_filters
fs = []
fs << Spree::Core::ProductFilters.fit_filter if Spree::Core::ProductFilters.respond_to?(:fit_filter)
fs
end
end
Spree::Taxon.class\u eval do
def过滤器
fs=[]
fs关于数值过滤,我已经为选项范围编写了一个过滤器:
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
对于离散的特定值,您也可以看到这个:
大家好。有没有关于这个习俗的文件。谢谢:)。我读了你的答案,但我不明白我是如何定义的