Commit d3c8e3c4f3d4bbb7bf07bf9395d81837c8936592

Authored by Rodrigo Souto
1 parent 82fbfb86

[search-improvements] Refactor search page

app/controllers/public/search_controller.rb
@@ -20,7 +20,7 @@ class SearchController < PublicController @@ -20,7 +20,7 @@ class SearchController < PublicController
20 20
21 def index 21 def index
22 @searches = {} 22 @searches = {}
23 - @order = [] 23 + @assets = []
24 @names = {} 24 @names = {}
25 @results_only = true 25 @results_only = true
26 26
@@ -28,7 +28,7 @@ class SearchController < PublicController @@ -28,7 +28,7 @@ class SearchController < PublicController
28 load_query 28 load_query
29 @asset = key 29 @asset = key
30 send(key) 30 send(key)
31 - @order << key 31 + @assets << key
32 @names[key] = _(description) 32 @names[key] = _(description)
33 end 33 end
34 @asset = nil 34 @asset = nil
@@ -42,7 +42,7 @@ class SearchController &lt; PublicController @@ -42,7 +42,7 @@ class SearchController &lt; PublicController
42 # view the summary of one category 42 # view the summary of one category
43 def category_index 43 def category_index
44 @searches = {} 44 @searches = {}
45 - @order = [] 45 + @assets = []
46 @names = {} 46 @names = {}
47 limit = MULTIPLE_SEARCH_LIMIT 47 limit = MULTIPLE_SEARCH_LIMIT
48 [ 48 [
@@ -53,7 +53,7 @@ class SearchController &lt; PublicController @@ -53,7 +53,7 @@ class SearchController &lt; PublicController
53 [ :communities, _('Communities'), :recent_communities ], 53 [ :communities, _('Communities'), :recent_communities ],
54 [ :articles, _('Contents'), :recent_articles ] 54 [ :articles, _('Contents'), :recent_articles ]
55 ].each do |asset, name, filter| 55 ].each do |asset, name, filter|
56 - @order << asset 56 + @assets << asset
57 @searches[asset]= {:results => @category.send(filter, limit)} 57 @searches[asset]= {:results => @category.send(filter, limit)}
58 raise "No total_entries for: #{asset}" unless @searches[asset][:results].respond_to?(:total_entries) 58 raise "No total_entries for: #{asset}" unless @searches[asset][:results].respond_to?(:total_entries)
59 @names[asset] = name 59 @names[asset] = name
app/helpers/search_helper.rb
@@ -110,29 +110,26 @@ module SearchHelper @@ -110,29 +110,26 @@ module SearchHelper
110 end 110 end
111 111
112 def filters(asset) 112 def filters(asset)
  113 + return if !asset
113 klass = asset_class(asset) 114 klass = asset_class(asset)
114 content_tag('div', klass::SEARCH_FILTERS.map do |name, options| 115 content_tag('div', klass::SEARCH_FILTERS.map do |name, options|
115 select_filter(name, options) 116 select_filter(name, options)
116 end.join("\n"), :id => 'search-filters') 117 end.join("\n"), :id => 'search-filters')
117 end 118 end
  119 +
  120 + def assets_links(selected)
  121 + assets = SEARCHES.keys
  122 + content_tag('ul',
  123 + assets.map do |asset|
  124 + options = {}
  125 + options.merge!(:class => 'selected') if selected.to_s == asset.to_s
  126 + content_tag('li', asset_link(asset), options)
  127 + end.join("\n"),
  128 + :id => 'assets-links')
118 end 129 end
119 130
120 - def filter_title(asset, filter)  
121 - {  
122 - 'articles_more_recent' => _('More recent contents from network'),  
123 - 'articles_more_popular' => _('More viewed contents from network'),  
124 - 'articles_more_comments' => _('Most commented contents from network'),  
125 - 'people_more_recent' => _('More recent people from network'),  
126 - 'people_more_active' => _('More active people from network'),  
127 - 'people_more_popular' => _('More popular people from network'),  
128 - 'communities_more_recent' => _('More recent communities from network'),  
129 - 'communities_more_active' => _('More active communities from network'),  
130 - 'communities_more_popular' => _('More popular communities from network'),  
131 - 'enterprises_more_recent' => _('More recent enterprises from network'),  
132 - 'enterprises_more_active' => _('More active enterprises from network'),  
133 - 'enterprises_more_popular' => _('More popular enterprises from network'),  
134 - 'products_more_recent' => _('Highlights'),  
135 - }[asset.to_s + '_' + filter].to_s 131 + def asset_link(asset)
  132 + link_to(SEARCHES[asset], "/search/#{asset}")
136 end 133 end
137 134
138 end 135 end
app/views/search/_results_header.html.erb
@@ -1,6 +0,0 @@ @@ -1,6 +0,0 @@
1 -<div class="search-results-header <%= "search-no-results" if @searches[@asset].nil? or @searches[@asset].length == 0 %>">  
2 - <div id='search-filter-title'><%= filter_title(@asset, @filter) %></div>  
3 - <%= display_selector(@asset, params[:display]) %>  
4 - <%= filter_selector(@asset, @filter) %>  
5 - <div style="clear: both"></div>  
6 -</div>  
app/views/search/_search_form.html.erb
1 -<div class='search-form'>  
2 1
  2 +<div class='search-form'>
