Commit 6f0362a9b2290c6ee419e8050b3994ff4831d693

Authored by MoisesMachado
1 parent a5313c2a

ActionItem492: added the product box in the category page, the possibility of ma…

…ntain the order of the blocks and fixed the total_entries in the see all... sub tab


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2156 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/search_controller.rb
@@ -149,9 +149,11 @@ class SearchController < ApplicationController @@ -149,9 +149,11 @@ class SearchController < ApplicationController
149 number_of_result_assets = @searching.values.select{|v| v}.size 149 number_of_result_assets = @searching.values.select{|v| v}.size
150 150
151 @results = {} 151 @results = {}
  152 + @order = []
152 @names = {} 153 @names = {}
153 154
154 SEARCH_IN.select { |key,description| @searching[key] }.each do |key, description| 155 SEARCH_IN.select { |key,description| @searching[key] }.each do |key, description|
  156 + @order << key
155 @results[key] = @finder.find(key, @filtered_query, calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius])) 157 @results[key] = @finder.find(key, @filtered_query, calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius]))
156 @names[key] = gettext(description) 158 @names[key] = gettext(description)
157 end 159 end
@@ -176,15 +178,18 @@ class SearchController &lt; ApplicationController @@ -176,15 +178,18 @@ class SearchController &lt; ApplicationController
176 # view the summary of one category 178 # view the summary of one category
177 def category_index 179 def category_index
178 @results = {} 180 @results = {}
  181 + @order = []
179 @names = {} 182 @names = {}
180 [ 183 [
181 - [ :people, _('Newer people'), @finder.recent('people') ],  
182 - [ :communities, _('Newer communities'), @finder.recent('communities') ],  
183 - [ :articles, _('Newer articles'), @finder.recent('articles') ],  
184 - [ :most_commented_articles, _('Most commented articles'), @finder.most_commented_articles ],  
185 - [ :enterprises, _('Newer enterprises'), @finder.recent('enterprises') ],  
186 - [ :events, _('Near events TODO'), @finder.current_events(params[:year], params[:month]) ] 184 + [ :people, _('Newer people'), @finder.recent('people', limit) ],
  185 + [ :enterprises, _('Newer enterprises'), @finder.recent('enterprises', limit) ],
  186 + [ :products, ('Newer products'), @finder.recent('products', limit) ],
  187 + [ :events, _('Near events TODO'), @finder.current_events(params[:year], params[:month], {:per_page => limit}) ],
  188 + [ :communities, _('Newer communities'), @finder.recent('communities', limit) ],
  189 + [ :articles, _('Newer articles'), @finder.recent('articles', limit) ],
  190 + [ :most_commented_articles, _('Most commented articles'), @finder.most_commented_articles(limit) ]
187 ].each do |key, name, list| 191 ].each do |key, name, list|
  192 + @order << key
188 @results[key] = list 193 @results[key] = list
189 @names[key] = name 194 @names[key] = name
190 end 195 end
app/models/category_finder.rb
@@ -34,14 +34,16 @@ class CategoryFinder @@ -34,14 +34,16 @@ class CategoryFinder
34 find(asset, query, options).total_entries 34 find(asset, query, options).total_entries
35 end 35 end
36 36
37 - def most_commented_articles(limit=10)  
38 - Article.find(:all, options_for_find(Article, :limit => limit, :order => 'comments_count DESC')) 37 + def most_commented_articles(limit=10, options={})
  38 + options = {:page => 1, :per_page => limit, :order => 'comments_count DESC'}.merge(options)
  39 + Article.paginate(:all, options_for_find(Article, options))
39 end 40 end
40 41
41 - def current_events(year, month) 42 + def current_events(year, month, options={})
  43 + options = {:page => 1}.merge(options)
