Commit 11205f7f215b501932a36ab17ba130b75423ef50
1 parent
f0b5cd9d
Exists in
master
and in
28 other branches
Add facets helpers and views
Showing
5 changed files
with
99 additions
and
0 deletions
Show diff stats
app/controllers/public/search_controller.rb
... | ... | @@ -106,6 +106,11 @@ class SearchController < PublicController |
106 | 106 | result[:date_range] = (date - 1.month)..(date + 1.month).at_end_of_month |
107 | 107 | end |
108 | 108 | |
109 | + if asset_class(asset).methods.include?('facets') | |
110 | + result.merge!(:facets => {:zeros => false, :sort => :count, :fields => asset_class(asset).facets.keys, | |
111 | + :browse => params[:facet] ? params[:facet].map{ |k,v| k.to_s+':'+v.to_s} : ''}) | |
112 | + end | |
113 | + | |
109 | 114 | result |
110 | 115 | end |
111 | 116 | ... | ... |
app/helpers/search_helper.rb
... | ... | @@ -115,4 +115,23 @@ module SearchHelper |
115 | 115 | render(:partial => 'product_categories_menu', :object => product_categories_menu) |
116 | 116 | end |
117 | 117 | |
118 | + def facets_menu(asset, _facets) | |
119 | + @asset_class = asset_class(asset) | |
120 | + @facets = _facets | |
121 | + render(:partial => 'facets_menu') | |
122 | + end | |
123 | + | |
124 | + def facets_unselect_menu(asset) | |
125 | + @asset_class = asset_class(asset) | |
126 | + render(:partial => 'facets_unselect_menu') | |
127 | + end | |
128 | + | |
129 | + def asset_class(asset) | |
130 | + asset.to_s.singularize.camelize.constantize | |
131 | + end | |
132 | + | |
133 | + def asset_table(asset) | |
134 | + asset_class(asset).table_name | |
135 | + end | |
136 | + | |
118 | 137 | end | ... | ... |
... | ... | @@ -0,0 +1,51 @@ |
1 | +<% more_options = _("+ Options") %> | |
2 | +<% less_options = _("- Options") %> | |
3 | +<% less_options_limit = 7 %> | |
4 | + | |
5 | +<div id="facets-menu"> | |
6 | + <% if !@facets["facet_fields"].empty? %> | |
7 | + <% @asset_class.each_facet do |facet_id, index| %> | |
8 | + <% facet = @asset_class.facets[facet_id] %> | |
9 | + <% solr_facet = @asset_class.to_solr_facet_fields[facet_id] %> | |
10 | + <% facets_all = @facets["facet_fields"][solr_facet] %> | |
11 | + <% facets_found = params[:facet] ? facets_all.reject {|id,c| params[:facet][facet_id.to_s].to_s == id.to_s } : facets_all %> | |
12 | + | |
13 | + <% if facets_found.count > 0 %> | |
14 | + <div id="facet-menu-<%= index.to_s %>" class="facet-menu"> | |
15 | + <div class="facet-menu-label"> | |
16 | + <%= facet[:label] %> | |
17 | + </div><br /> | |
18 | + | |
19 | + <% if facets_found.count > less_options_limit %> | |
20 | + <div class="facet-menu-options facet-menu-more-options" style="display: none"> | |
21 | + <% array = [] %> | |
22 | + <% @asset_class.each_facet_obj(solr_facet, facets_found, :sort => :alphabetically) do |obj, count| %> | |
23 | + <% array << {:id => id, :name => link_to(obj.send(facet[:display_field].to_s), params.merge({"facet[#{facet_id.to_s}]" => obj.id})) + " (#{count})"} %> | |
24 | + <% end %> | |
25 | + | |
26 | + <%= text_field :facet, facet_id %> | |
27 | + <% javascript_tag do %> | |
28 | + jQuery.TokenList($("<%='facet_'+facet_id.to_s%>"), <%=array.to_json%>, {searchDelay: 0, permanentDropdown: true, theme: "facet", dontAdd: true, preventDuplicates: true}); | |
29 | + <% end %> | |
30 | + </div> | |
31 | + <% end %> | |
32 | + | |
33 | + <div class="facet-menu-options facet-menu-less-options"> | |
34 | + <% c = 0; @asset_class.each_facet_obj(solr_facet, facets_found, :sort => :count) do |obj, count| %> | |
35 | + <%= link_to(obj.send(facet[:display_field].to_s), params.merge({"facet[#{facet_id.to_s}]" => obj.id})) + " (#{count})" %><br /> | |
36 | + <% break if (c += 1) > less_options_limit %> | |
37 | + <% end %> | |
38 | + </div> <br /> | |
39 | + | |
40 | + <% if facets_found.count > less_options_limit %> | |
41 | + <%= link_to_function more_options, "jQuery('#facet-menu-"+index.to_s+" .facet-menu-options').toggle(200); " + | |
42 | + "jQuery(this).text(jQuery(this).text() == '"+less_options+"' ? '"+more_options+"' : '"+less_options+"');" %> | |
43 | + <br /> | |
44 | + <% end %> | |
45 | + | |
46 | + <br /> | |
47 | + </div> | |
48 | + <% end %> | |
49 | + <% end %> | |
50 | + <% end %> | |
51 | + </div> | ... | ... |
... | ... | @@ -0,0 +1,8 @@ |
1 | + | |
2 | +<% if params[:facet] %> | |
3 | + <% params[:facet].each do |facet_id, obj_id| %> | |
4 | + <% facet = @asset_class.facets[facet_id.to_sym] %> | |
5 | + <% obj = facet[:class].find_by_id(obj_id.to_i) %> | |
6 | + <%= link_to(obj.send(facet[:display_field]), params.merge(:facet => params[:facet].reject {|k,v| k == facet_id}), :class => 'facet-selected') if obj %> | |
7 | + <% end %> | |
8 | +<% end %> | ... | ... |
public/stylesheets/application.css
... | ... | @@ -3049,6 +3049,22 @@ div#activation_enterprise div { |
3049 | 3049 | margin-top: 0px; |
3050 | 3050 | } |
3051 | 3051 | |
3052 | +.controller-search #facets-menu { | |
3053 | + width: 20%; | |
3054 | + float: left; | |
3055 | +} | |
3056 | + | |
3057 | +.controller-search #facets-menu div { | |
3058 | + position: relative; | |
3059 | +} | |
3060 | + | |
3061 | +.controller-search .facet-selected { | |
3062 | + display: block; | |
3063 | + background-color: #000; | |
3064 | + color: #fff; | |
3065 | + float: left; | |
3066 | +} | |
3067 | + | |
3052 | 3068 | #product-category, |
3053 | 3069 | .product-category-hierarchy { |
3054 | 3070 | margin-top: 40px; | ... | ... |