From 6f0362a9b2290c6ee419e8050b3994ff4831d693 Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Fri, 4 Jul 2008 18:54:23 +0000 Subject: [PATCH] ActionItem492: added the product box in the category page, the possibility of mantain the order of the blocks and fixed the total_entries in the see all... sub tab --- app/controllers/public/search_controller.rb | 17 +++++++++++------ app/models/category_finder.rb | 10 ++++++---- app/views/search/_display_results.rhtml | 5 +++-- test/unit/category_finder_test.rb | 10 +++++++++- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index bd485ed..a9839f6 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -149,9 +149,11 @@ class SearchController < ApplicationController number_of_result_assets = @searching.values.select{|v| v}.size @results = {} + @order = [] @names = {} SEARCH_IN.select { |key,description| @searching[key] }.each do |key, description| + @order << key @results[key] = @finder.find(key, @filtered_query, calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius])) @names[key] = gettext(description) end @@ -176,15 +178,18 @@ class SearchController < ApplicationController # view the summary of one category def category_index @results = {} + @order = [] @names = {} [ - [ :people, _('Newer people'), @finder.recent('people') ], - [ :communities, _('Newer communities'), @finder.recent('communities') ], - [ :articles, _('Newer articles'), @finder.recent('articles') ], - [ :most_commented_articles, _('Most commented articles'), @finder.most_commented_articles ], - [ :enterprises, _('Newer enterprises'), @finder.recent('enterprises') ], - [ :events, _('Near events TODO'), @finder.current_events(params[:year], params[:month]) ] + [ :people, _('Newer people'), @finder.recent('people', limit) ], + [ :enterprises, _('Newer enterprises'), @finder.recent('enterprises', limit) ], + [ :products, ('Newer products'), @finder.recent('products', limit) ], + [ :events, _('Near events TODO'), @finder.current_events(params[:year], params[:month], {:per_page => limit}) ], + [ :communities, _('Newer communities'), @finder.recent('communities', limit) ], + [ :articles, _('Newer articles'), @finder.recent('articles', limit) ], + [ :most_commented_articles, _('Most commented articles'), @finder.most_commented_articles(limit) ] ].each do |key, name, list| + @order << key @results[key] = list @names[key] = name end diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index 783173a..a5291b5 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -34,14 +34,16 @@ class CategoryFinder find(asset, query, options).total_entries end - def most_commented_articles(limit=10) - Article.find(:all, options_for_find(Article, :limit => limit, :order => 'comments_count DESC')) + def most_commented_articles(limit=10, options={}) + options = {:page => 1, :per_page => limit, :order => 'comments_count DESC'}.merge(options) + Article.paginate(:all, options_for_find(Article, options)) end - def current_events(year, month) + def current_events(year, month, options={}) + options = {:page => 1}.merge(options) range = Event.date_range(year, month) - Event.find(:all, :include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }) + Event.paginate(:all, {:include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }}.merge(options)) end protected diff --git a/app/views/search/_display_results.rhtml b/app/views/search/_display_results.rhtml index 89ecb39..f016b77 100644 --- a/app/views/search/_display_results.rhtml +++ b/app/views/search/_display_results.rhtml @@ -4,7 +4,8 @@ pos2 = :odd # allow to format in a two columns layout pos3 = 3 # allow to format in a thre columns layout %> -<% @results.each do |name,results| %> +<% @order.each do |name| %> + <% results = @results[name] %> <% pos3 += 1; pos3 = 1 if pos3 > 3 pos2==:odd ? pos2=:even : pos2=:odd @@ -17,7 +18,7 @@ <%= @names[name] %> <% end %> - <%= link_to( _('see all (%d)') % results.total_entries, + <%= link_to( results.respond_to?(:total_entries) ? _('see all (%d)') % results.total_entries : _('see all...'), params.merge(:action => 'index', :find_in => [ name ]), :class => 'see-more' ) if @results.size > 1 %> diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index 98e7f25..6e0d53b 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -115,6 +115,11 @@ class CategoryFinderTest < ActiveSupport::TestCase ent = Enterprise.create!(:name => 'teste', :identifier => 'teste', :category_ids => [@category.id]) assert_includes @finder.recent('enterprises'), ent end + + should 'respond to total_entries in the recent enterprises result' do + ent = Enterprise.create!(:name => 'teste', :identifier => 'teste', :category_ids => [@category.id]) + assert_respond_to @finder.recent('enterprises'), :total_entries + end should 'not list more enterprises than limit' do ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) @@ -244,8 +249,10 @@ class CategoryFinderTest < ActiveSupport::TestCase 2.times { articles[0].comments.build(:title => 'test', :body => 'asdsad', :author => person).save! } 4.times { articles[1].comments.build(:title => 'test', :body => 'asdsad', :author => person).save! } + result = @finder.most_commented_articles(2) # should respect the order (more commented comes first) - assert_equal [articles[1], articles[0]], @finder.most_commented_articles(2) + assert_equal [articles[1], articles[0]], result + assert_respond_to result, :total_entries end should 'find person and enterprise by radius and region' do @@ -280,6 +287,7 @@ class CategoryFinderTest < ActiveSupport::TestCase events = finder.current_events(2008, 1) assert_includes events, e1 assert_not_includes events, e2 + assert_respond_to events, :total_entries end should 'find person and enterprise in category by radius and region even without query' do -- libgit2 0.21.2