Permalink
Browse files

Improved support of facets.

  • Loading branch information...
1 parent cfb34b2 commit 54eac2df5aa891fe871cc17800901689f3c4acda @romul committed Feb 10, 2010
@@ -0,0 +1,8 @@
+module SphinxHelper
+ def link_to_facet(name, value, count)
+ facets_hash = params[:facets]
+ facets_hash = {} unless facets_hash.is_a?(Hash)
+ facets_hash = facets_hash.merge({name => [value, facets_hash[name]].join(', ')})
+ link_to("#{value} (#{count})", :overwrite_params => params.merge({:facets => facets_hash}))
+ end
+end
@@ -0,0 +1,17 @@
+<%
+facets = Spree::Config.searcher.facets || []
+ for facet in facets
+ options = facet.options
+ options = options.sort{|x,y| y.count <=> x.count} unless facet.name == "price_range"
+ unless options.empty? %>
+ <h4><%= t "#{facet.name}_facet" %></h4>
+ <ul><%
+ for option in options %>
+ <li>
+ <%= link_to_facet(facet.name, option.name, option.count) -%>
+ </li><%
+ end %>
+ </ul><%
+ end
+ end
+%>
@@ -10,11 +10,12 @@ def get_products_conditions_for(query)
if facets_hash
search_options.merge!(:conditions => facets_hash)
end
+
facets = Product.facets(query, search_options)
- products = facets.for(facets_hash)
+ products = facets.for
@properties[:products] = products
- @properties[:facets] = facets
+ @properties[:facets] = parse_facets_hash(facets)
{:conditions=> ["products.id IN (?)", products.map(&:id)]}
end
@@ -26,5 +27,38 @@ def prepare(params)
@properties[:manage_pagination] = true
@properties[:order_by_price] = params[:order_by_price]
end
+
+ private
+
+ def parse_facets_hash(facets_hash = {})
+ facets = []
+ facets_hash.each do |name, options|
+ next if options.size <= 1
+ facet = Facet.new(name)
+ options.each do |value, count|
+ facet.options << FacetOption.new(value, count)
+ end
+ facets << facet
+ end
+ facets
+ end
+ end
+
+ class Facet
+ attr_accessor :options
+ attr_accessor :name
+ def initialize(name, options = [])
+ self.name = name
+ self.options = options
+ end
+ end
+
+ class FacetOption
+ attr_accessor :name
+ attr_accessor :count
+ def initialize(name, count)
+ self.name = name
+ self.count = count
+ end
end
end
@@ -25,6 +25,10 @@ def activate
end
end
+ Spree::BaseController.class_eval do
+ helper :sphinx
+ end
+
Spree::Config.searcher = Spree::Search::ThinkingSphinx.new
end
end
@@ -1,43 +1,5 @@
class SphinxSearchHooks < Spree::ThemeSupport::HookListener
- #
- # In this file you can modify the content of the hooks available in the default templates
- # and avoid overriding a template in many situations. Multiple extensions can modify the
- # same hook, the changes being applied cumulatively based on extension load order
- #
- # Most hooks are defined with blocks so they span a region of the template, allowing content
- # to be replaced or removed as well as added to.
- #
- # Usage
- #
- # The following methods are available
- #
- # * insert_before
- # * insert_after
- # * replace
- # * remove
- #
- # All accept a block name symbol followed either by arguments that would be valid for 'render'
- # or a block which returns the string to be inserted. The block will have access to any methods
- # or instance variables accessible in your views
- #
- # Examples
- #
- # insert_before :homepage_products, :text => "<h1>Welcome!</h1>"
- # insert_after :homepage_products, 'shared/offers' # renders a partial
- # replace :taxon_sidebar_navigation, 'shared/my_sidebar
- #
- # adding a link below product details:
- #
- # insert_after :product_description do
- # '<p>' + link_to('Back to products', products_path) + '</p>'
- # end
- #
- # adding a new tab to the admin navigation
- #
- # insert_after :admin_tabs do
- # tab(:taxonomies)
- # end
- #
+ insert_before :search_results, 'products/facets'
end

0 comments on commit 54eac2d

Please sign in to comment.