2016-03-23 13 views
0

ここでは、https://github.com/spree/spreeのように最新のバージョンのspree eコマースでカテゴリのカスタムフィルタを実装する必要があります。複数のフィルタをSpree Commerce Railsに追加

約100個以上のフィルタがあるため、動的に行う必要があります。理想的な解決策は、管理領域にすべての使用可能なフィルタを表示することです。管理者は各カテゴリに対してフィルタを有効または無効にすることができます。

現在のシナリオ: 新しいフィルタを作成して適用する方法はわかっています。しかし、以下にリンクされているproduct_filter.rbファイルに示されているように、フィルタあたり約4つのメソッドが必要です。

我々が有用であることが分かってきたいくつかのリンク:ここ

https://gist.github.com/maxivak/cc73b88699c9c6b45a95 https://github.com/radar/spree-core/blob/master/lib/spree/product_filters.rb

+0

私はこの問題を自分で解決しました。今私はメタプログラミングによって動的フィルタを作成しています。実行時にフィルタを作成します。 –

+0

どのように達成されたかのヒント/ – GorillaApe

答えて

0

を使用すると、複数のプロパティでフィルタすることができますいくつかのコードです。理想的ではありませんが(適切な検証などはありませんが)、複数の「in」サブクエリを実行するよりも優れていると思います。

 def add_search_scopes(base_scope) 
      joins = nil 
      conditions = nil 
      product_property_alias = nil 
      i = 1 
      search.each do |name, scope_attribute| 
       scope_name = name.to_sym 
       # If method is defined in product_filters 
       if base_scope.respond_to?(:search_scopes) && base_scope.search_scopes.include?(scope_name.to_sym) 
        base_scope = base_scope.send(scope_name, *scope_attribute) 
       else 
        next if scope_attribute.first.empty? 
        # Find property by name 
        property_name = name.gsub('_any', '').gsub('selective_', '') 
        property = Spree::Property.find_by_name(property_name) 
        next unless property 

        # Table joins 
        joins = product if joins.nil? 
        product_property_alias = product_property.alias("filter_product_property_#{i}") 
        joins = joins.join(product_property_alias).on(product[:id].eq(product_property_alias[:product_id])) 
        i += 1 

        # Conditions 
        condition = product_property_alias[:property_id].eq(property.id) 
                    .and(product_property_alias[:value].eq(scope_attribute)) 

        conditions = conditions.nil? ? condition : conditions.and(condition) 

       end 
      end if search.is_a?(Hash) 
      joins ? base_scope.joins(joins.join_sources).where(conditions) : base_scope 
     end 

     def prepare(params) 
      super 
      @properties[:product] = Spree::Product.arel_table 
      @properties[:product_property] = Spree::ProductProperty.arel_table 
     end 
関連する問題