Commit 2cb08de61c7c6fb1f0df3976df906e64af4f4a3c

Authored by MoisesMachado
1 parent 8378203d

ActionItem160: search results divided into articles, communities, products, people and enterprises


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1471 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/search_controller.rb
@@ -2,35 +2,10 @@ class SearchController < ApplicationController @@ -2,35 +2,10 @@ class SearchController < ApplicationController
2 2
3 helper TagsHelper 3 helper TagsHelper
4 4
5 - SEARCHES = []  
6 -  
7 - def self.search(&block)  
8 - SEARCHES << block  
9 - end  
10 -  
11 protected 5 protected
12 6
13 - #############################################  
14 - # XXX add yours searches here  
15 - #############################################  
16 -  
17 - search do |query|  
18 - Article.find_by_contents(query)  
19 - end  
20 -  
21 - search do |query|  
22 - Profile.find_by_contents(query)  
23 - end  
24 -  
25 - search do |query|  
26 - Product.find_by_contents(query)  
27 - end  
28 -  
29 - # auxiliary method to search in all defined searches and collect the results  
30 - def search(query)  
31 - SEARCHES.inject([]) do |acc,finder|  
32 - acc += finder.call(query)  
33 - end.sort_by do |hit| 7 + def search(klass, query)
  8 + klass.find_by_contents(query).sort_by do |hit|
34 -(relevance_for(hit)) 9 -(relevance_for(hit))
35 end 10 end
36 end 11 end
@@ -42,7 +17,8 @@ class SearchController &lt; ApplicationController @@ -42,7 +17,8 @@ class SearchController &lt; ApplicationController
42 def index 17 def index
43 @query = params[:query] || '' 18 @query = params[:query] || ''
44 @filtered_query = remove_stop_words(@query) 19 @filtered_query = remove_stop_words(@query)
45 - @results = search(@filtered_query) 20 + @articles, @people, @enterprises, @communities, @products =
  21 + [Article, Person, Enterprise, Community, Product].map{ |klass| search(klass, @query) }
46 end 22 end
47 23
48 def tags 24 def tags
app/views/search/_article.rhtml
1 -<div>  
2 - <%= icon('cms', :style => 'float: left') %>  
3 <div class='search-result-text'> 1 <div class='search-result-text'>
4 <div> 2 <div>
5 <strong> 3 <strong>
@@ -10,5 +8,4 @@ @@ -10,5 +8,4 @@
10 <%= strip_tags(hit.abstract) %> 8 <%= strip_tags(hit.abstract) %>
11 </div> 9 </div>
12 </div> 10 </div>
13 -</div>  
14 11
app/views/search/_product.rhtml
1 -<%# FIXME add photo if available %>  
2 <%# FIXME add more information %> 1 <%# FIXME add more information %>
3 2
4 -<div>  
5 - <%= icon('product', :style => 'float: left') %>  
6 <div class='search-result-text'> 3 <div class='search-result-text'>
7 - <%= image_tag(hit.image.public_filename(:minor), :style => 'float:right;') if hit.image %>  
8 - <strong> 4 + <%= image_tag(hit.image.public_filename(:minor)) if hit.image %>
  5 + <strong>
9 <%= link_to_product(hit) %> 6 <%= link_to_product(hit) %>
10 - </strong> <br/>  
11 - <%= _('Price: %d') % hit.price %> <br>  
12 - <%= _('Suplier: %s') % link_to_homepage(hit.enterprise.name, hit.enterprise.identifier) if hit.enterprise %> <br>  
13 - <%= _('Category: %s') % link_to_category(hit.product_category) %> 7 + </strong>
  8 + <ul>
  9 + <li> <%= _('Price: %d') % hit.price %> </li>
  10 + <% if hit.enterprise %>
  11 + <li> <%= _('Suplier: %s') % link_to_homepage(hit.enterprise.name, hit.enterprise.identifier) %> </li>
  12 + <% end %>
  13 + <li> <%= _('Category: %s') % link_to_category(hit.product_category) %> </li>
  14 + </ul>
14 </div> 15 </div>
15 -</div>  
app/views/search/_profile.rhtml
1 -<%# FIXME add photo if available %>  
2 <%# FIXME add more information %> 1 <%# FIXME add more information %>
3 2
4 -<div>  
5 - <%= icon('person', :style => 'float: left') %>  
6 - <div class='search-result-text'>  
7 - <strong>  
8 - <%= link_to_homepage(hit.name, hit.identifier) %>  
9 - </strong>  
10 - </div> 3 +<div class='search-result-text'>
  4 + <%= image_tag(hit.image.public_filename(:minor)) if hit.image %>
  5 + <strong>
  6 + <%= link_to_homepage(hit.name, hit.identifier) %>
  7 + </strong>
11 </div> 8 </div>
app/views/search/index.rhtml
1 <h2> <%= _('Search results for "%s"') % @query %> </h2> 1 <h2> <%= _('Search results for "%s"') % @query %> </h2>
2 2
3 -<% @results.each do |hit| %>  
4 - <%= render :partial => partial_for_class(hit.class), :locals => { :hit => hit } %>  
5 - <br style='clear: left'/> 3 +<% [ [@articles, _('Articles'), 'articles'],
  4 + [@people, _('People'), 'people'],
  5 + [@enterprises, _('Entreprises'), 'enterprises'],
  6 + [@communities, _('Communities'), 'communities'],
  7 + [@products, _('Products'), 'products'] ].each do |results, iname, name| %>
  8 + <% if results && !results.empty? %>
  9 + <div id="search-result-<%= name %>">
  10 + <h3> <%= iname %> </h3>
  11 + <% results.each do |hit| %>
  12 + <%= render :partial => partial_for_class(hit.class), :locals => { :hit => hit } %>
  13 + <% end %>
  14 + </div>
  15 + <% end %>
6 <% end %> 16 <% end %>
test/functional/search_controller_test.rb
@@ -16,9 +16,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -16,9 +16,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
16 get 'index', :query => 'teste' 16 get 'index', :query => 'teste'
17 assert_response :success 17 assert_response :success
18 assert_template 'index' 18 assert_template 'index'
19 - assert assigns('results')  
20 - assert assigns('results').include?(ent)  
21 - 19 + assert assigns('enterprises')
  20 + assert assigns('enterprises').include?(ent)
22 end 21 end
23 22
24 should 'filter stop words' do 23 should 'filter stop words' do