3 <%= form_tag( { :controller => 'search', :action => @asset ? @asset : 'index', :asset => nil, :category_path => ( @category ? @category.path : nil ) }, 3 <%= form_tag( { :controller => 'search', :action => @asset ? @asset : 'index', :asset => nil, :category_path => ( @category ? @category.path : nil ) },
4 :method => 'get', :class => 'search_form' ) do %> 4 :method => 'get', :class => 'search_form' ) do %>
5 -  
6 - <%= hidden_field_tag :display, params[:display] %>  
7 - <%= hidden_field_tag :filter, params[:filter] %> 5 + <div id='search-header'>
  6 + <%= assets_links(@asset) %>
  7 + <%= filters(@asset) %>
  8 + </div>
8 9
9 <div class="search-field"> 10 <div class="search-field">
10 <span class="formfield"> 11 <span class="formfield">
11 - <%= search_input_with_suggestions('query', @asset, @query, {:size => 50}) %>  
12 - <%= javascript_tag "jQuery('#search-input').attr('title', \"#{hint}\").hint()" if defined?(hint) %> 12 + <%= search_input_with_suggestions('query', @asset, @query, {:size => 50, :placeholder => hint}) %>
13 </span> 13 </span>
14 14
15 <%= submit_button(:search, _('Search')) %> 15 <%= submit_button(:search, _('Search')) %>
app/views/search/search_page.html.erb
1 -<%= search_page_title( @titles[@asset], @category ) %>  
2 1
3 <%= render :partial => 'search_form', :locals => { :hint => _("Type words about the %s you're looking for") % @asset.to_s.singularize } %> 2 <%= render :partial => 'search_form', :locals => { :hint => _("Type words about the %s you're looking for") % @asset.to_s.singularize } %>
4 -<%= render :partial => 'results_header' %>  
5 -  
6 <%= render :partial => 'search_content' %> 3 <%= render :partial => 'search_content' %>
7 4
8 <div style="clear: both"></div> 5 <div style="clear: both"></div>
public/javascripts/search.js
@@ -20,10 +20,17 @@ @@ -20,10 +20,17 @@
20 return false; 20 return false;
21 }); 21 });
22 22
23 - $("input#search-input").typeWatch({  
24 - callback: function (value) {$('form.search_form').submit()},  
25 - wait: 750,  
26 - highlight: true,  
27 - captureLength: 2 23 + // Assets links
  24 + $('#assets-links a').click(function(e){
  25 + e.preventDefault();
  26 + window.location.href = $(this).attr("href") + '?query=' + $('#search-input').val();
28 }); 27 });
  28 +
  29 + // Real time search
  30 + // $("input#search-input").typeWatch({
  31 + // callback: function (value) {$('form.search_form').submit()},
  32 + // wait: 750,
  33 + // highlight: true,
  34 + // captureLength: 2
  35 + // });
29 })(jQuery); 36 })(jQuery);
test/functional/search_controller_test.rb
@@ -283,7 +283,7 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -283,7 +283,7 @@ class SearchControllerTest &lt; ActionController::TestCase
283 should 'search all enabled assets in general search' do 283 should 'search all enabled assets in general search' do
284 ent1 = create_profile_with_optional_category(Enterprise, 'test enterprise') 284 ent1 = create_profile_with_optional_category(Enterprise, 'test enterprise')
285 prod_cat = create(ProductCategory, :name => 'pctest', :environment => Environment.default) 285 prod_cat = create(ProductCategory, :name => 'pctest', :environment => Environment.default)
286 - prod = ent1.products.create!(:name => 'test product', :product_category => prod_cat) 286 + prod = create(Product,:name => 'test product', :product_category => prod_cat, :enterprise => ent1)
287 art = create(Article, :name => 'test article', :profile_id => fast_create(Person).id) 287 art = create(Article, :name => 'test article', :profile_id => fast_create(Person).id)
288 per = create(Person, :name => 'test person', :identifier => 'test-person', :user_id => fast_create(User).id) 288 per = create(Person, :name => 'test person', :identifier => 'test-person', :user_id => fast_create(User).id)
289 com = create(Community, :name => 'test community') 289 com = create(Community, :name => 'test community')
@@ -301,7 +301,7 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -301,7 +301,7 @@ class SearchControllerTest &lt; ActionController::TestCase
301 should 'display category image while in directory' do 301 should 'display category image while in directory' do
302 parent = Category.create!(:name => 'category1', :environment => Environment.default) 302 parent = Category.create!(:name => 'category1', :environment => Environment.default)
303 cat = Category.create!(:name => 'category2', :environment => Environment.default, :parent_id => parent.id, 303 cat = Category.create!(:name => 'category2', :environment => Environment.default, :parent_id => parent.id,
304 - :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')} 304 + :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}
305 ) 305 )
306 306
307 process_delayed_job_queue 307 process_delayed_job_queue
@@ -447,11 +447,6 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -447,11 +447,6 @@ class SearchControllerTest &lt; ActionController::TestCase
447 assert_tag :tag => 'div', :attributes => {:class => /search-results-articles/} , :descendant => {:tag => 'a', :attributes => { :href => '/search/articles/my-category'}} 447 assert_tag :tag => 'div', :attributes => {:class => /search-results-articles/} , :descendant => {:tag => 'a', :attributes => { :href => '/search/articles/my-category'}}
448 end 448 end
449 449
450 - should 'display correct title on list communities' do  
451 - get :communities  
452 - assert_tag :tag => 'h1', :content => 'Communities'  
453 - end  
454 -  
455 should 'indicate more than the page limit for total_entries' do 450 should 'indicate more than the page limit for total_entries' do
456 Enterprise.destroy_all 451 Enterprise.destroy_all
457 ('1'..'20').each do |n| 452 ('1'..'20').each do |n|