42 range = Event.date_range(year, month) 44 range = Event.date_range(year, month)
43 45
44 - Event.find(:all, :include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }) 46 + Event.paginate(:all, {:include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }}.merge(options))
45 end 47 end
46 48
47 protected 49 protected
app/views/search/_display_results.rhtml
@@ -4,7 +4,8 @@ @@ -4,7 +4,8 @@
4 pos2 = :odd # allow to format in a two columns layout 4 pos2 = :odd # allow to format in a two columns layout
5 pos3 = 3 # allow to format in a thre columns layout 5 pos3 = 3 # allow to format in a thre columns layout
6 %> 6 %>
7 -<% @results.each do |name,results| %> 7 +<% @order.each do |name| %>
  8 + <% results = @results[name] %>
8 <% 9 <%
9 pos3 += 1; pos3 = 1 if pos3 > 3 10 pos3 += 1; pos3 = 1 if pos3 > 3
10 pos2==:odd ? pos2=:even : pos2=:odd 11 pos2==:odd ? pos2=:even : pos2=:odd
@@ -17,7 +18,7 @@ @@ -17,7 +18,7 @@
17 <%= @names[name] %> 18 <%= @names[name] %>
18 </h3> 19 </h3>
19 <% end %> 20 <% end %>
20 - <%= link_to( _('see all (%d)') % results.total_entries, 21 + <%= link_to( results.respond_to?(:total_entries) ? _('see all (%d)') % results.total_entries : _('see all...'),
21 params.merge(:action => 'index', 22 params.merge(:action => 'index',
22 :find_in => [ name ]), 23 :find_in => [ name ]),
23 :class => 'see-more' ) if @results.size > 1 %> 24 :class => 'see-more' ) if @results.size > 1 %>
test/unit/category_finder_test.rb
@@ -115,6 +115,11 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase @@ -115,6 +115,11 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
115 ent = Enterprise.create!(:name => 'teste', :identifier => 'teste', :category_ids => [@category.id]) 115 ent = Enterprise.create!(:name => 'teste', :identifier => 'teste', :category_ids => [@category.id])
116 assert_includes @finder.recent('enterprises'), ent 116 assert_includes @finder.recent('enterprises'), ent
117 end 117 end
  118 +
  119 + should 'respond to total_entries in the recent enterprises result' do
  120 + ent = Enterprise.create!(:name => 'teste', :identifier => 'teste', :category_ids => [@category.id])
  121 + assert_respond_to @finder.recent('enterprises'), :total_entries
  122 + end
118 123
119 should 'not list more enterprises than limit' do 124 should 'not list more enterprises than limit' do
120 ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) 125 ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id])
@@ -244,8 +249,10 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase @@ -244,8 +249,10 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
244 2.times { articles[0].comments.build(:title => 'test', :body => 'asdsad', :author => person).save! } 249 2.times { articles[0].comments.build(:title => 'test', :body => 'asdsad', :author => person).save! }
245 4.times { articles[1].comments.build(:title => 'test', :body => 'asdsad', :author => person).save! } 250 4.times { articles[1].comments.build(:title => 'test', :body => 'asdsad', :author => person).save! }
246 251
  252 + result = @finder.most_commented_articles(2)
247 # should respect the order (more commented comes first) 253 # should respect the order (more commented comes first)
248 - assert_equal [articles[1], articles[0]], @finder.most_commented_articles(2) 254 + assert_equal [articles[1], articles[0]], result
  255 + assert_respond_to result, :total_entries
249 end 256 end
250 257
251 should 'find person and enterprise by radius and region' do 258 should 'find person and enterprise by radius and region' do
@@ -280,6 +287,7 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase @@ -280,6 +287,7 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
280 events = finder.current_events(2008, 1) 287 events = finder.current_events(2008, 1)
281 assert_includes events, e1 288 assert_includes events, e1
282 assert_not_includes events, e2 289 assert_not_includes events, e2
  290 + assert_respond_to events, :total_entries
283 end 291 end
284 292
285 should 'find person and enterprise in category by radius and region even without query' do 293 should 'find person and enterprise in category by radius and region even without query' do