Commit f25bd3ac1aecb661584ea5b6b1c6646bb09545a6
1 parent
607528df
Exists in
master
and in
28 other branches
Add functions to check multiple and map search conditions
Showing
5 changed files
with
62 additions
and
28 deletions
Show diff stats
app/controllers/public/search_controller.rb
@@ -250,10 +250,9 @@ class SearchController < PublicController | @@ -250,10 +250,9 @@ class SearchController < PublicController | ||
250 | end | 250 | end |
251 | 251 | ||
252 | def limit | 252 | def limit |
253 | - searching = @searching.values.select{ |v| v } | ||
254 | - if params[:display] == 'map' | 253 | + if map_search? |
255 | MAP_SEARCH_LIMIT | 254 | MAP_SEARCH_LIMIT |
256 | - elsif searching.size <= 1 | 255 | + elsif !multiple_search? |
257 | if [:people, :communities].include? @asset | 256 | if [:people, :communities].include? @asset |
258 | BLOCKS_SEARCH_LIMIT | 257 | BLOCKS_SEARCH_LIMIT |
259 | elsif @asset == :enterprises and @empty_query | 258 | elsif @asset == :enterprises and @empty_query |
@@ -267,31 +266,34 @@ class SearchController < PublicController | @@ -267,31 +266,34 @@ class SearchController < PublicController | ||
267 | end | 266 | end |
268 | 267 | ||
269 | def paginate_options(page = params[:page]) | 268 | def paginate_options(page = params[:page]) |
269 | + page = 1 if multiple_search? or params[:display] == 'map' | ||
270 | { :per_page => limit, :page => page } | 270 | { :per_page => limit, :page => page } |
271 | end | 271 | end |
272 | 272 | ||
273 | def full_text_search(filters = [], options = {}) | 273 | def full_text_search(filters = [], options = {}) |
274 | paginate_options = paginate_options(params[:page]) | 274 | paginate_options = paginate_options(params[:page]) |
275 | asset_class = asset_class(@asset) | 275 | asset_class = asset_class(@asset) |
276 | - | ||
277 | solr_options = options | 276 | solr_options = options |
278 | - if !@results_only and asset_class.respond_to? :facets | ||
279 | - solr_options.merge! asset_class.facets_find_options(params[:facet]) | ||
280 | - solr_options[:all_facets] = true | ||
281 | - solr_options[:limit] = 0 if @facets_only | ||
282 | - end | ||
283 | - solr_options[:filter_queries] ||= [] | ||
284 | - solr_options[:filter_queries] += filters | ||
285 | - solr_options[:filter_queries] << "environment_id:#{environment.id}" | ||
286 | - solr_options[:filter_queries] << asset_class.facet_category_query.call(@category) if @category | ||
287 | - | ||
288 | - solr_options[:boost_functions] ||= [] | ||
289 | - params[:order_by] = nil if params[:order_by] == 'none' | ||
290 | - if params[:order_by] | ||
291 | - order = SortOptions[@asset][params[:order_by].to_sym] | ||
292 | - raise "Unknown order by" if order.nil? | ||
293 | - order[:solr_opts].each do |opt, value| | ||
294 | - solr_options[opt] = value.is_a?(Proc) ? instance_eval(&value) : value | 277 | + pg_options = paginate_options(params[:page]) |
278 | + | ||
279 | + if !multiple_search? | ||
280 | + if !@results_only and asset_class.respond_to? :facets | ||
281 | + solr_options.merge! asset_class.facets_find_options(params[:facet]) | ||
282 | + solr_options[:all_facets] = true | ||
283 | + end | ||
284 | + solr_options[:filter_queries] ||= [] | ||
285 | + solr_options[:filter_queries] += filters | ||
286 | + solr_options[:filter_queries] << "environment_id:#{environment.id}" | ||
287 | + solr_options[:filter_queries] << asset_class.facet_category_query.call(@category) if @category | ||
288 | + | ||
289 | + solr_options[:boost_functions] ||= [] | ||
290 | + params[:order_by] = nil if params[:order_by] == 'none' | ||
291 | + if params[:order_by] | ||
292 | + order = SortOptions[@asset][params[:order_by].to_sym] | ||
293 | + raise "Unknown order by" if order.nil? | ||
294 | + order[:solr_opts].each do |opt, value| | ||
295 | + solr_options[opt] = value.is_a?(Proc) ? instance_eval(&value) : value | ||
296 | + end | ||
295 | end | 297 | end |
296 | end | 298 | end |
297 | 299 |
app/helpers/search_helper.rb
@@ -45,6 +45,14 @@ module SearchHelper | @@ -45,6 +45,14 @@ module SearchHelper | ||
45 | # FIXME remove it after search_controler refactored | 45 | # FIXME remove it after search_controler refactored |
46 | include EventsHelper | 46 | include EventsHelper |
47 | 47 | ||
48 | + def multiple_search? | ||
49 | + ['index', 'category_index'].include?(params[:action]) or @results.size > 1 | ||
50 | + end | ||
51 | + | ||
52 | + def map_search? | ||
53 | + !@query.blank? and !multiple_search? and params[:display] == 'map' | ||
54 | + end | ||
55 | + | ||
48 | def search_page_title(title, category = nil) | 56 | def search_page_title(title, category = nil) |
49 | title = "<h1>" + title | 57 | title = "<h1>" + title |
50 | title += '<small>' + category.name + '</small>' if category | 58 | title += '<small>' + category.name + '</small>' if category |
@@ -58,8 +66,8 @@ module SearchHelper | @@ -58,8 +66,8 @@ module SearchHelper | ||
58 | :align => 'center', :class => 'search-category-context') if category | 66 | :align => 'center', :class => 'search-category-context') if category |
59 | end | 67 | end |
60 | 68 | ||
61 | - def display_results(use_map = false) | ||
62 | - if params[:display] == 'map' && use_map | 69 | + def display_results(map_capable = false) |
70 | + if map_capable and map_search? | ||
63 | partial = 'google_maps' | 71 | partial = 'google_maps' |
64 | klass = 'map' | 72 | klass = 'map' |
65 | else | 73 | else |
app/views/search/_display_results.rhtml
1 | -<div id="search-results" class="<%= @results.size == 1 ? 'only-one-result-box' : 'multiple-results-boxes' %>"> | 1 | +<div id="search-results" class="<%= !multiple_search? ? 'only-one-result-box' : 'multiple-results-boxes' %>"> |
2 | <% @order.each do |name| %> | 2 | <% @order.each do |name| %> |
3 | <% results = @results[name] %> | 3 | <% results = @results[name] %> |
4 | <% empty = results.nil? || results.empty? %> | 4 | <% empty = results.nil? || results.empty? %> |
@@ -7,7 +7,7 @@ | @@ -7,7 +7,7 @@ | ||
7 | <% if not empty %> | 7 | <% if not empty %> |
8 | <% partial = partial_for_class(results.first.class.class_name.constantize) %> | 8 | <% partial = partial_for_class(results.first.class.class_name.constantize) %> |
9 | 9 | ||
10 | - <% if @results.size > 1 %> | 10 | + <% if multiple_search? %> |
11 | <h3><%= @names[name] %></h3> | 11 | <h3><%= @names[name] %></h3> |
12 | <% if results.total_entries > SearchController::MULTIPLE_SEARCH_LIMIT %> | 12 | <% if results.total_entries > SearchController::MULTIPLE_SEARCH_LIMIT %> |
13 | <%= link_to(_('see all (%d)') % results.total_entries, params.merge(:action => name), :class => 'see-more' ) %> | 13 | <%= link_to(_('see all (%d)') % results.total_entries, params.merge(:action => name), :class => 'see-more' ) %> |
@@ -22,9 +22,10 @@ | @@ -22,9 +22,10 @@ | ||
22 | </ul> | 22 | </ul> |
23 | </div> | 23 | </div> |
24 | <% else %> | 24 | <% else %> |
25 | - <% if @results.size > 1 %> | 25 | + <% if multiple_search? %> |
26 | <h3><%= @names[name] %></h3> | 26 | <h3><%= @names[name] %></h3> |
27 | <% end %> | 27 | <% end %> |
28 | + | ||
28 | <div class="search-results-innerbox search-results-type-empty"> | 29 | <div class="search-results-innerbox search-results-type-empty"> |
29 | <div> <%= _('None') %> </div> | 30 | <div> <%= _('None') %> </div> |
30 | </div> | 31 | </div> |
app/views/search/_profile.rhtml
1 | <li class="search-profile-item"> | 1 | <li class="search-profile-item"> |
2 | -<% if @empty_query or @results.size > 1 or !profile.enterprise? %> | 2 | +<% if @empty_query or multiple_search? or !profile.enterprise? %> |
3 | <%= profile_image_link profile, :portrait, 'div', | 3 | <%= profile_image_link profile, :portrait, 'div', |
4 | @filter == 'more_recent' ? profile.send(@filter + '_label') + show_date(profile.created_at) : profile.send(@filter + '_label') %> | 4 | @filter == 'more_recent' ? profile.send(@filter + '_label') + show_date(profile.created_at) : profile.send(@filter + '_label') %> |
5 | <% else %> | 5 | <% else %> |
test/unit/search_helper_test.rb
@@ -4,6 +4,28 @@ class SearchHelperTest < ActiveSupport::TestCase | @@ -4,6 +4,28 @@ class SearchHelperTest < ActiveSupport::TestCase | ||
4 | 4 | ||
5 | include SearchHelper | 5 | include SearchHelper |
6 | 6 | ||
7 | + should 'return whether on a multiple search' do | ||
8 | + stubs(:params).returns({:action => 'index', :display => 'map'}) | ||
9 | + @results = {:articles => [1,2], :products => [1,2]} | ||
10 | + assert multiple_search? | ||
11 | + | ||
12 | + stubs(:params).returns({:action => 'products', :display => 'map'}) | ||
13 | + @results = {:products => [1,2]} | ||
14 | + assert !multiple_search? | ||
15 | + end | ||
16 | + | ||
17 | + should 'return whether on a map search' do | ||
18 | + stubs(:params).returns({:action => 'index', :display => 'map'}) | ||
19 | + @results = {:articles => [1,2], :products => [1,2]} | ||
20 | + @query = '' | ||
21 | + assert !map_search? | ||
22 | + | ||
23 | + stubs(:params).returns({:action => 'products', :display => 'map'}) | ||
24 | + @results = {:products => [1,2]} | ||
25 | + @query = 'test' | ||
26 | + assert map_search? | ||
27 | + end | ||
28 | + | ||
7 | should 'display search page title' do | 29 | should 'display search page title' do |
8 | title = 'page_title' | 30 | title = 'page_title' |
9 | assert_equal search_page_title(title), '<h1>page_title</h1>' | 31 | assert_equal search_page_title(title), '<h1>page_title</h1>' |
@@ -35,6 +57,8 @@ class SearchHelperTest < ActiveSupport::TestCase | @@ -35,6 +57,8 @@ class SearchHelperTest < ActiveSupport::TestCase | ||
35 | 57 | ||
36 | should 'display results with map' do | 58 | should 'display results with map' do |
37 | stubs(:params).returns({:display => 'map'}) | 59 | stubs(:params).returns({:display => 'map'}) |
60 | + @query = 'test' | ||
61 | + @results = {:products => [1,2]} | ||
38 | expects('render').with({:partial => 'google_maps'}).returns('render_return') | 62 | expects('render').with({:partial => 'google_maps'}).returns('render_return') |
39 | expects('content_tag').with('div', 'render_return', :class => 'map-or-list-search-results map') | 63 | expects('content_tag').with('div', 'render_return', :class => 'map-or-list-search-results map') |
40 | display_results true | 64 | display_results true |
@@ -231,7 +255,6 @@ class SearchHelperTest < ActiveSupport::TestCase | @@ -231,7 +255,6 @@ class SearchHelperTest < ActiveSupport::TestCase | ||
231 | end | 255 | end |
232 | end | 256 | end |
233 | 257 | ||
234 | - | ||
235 | should 'return asset class from string' do | 258 | should 'return asset class from string' do |
236 | asset_names = ['products', 'events', 'articles', 'enterprises', 'people', 'communities'] | 259 | asset_names = ['products', 'events', 'articles', 'enterprises', 'people', 'communities'] |
237 | asset_classes = [Product, Event, Article, Enterprise, Person, Community] | 260 | asset_classes = [Product, Event, Article, Enterprise, Person, Community] |