Commit 1bac841f2b52362c62447df68afa356b768da61c
1 parent
755f9431
Exists in
master
and in
28 other branches
Fixing search controller tests
Showing
8 changed files
with
49 additions
and
39 deletions
Show diff stats
app/controllers/public/search_controller.rb
... | ... | @@ -33,7 +33,10 @@ class SearchController < PublicController |
33 | 33 | end |
34 | 34 | @asset = nil |
35 | 35 | |
36 | - render :action => @searches.keys.first if @searches.keys.size == 1 | |
36 | + if @searches.keys.size == 1 | |
37 | + @asset = @searches.keys.first | |
38 | + render :action => @asset | |
39 | + end | |
37 | 40 | end |
38 | 41 | |
39 | 42 | # view the summary of one category |
... | ... | @@ -170,7 +173,7 @@ class SearchController < PublicController |
170 | 173 | return |
171 | 174 | end |
172 | 175 | |
173 | - @enabled_searches = SEARCHES.select {|key, name| environment.disabled?("disable_asset_#{params[:action]}") } | |
176 | + @enabled_searches = SEARCHES.select {|key, name| environment.disabled?("disable_asset_#{key}") } | |
174 | 177 | @searching = {} |
175 | 178 | @titles = {} |
176 | 179 | @enabled_searches.each do |key, name| | ... | ... |
app/helpers/search_helper.rb
... | ... | @@ -14,17 +14,6 @@ module SearchHelper |
14 | 14 | :events, _('Events'), |
15 | 15 | ] |
16 | 16 | |
17 | - VALID_DISPLAYS = %w[full compatct map] | |
18 | - | |
19 | - DEFAULT_DISPLAY = { | |
20 | - :articles => 'full', | |
21 | - :communities => 'compact', | |
22 | - :enterprises => 'compact', | |
23 | - :events => 'full', | |
24 | - :people => 'compact', | |
25 | - :products => 'full', | |
26 | - } | |
27 | - | |
28 | 17 | FILTER_TRANSLATION = { |
29 | 18 | 'more_popular' => _('More popular'), |
30 | 19 | 'more_active' => _('More active'), |
... | ... | @@ -60,20 +49,12 @@ module SearchHelper |
60 | 49 | :align => 'center', :class => 'search-category-context') if category |
61 | 50 | end |
62 | 51 | |
63 | - def display_map?(asset) | |
64 | - [:enterprises, :products].include?(asset) | |
65 | - end | |
66 | - | |
67 | - def display_compact?(asset) | |
68 | - [:communities, :enterprises, :people].include?(asset) | |
69 | - end | |
70 | - | |
71 | - def display_full?(asset) | |
72 | - [:articles, :enterprises, :events, :products].include?(asset) | |
52 | + def display?(asset, mode) | |
53 | + defined?(asset_class(asset)::SEARCH_DISPLAYS) && asset_class(asset)::SEARCH_DISPLAYS.include?(mode.to_s) | |
73 | 54 | end |
74 | 55 | |
75 | 56 | def display_results(searches=nil, asset=nil) |
76 | - if display_map?(asset) && map_search?(searches) | |
57 | + if display?(asset, :map) && map_search?(searches) | |
77 | 58 | partial = 'google_maps' |
78 | 59 | klass = 'map' |
79 | 60 | else |
... | ... | @@ -84,11 +65,12 @@ module SearchHelper |
84 | 65 | content_tag('div', render(:partial => partial), :class => "map-or-list-search-results #{klass}") |
85 | 66 | end |
86 | 67 | |
87 | - def display_filter(asset, display) | |
88 | - if VALID_DISPLAYS.include?(display) && send("display_#{display}?", asset) | |
68 | + def display_filter(item, display) | |
69 | + asset = item.class.name.downcase.pluralize.to_sym | |
70 | + if display?(asset, display) | |
89 | 71 | display |
90 | 72 | else |
91 | - DEFAULT_DISPLAY[asset] | |
73 | + item.default_search_display | |
92 | 74 | end |
93 | 75 | end |
94 | 76 | |
... | ... | @@ -106,10 +88,10 @@ module SearchHelper |
106 | 88 | end |
107 | 89 | |
108 | 90 | def display_selector(asset, display, float = 'right') |
109 | - if [display_map?(asset), display_compact?(asset), display_full?(asset)].select {|option| option}.count > 1 | |
110 | - compact_link = display_compact?(asset) ? (display == 'compact' ? _('Compact') : link_to(_('Compact'), params.merge(:display => 'compact'))) : nil | |
111 | - map_link = display_map?(asset) ? (display == 'map' ? _('Map') : link_to(_('Map'), params.merge(:display => 'map'))) : nil | |
112 | - full_link = display_full?(asset) ? (display == 'full' ? _('Full') : link_to(_('Full'), params.merge(:display => 'full'))) : nil | |
91 | + if [display?(asset, :map), display?(asset, :compact), display?(asset, :full)].select {|option| option}.count > 1 | |
92 | + compact_link = display?(asset, :compact) ? (display == 'compact' ? _('Compact') : link_to(_('Compact'), params.merge(:display => 'compact'))) : nil | |
93 | + map_link = display?(asset, :map) ? (display == 'map' ? _('Map') : link_to(_('Map'), params.merge(:display => 'map'))) : nil | |
94 | + full_link = display?(asset, :full) ? (display == 'full' ? _('Full') : link_to(_('Full'), params.merge(:display => 'full'))) : nil | |
113 | 95 | content_tag('div', |
114 | 96 | content_tag('strong', _('Display')) + ': ' + [compact_link, map_link, full_link].compact.join(' | '), |
115 | 97 | :class => 'search-customize-options' | ... | ... |
app/models/article.rb
... | ... | @@ -16,6 +16,12 @@ class Article < ActiveRecord::Base |
16 | 16 | more_comments |
17 | 17 | ] |
18 | 18 | |
19 | + SEARCH_DISPLAYS = %w[full] | |
20 | + | |
21 | + def default_search_display | |
22 | + 'full' | |
23 | + end | |
24 | + | |
19 | 25 | track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead, :first_image], :if => Proc.new { |a| a.is_trackable? && !a.image? } |
20 | 26 | |
21 | 27 | # xss_terminate plugin can't sanitize array fields | ... | ... |
app/models/enterprise.rb
app/models/product.rb
... | ... | @@ -9,6 +9,12 @@ class Product < ActiveRecord::Base |
9 | 9 | more_recent |
10 | 10 | ] |
11 | 11 | |
12 | + SEARCH_DISPLAYS = %w[map full] | |
13 | + | |
14 | + def default_search_display | |
15 | + 'full' | |
16 | + end | |
17 | + | |
12 | 18 | belongs_to :enterprise |
13 | 19 | has_one :region, :through => :enterprise |
14 | 20 | validates_presence_of :enterprise | ... | ... |
app/models/profile.rb
app/views/search/_display_results.rhtml
... | ... | @@ -12,11 +12,11 @@ |
12 | 12 | <% end %> |
13 | 13 | <% end %> |
14 | 14 | |
15 | - <% display = display_filter(name, params[:display]) %> | |
16 | 15 | |
17 | 16 | <div class="search-results-innerbox search-results-type-<%= name.to_s.singularize %> <%= 'common-profile-list-block' if [:enterprises, :people, :communities].include?(name) %>"> |
18 | 17 | <ul> |
19 | 18 | <% search[:results].each do |hit| %> |
19 | + <% display = display_filter(hit, params[:display]) %> | |
20 | 20 | <% partial = partial_for_class(hit.class, display) %> |
21 | 21 | <% variable_name = partial.gsub("#{display}_", '').to_sym %> |
22 | 22 | <%= render :partial => partial, :locals => {variable_name => hit} %> | ... | ... |
test/functional/search_controller_test.rb
... | ... | @@ -355,17 +355,22 @@ class SearchControllerTest < ActionController::TestCase |
355 | 355 | # end |
356 | 356 | |
357 | 357 | should 'render specific action when only one asset is enabled' do |
358 | - # initialize @controller.environment | |
359 | - get :index | |
360 | - | |
358 | + environment = Environment.default | |
361 | 359 | # article is not disabled |
362 | 360 | [:enterprises, :people, :communities, :products, :events].select do |key, name| |
363 | - @controller.environment.enable('disable_asset_' + key.to_s) | |
361 | + environment.enable('disable_asset_' + key.to_s) | |
364 | 362 | end |
365 | - | |
366 | - @controller.expects(:articles) | |
363 | + environment.save! | |
364 | + @controller.stubs(:environment).returns(environment) | |
367 | 365 | |
368 | 366 | get :index, :query => 'something' |
367 | + | |
368 | + assert assigns(:searches).has_key?(:articles) | |
369 | + assert !assigns(:searches).has_key?(:enterprises) | |
370 | + assert !assigns(:searches).has_key?(:people) | |
371 | + assert !assigns(:searches).has_key?(:communities) | |
372 | + assert !assigns(:searches).has_key?(:products) | |
373 | + assert !assigns(:searches).has_key?(:events) | |
369 | 374 | end |
370 | 375 | |
371 | 376 | should 'search all enabled assets in general search' do |
... | ... | @@ -399,7 +404,7 @@ class SearchControllerTest < ActionController::TestCase |
399 | 404 | |
400 | 405 | should 'search for events' do |
401 | 406 | person = create_user('teste').person |
402 | - event = create_event(person, :name => 'an event to be found') | |
407 | + event = create_event(person, :name => 'an event to be found', :start_date => Date.today) | |
403 | 408 | |
404 | 409 | get :events, :query => 'event to be found' |
405 | 410 | ... | ... |