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 149 number_of_result_assets = @searching.values.select{|v| v}.size
150 150  
151 151 @results = {}
  152 + @order = []
152 153 @names = {}
153 154  
154 155 SEARCH_IN.select { |key,description| @searching[key] }.each do |key, description|
  156 + @order << key
155 157 @results[key] = @finder.find(key, @filtered_query, calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius]))
156 158 @names[key] = gettext(description)
157 159 end
... ... @@ -176,15 +178,18 @@ class SearchController &lt; ApplicationController
176 178 # view the summary of one category
177 179 def category_index
178 180 @results = {}
  181 + @order = []
179 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 191 ].each do |key, name, list|
  192 + @order << key
188 193 @results[key] = list
189 194 @names[key] = name
190 195 end
... ...
app/models/category_finder.rb
... ... @@ -34,14 +34,16 @@ class CategoryFinder
34 34 find(asset, query, options).total_entries
35 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 40 end
40 41  
41   - def current_events(year, month)
  42 + def current_events(year, month, options={})
  43 + options = {:page => 1}.merge(options)
42 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 47 end
46 48  
47 49 protected
... ...
app/views/search/_display_results.rhtml
... ... @@ -4,7 +4,8 @@
4 4 pos2 = :odd # allow to format in a two columns layout
5 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 10 pos3 += 1; pos3 = 1 if pos3 > 3
10 11 pos2==:odd ? pos2=:even : pos2=:odd
... ... @@ -17,7 +18,7 @@
17 18 <%= @names[name] %>
18 19 </h3>
19 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 22 params.merge(:action => 'index',
22 23 :find_in => [ name ]),
23 24 :class => 'see-more' ) if @results.size > 1 %>
... ...
test/unit/category_finder_test.rb
... ... @@ -115,6 +115,11 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
115 115 ent = Enterprise.create!(:name => 'teste', :identifier => 'teste', :category_ids => [@category.id])
116 116 assert_includes @finder.recent('enterprises'), ent
117 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 124 should 'not list more enterprises than limit' do
120 125 ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id])
... ... @@ -244,8 +249,10 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
244 249 2.times { articles[0].comments.build(:title => 'test', :body => 'asdsad', :author => person).save! }
245 250 4.times { articles[1].comments.build(:title => 'test', :body => 'asdsad', :author => person).save! }
246 251  
  252 + result = @finder.most_commented_articles(2)
247 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 256 end
250 257  
251 258 should 'find person and enterprise by radius and region' do
... ... @@ -280,6 +287,7 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
280 287 events = finder.current_events(2008, 1)
281 288 assert_includes events, e1
282 289 assert_not_includes events, e2
  290 + assert_respond_to events, :total_entries
283 291 end
284 292  
285 293 should 'find person and enterprise in category by radius and region even without query' do
... ...