Commit 000b5f537c9e8f9e5ff6bb61bae71692f6973e87

Authored by Braulio Bhavamitra
1 parent d6de9190

Facets and ordering for product search

(ActionItem1901)
app/models/product.rb
... ... @@ -35,8 +35,6 @@ class Product < ActiveRecord::Base
35 35 end
36 36 end
37 37  
38   - acts_as_searchable :fields => [ :name, :description, :category_full_name ]
39   -
40 38 xss_terminate :only => [ :name ], :on => 'validation'
41 39 xss_terminate :only => [ :description ], :with => 'white_list', :on => 'validation'
42 40  
... ... @@ -153,4 +151,41 @@ class Product < ActiveRecord::Base
153 151 true
154 152 end
155 153  
  154 + private
  155 + def name_or_category
  156 + name ? name : product_category.name
  157 + end
  158 + def f_category_id
  159 + #def childs(id)
  160 + #ret = ProductCategory.find(:all, :conditions => {:parent_id => id}).collect(&:id)
  161 + #([id, ret.map { |i| childs(i) }])
  162 + #end
  163 + #childs(self.product_category.id).flatten
  164 + [self.product_category.id]
  165 + end
  166 + def f_region_id
  167 + if self.enterprise.region
  168 + self.enterprise.region.id
  169 + else
  170 + nil
  171 + end
  172 + end
  173 + def f_qualifier_id
  174 + product_qualifiers.collect(&:qualifier_id)
  175 + end
  176 + def f_certifier_id
  177 + product_qualifiers.collect(&:certifier_id)
  178 + end
  179 + public
  180 +
  181 + acts_as_faceted :fields => {:f_category_id => {:class => ProductCategory, :display_field => :name, :label => _('Related products')},
  182 + :f_region_id => {:class => Region, :display_field => :name, :label => _('Region')},
  183 + :f_qualifier_id => {:class => Qualifier, :display_field => :name, :label => _('Qualifiers')},
  184 + :f_certifier_id => {:class => Certifier, :display_field => :name, :label => _('Certifiers')}},
  185 + :order => [:f_category_id, :f_region_id, :f_qualifier_id, :f_certifier_id]
  186 +
  187 + acts_as_searchable :fields => [{:name_or_category => {:type => :text, :as => :name}},
  188 + {:name_or_category => {:type => :string, :as => :name_sort}}, :description, {:price => :float}, :category_full_name ] | facets.keys.map{|i| {i => :facet}},
  189 + :facets => facets.keys
  190 +
156 191 end
... ...
app/views/search/products.rhtml
... ... @@ -14,9 +14,17 @@
14 14 <% end %>
15 15  
16 16 <% cache(:action => 'assets', :asset => 'products', :category_path => params[:category_path], :query => @query, :product_category => @product_category, :region => @region, :radius => params[:radius]) do %>
17   - <%= product_categories_menu(:products, @product_category, @result_ids) %>
  17 + <% if @query.empty? %>
  18 + <%= product_categories_menu(:products, @product_category, @result_ids) %>
  19 + <% else %>
  20 + <%= facets_menu(:products, @facets[:products]) %>
  21 + <% end %>
18 22 <% end %>
19 23  
  24 +<%= facets_unselect_menu(:products) %>
  25 +
  26 +<%= order_by(:products, [[_('Best match'), ''], [_('Name'), 'name_sort asc'], [_('Lower price'), 'price asc'], [_('Higher price'), 'price desc']]) %>
  27 +
20 28 <%= display_results %>
21 29  
22 30 <% if @categories_menu %>
... ...