Commit f25bd3ac1aecb661584ea5b6b1c6646bb09545a6
1 parent
607528df
Exists in
master
and in
29 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 | 250 | end |
| 251 | 251 | |
| 252 | 252 | def limit |
| 253 | - searching = @searching.values.select{ |v| v } | |
| 254 | - if params[:display] == 'map' | |
| 253 | + if map_search? | |
| 255 | 254 | MAP_SEARCH_LIMIT |
| 256 | - elsif searching.size <= 1 | |
| 255 | + elsif !multiple_search? | |
| 257 | 256 | if [:people, :communities].include? @asset |
| 258 | 257 | BLOCKS_SEARCH_LIMIT |
| 259 | 258 | elsif @asset == :enterprises and @empty_query |
| ... | ... | @@ -267,31 +266,34 @@ class SearchController < PublicController |
| 267 | 266 | end |
| 268 | 267 | |
| 269 | 268 | def paginate_options(page = params[:page]) |
| 269 | + page = 1 if multiple_search? or params[:display] == 'map' | |
| 270 | 270 | { :per_page => limit, :page => page } |
| 271 | 271 | end |
| 272 | 272 | |
| 273 | 273 | def full_text_search(filters = [], options = {}) |
| 274 | 274 | paginate_options = paginate_options(params[:page]) |
| 275 | 275 | asset_class = asset_class(@asset) |
| 276 | - | |
| 277 | 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 | 297 | end |
| 296 | 298 | end |
| 297 | 299 | ... | ... |
app/helpers/search_helper.rb
| ... | ... | @@ -45,6 +45,14 @@ module SearchHelper |
| 45 | 45 | # FIXME remove it after search_controler refactored |
| 46 | 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 | 56 | def search_page_title(title, category = nil) |
| 49 | 57 | title = "<h1>" + title |
| 50 | 58 | title += '<small>' + category.name + '</small>' if category |
| ... | ... | @@ -58,8 +66,8 @@ module SearchHelper |
| 58 | 66 | :align => 'center', :class => 'search-category-context') if category |
| 59 | 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 | 71 | partial = 'google_maps' |
| 64 | 72 | klass = 'map' |
| 65 | 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 | 2 | <% @order.each do |name| %> |
| 3 | 3 | <% results = @results[name] %> |
| 4 | 4 | <% empty = results.nil? || results.empty? %> |
| ... | ... | @@ -7,7 +7,7 @@ |
| 7 | 7 | <% if not empty %> |
| 8 | 8 | <% partial = partial_for_class(results.first.class.class_name.constantize) %> |
| 9 | 9 | |
| 10 | - <% if @results.size > 1 %> | |
| 10 | + <% if multiple_search? %> | |
| 11 | 11 | <h3><%= @names[name] %></h3> |
| 12 | 12 | <% if results.total_entries > SearchController::MULTIPLE_SEARCH_LIMIT %> |
| 13 | 13 | <%= link_to(_('see all (%d)') % results.total_entries, params.merge(:action => name), :class => 'see-more' ) %> |
| ... | ... | @@ -22,9 +22,10 @@ |
| 22 | 22 | </ul> |
| 23 | 23 | </div> |
| 24 | 24 | <% else %> |
| 25 | - <% if @results.size > 1 %> | |
| 25 | + <% if multiple_search? %> | |
| 26 | 26 | <h3><%= @names[name] %></h3> |
| 27 | 27 | <% end %> |
| 28 | + | |
| 28 | 29 | <div class="search-results-innerbox search-results-type-empty"> |
| 29 | 30 | <div> <%= _('None') %> </div> |
| 30 | 31 | </div> | ... | ... |
app/views/search/_profile.rhtml
| 1 | 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 | 3 | <%= profile_image_link profile, :portrait, 'div', |
| 4 | 4 | @filter == 'more_recent' ? profile.send(@filter + '_label') + show_date(profile.created_at) : profile.send(@filter + '_label') %> |
| 5 | 5 | <% else %> | ... | ... |
test/unit/search_helper_test.rb
| ... | ... | @@ -4,6 +4,28 @@ class SearchHelperTest < ActiveSupport::TestCase |
| 4 | 4 | |
| 5 | 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 | 29 | should 'display search page title' do |
| 8 | 30 | title = 'page_title' |
| 9 | 31 | assert_equal search_page_title(title), '<h1>page_title</h1>' |
| ... | ... | @@ -35,6 +57,8 @@ class SearchHelperTest < ActiveSupport::TestCase |
| 35 | 57 | |
| 36 | 58 | should 'display results with map' do |
| 37 | 59 | stubs(:params).returns({:display => 'map'}) |
| 60 | + @query = 'test' | |
| 61 | + @results = {:products => [1,2]} | |
| 38 | 62 | expects('render').with({:partial => 'google_maps'}).returns('render_return') |
| 39 | 63 | expects('content_tag').with('div', 'render_return', :class => 'map-or-list-search-results map') |
| 40 | 64 | display_results true |
| ... | ... | @@ -231,7 +255,6 @@ class SearchHelperTest < ActiveSupport::TestCase |
| 231 | 255 | end |
| 232 | 256 | end |
| 233 | 257 | |
| 234 | - | |
| 235 | 258 | should 'return asset class from string' do |
| 236 | 259 | asset_names = ['products', 'events', 'articles', 'enterprises', 'people', 'communities'] |
| 237 | 260 | asset_classes = [Product, Event, Article, Enterprise, Person, Community] | ... | ... |