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,7 +33,10 @@ class SearchController < PublicController | ||
33 | end | 33 | end |
34 | @asset = nil | 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 | end | 40 | end |
38 | 41 | ||
39 | # view the summary of one category | 42 | # view the summary of one category |
@@ -170,7 +173,7 @@ class SearchController < PublicController | @@ -170,7 +173,7 @@ class SearchController < PublicController | ||
170 | return | 173 | return |
171 | end | 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 | @searching = {} | 177 | @searching = {} |
175 | @titles = {} | 178 | @titles = {} |
176 | @enabled_searches.each do |key, name| | 179 | @enabled_searches.each do |key, name| |
app/helpers/search_helper.rb
@@ -14,17 +14,6 @@ module SearchHelper | @@ -14,17 +14,6 @@ module SearchHelper | ||
14 | :events, _('Events'), | 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 | FILTER_TRANSLATION = { | 17 | FILTER_TRANSLATION = { |
29 | 'more_popular' => _('More popular'), | 18 | 'more_popular' => _('More popular'), |
30 | 'more_active' => _('More active'), | 19 | 'more_active' => _('More active'), |
@@ -60,20 +49,12 @@ module SearchHelper | @@ -60,20 +49,12 @@ module SearchHelper | ||
60 | :align => 'center', :class => 'search-category-context') if category | 49 | :align => 'center', :class => 'search-category-context') if category |
61 | end | 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 | end | 54 | end |
74 | 55 | ||
75 | def display_results(searches=nil, asset=nil) | 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 | partial = 'google_maps' | 58 | partial = 'google_maps' |
78 | klass = 'map' | 59 | klass = 'map' |
79 | else | 60 | else |
@@ -84,11 +65,12 @@ module SearchHelper | @@ -84,11 +65,12 @@ module SearchHelper | ||
84 | content_tag('div', render(:partial => partial), :class => "map-or-list-search-results #{klass}") | 65 | content_tag('div', render(:partial => partial), :class => "map-or-list-search-results #{klass}") |
85 | end | 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 | display | 71 | display |
90 | else | 72 | else |
91 | - DEFAULT_DISPLAY[asset] | 73 | + item.default_search_display |
92 | end | 74 | end |
93 | end | 75 | end |
94 | 76 | ||
@@ -106,10 +88,10 @@ module SearchHelper | @@ -106,10 +88,10 @@ module SearchHelper | ||
106 | end | 88 | end |
107 | 89 | ||
108 | def display_selector(asset, display, float = 'right') | 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 | content_tag('div', | 95 | content_tag('div', |
114 | content_tag('strong', _('Display')) + ': ' + [compact_link, map_link, full_link].compact.join(' | '), | 96 | content_tag('strong', _('Display')) + ': ' + [compact_link, map_link, full_link].compact.join(' | '), |
115 | :class => 'search-customize-options' | 97 | :class => 'search-customize-options' |
app/models/article.rb
@@ -16,6 +16,12 @@ class Article < ActiveRecord::Base | @@ -16,6 +16,12 @@ class Article < ActiveRecord::Base | ||
16 | more_comments | 16 | more_comments |
17 | ] | 17 | ] |
18 | 18 | ||
19 | + SEARCH_DISPLAYS = %w[full] | ||
20 | + | ||
21 | + def default_search_display | ||
22 | + 'full' | ||
23 | + end | ||
24 | + | ||
19 | track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead, :first_image], :if => Proc.new { |a| a.is_trackable? && !a.image? } | 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 | # xss_terminate plugin can't sanitize array fields | 27 | # xss_terminate plugin can't sanitize array fields |
app/models/enterprise.rb
@@ -2,6 +2,8 @@ | @@ -2,6 +2,8 @@ | ||
2 | # only enterprises can offer products and services. | 2 | # only enterprises can offer products and services. |
3 | class Enterprise < Organization | 3 | class Enterprise < Organization |
4 | 4 | ||
5 | + SEARCH_DISPLAYS += %w[map full] | ||
6 | + | ||
5 | def self.type_name | 7 | def self.type_name |
6 | _('Enterprise') | 8 | _('Enterprise') |
7 | end | 9 | end |
app/models/product.rb
@@ -9,6 +9,12 @@ class Product < ActiveRecord::Base | @@ -9,6 +9,12 @@ class Product < ActiveRecord::Base | ||
9 | more_recent | 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 | belongs_to :enterprise | 18 | belongs_to :enterprise |
13 | has_one :region, :through => :enterprise | 19 | has_one :region, :through => :enterprise |
14 | validates_presence_of :enterprise | 20 | validates_presence_of :enterprise |
app/models/profile.rb
@@ -13,6 +13,12 @@ class Profile < ActiveRecord::Base | @@ -13,6 +13,12 @@ class Profile < ActiveRecord::Base | ||
13 | more_recent | 13 | more_recent |
14 | ] | 14 | ] |
15 | 15 | ||
16 | + SEARCH_DISPLAYS = %w[compact] | ||
17 | + | ||
18 | + def default_search_display | ||
19 | + 'compact' | ||
20 | + end | ||
21 | + | ||
16 | module Roles | 22 | module Roles |
17 | def self.admin(env_id) | 23 | def self.admin(env_id) |
18 | find_role('admin', env_id) | 24 | find_role('admin', env_id) |
app/views/search/_display_results.rhtml
@@ -12,11 +12,11 @@ | @@ -12,11 +12,11 @@ | ||
12 | <% end %> | 12 | <% end %> |
13 | <% end %> | 13 | <% end %> |
14 | 14 | ||
15 | - <% display = display_filter(name, params[:display]) %> | ||
16 | 15 | ||
17 | <div class="search-results-innerbox search-results-type-<%= name.to_s.singularize %> <%= 'common-profile-list-block' if [:enterprises, :people, :communities].include?(name) %>"> | 16 | <div class="search-results-innerbox search-results-type-<%= name.to_s.singularize %> <%= 'common-profile-list-block' if [:enterprises, :people, :communities].include?(name) %>"> |
18 | <ul> | 17 | <ul> |
19 | <% search[:results].each do |hit| %> | 18 | <% search[:results].each do |hit| %> |
19 | + <% display = display_filter(hit, params[:display]) %> | ||
20 | <% partial = partial_for_class(hit.class, display) %> | 20 | <% partial = partial_for_class(hit.class, display) %> |
21 | <% variable_name = partial.gsub("#{display}_", '').to_sym %> | 21 | <% variable_name = partial.gsub("#{display}_", '').to_sym %> |
22 | <%= render :partial => partial, :locals => {variable_name => hit} %> | 22 | <%= render :partial => partial, :locals => {variable_name => hit} %> |
test/functional/search_controller_test.rb
@@ -355,17 +355,22 @@ class SearchControllerTest < ActionController::TestCase | @@ -355,17 +355,22 @@ class SearchControllerTest < ActionController::TestCase | ||
355 | # end | 355 | # end |
356 | 356 | ||
357 | should 'render specific action when only one asset is enabled' do | 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 | # article is not disabled | 359 | # article is not disabled |
362 | [:enterprises, :people, :communities, :products, :events].select do |key, name| | 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 | end | 362 | end |
365 | - | ||
366 | - @controller.expects(:articles) | 363 | + environment.save! |
364 | + @controller.stubs(:environment).returns(environment) | ||
367 | 365 | ||
368 | get :index, :query => 'something' | 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 | end | 374 | end |
370 | 375 | ||
371 | should 'search all enabled assets in general search' do | 376 | should 'search all enabled assets in general search' do |
@@ -399,7 +404,7 @@ class SearchControllerTest < ActionController::TestCase | @@ -399,7 +404,7 @@ class SearchControllerTest < ActionController::TestCase | ||
399 | 404 | ||
400 | should 'search for events' do | 405 | should 'search for events' do |
401 | person = create_user('teste').person | 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 | get :events, :query => 'event to be found' | 409 | get :events, :query => 'event to be found' |
405 | 410 |