From 31719255f669d69bd6d7cf82e84e0a513ebe9544 Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Tue, 8 Jul 2008 21:05:19 +0000 Subject: [PATCH] ActionItem501: fixed the tracking of the number of item per page --- app/controllers/public/search_controller.rb | 16 ++++++++++------ app/models/product_category.rb | 8 ++------ test/unit/product_category_test.rb | 21 --------------------- 3 files changed, 12 insertions(+), 33 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index d3b38cc..038fca2 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -88,14 +88,16 @@ class SearchController < ApplicationController def load_product_categories_menu(asset) @results[asset].uniq! + # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| hits = @finder.count(asset, @filtered_query, calculate_find_options(asset, nil, nil, cat, @region, params[:radius], nil, nil)) childs = [] - # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop - childs = cat.children.map do |child| - child_hits = @finder.count(asset, @filtered_query, calculate_find_options(asset, nil, nil, child, @region, params[:radius], nil, nil)) - [child, child_hits] - end.select{|child, child_hits| child_hits > 0 } + if hits > 0 + childs = cat.children.map do |child| + child_hits = @finder.count(asset, @filtered_query, calculate_find_options(asset, nil, nil, child, @region, params[:radius])) + [child, child_hits] + end.select{|child, child_hits| child_hits > 0 } + end [cat, hits, childs] end.select{|cat, hits| hits > 0 } end @@ -117,7 +119,8 @@ class SearchController < ApplicationController # limit the number of results per page # TODO: dont hardcore like this def limit - (@searching.size == 1) ? 10 : 6 + searching = @searching.values.select{|v|v} + (searching.size == 1) ? 10 : 6 end public @@ -173,6 +176,7 @@ class SearchController < ApplicationController end render :action => 'index' + render :text => 'bla' end alias :assets :index diff --git a/app/models/product_category.rb b/app/models/product_category.rb index c9e619c..c3c9aa5 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -3,15 +3,11 @@ class ProductCategory < Category has_many :consumptions has_many :consumers, :through => :consumptions, :source => :profile - def tree - children.inject([]){|all,c| all + c.tree } << self - end - def all_products - Product.find(:all, :conditions => { :product_category_id => tree.map(&:id) }) + Product.find(:all, :conditions => { :product_category_id => (all_children << self).map(&:id) }) end def self.menu_categories(top_category, env) - top_category ? top_category.children : top_level_for(env) + top_category ? top_category.children : top_level_for(env).select{|c|c.kind_of?(ProductCategory)} end end diff --git a/test/unit/product_category_test.rb b/test/unit/product_category_test.rb index 1f5a080..b819532 100644 --- a/test/unit/product_category_test.rb +++ b/test/unit/product_category_test.rb @@ -18,27 +18,6 @@ class ProductCategoryTest < Test::Unit::TestCase assert !c3.errors.invalid?(:type) end - def test_tree - c0 = ProductCategory.create!(:name => 'base_cat', :environment => Environment.default) - assert ! c0.new_record? - assert_equivalent [c0], c0.tree - - c1 = ProductCategory.create!(:name => 'cat_1', :parent => c0, :environment => Environment.default) - c0.reload - assert_equivalent [c1], c1.tree - assert_equivalent [c0, c1], c0.tree - - c2 = ProductCategory.create!(:name => 'cat_2', :parent => c0, :environment => Environment.default) - c0.reload; c1.reload; - assert_equivalent [c0,c1,c2] , c0.tree - - c3 = ProductCategory.create!(:name => 'cat_3', :parent => c2, :environment => Environment.default) - c0.reload; c1.reload; c2.reload - assert_equivalent [c0,c1,c2,c3], c0.tree - assert_equivalent [c2,c3], c2.tree - - end - def test_all_products c0 = ProductCategory.create!(:name => 'base_cat', :environment => Environment.default) assert_equivalent [], c0.all_products -- libgit2 0.21.2