diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 69868ca..f797c08 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -155,12 +155,9 @@ class SearchController < ApplicationController def products @results[:products].uniq! - @categories = ProductCategory.menu_categories(@product_category) - @categories.map do |cat| + @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| [cat, @finder.count(:products, @filtered_query, calculate_find_options(:products, nil, cat, @region, params[:radius]))] - end - - @found_product_categories = @counts.values.sort_by{|v|v[0].full_name} + end.select{|cat, hits| hits > 0 } end alias :assets :index diff --git a/app/models/product_category.rb b/app/models/product_category.rb index ce91b4f..c9e619c 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -10,4 +10,8 @@ class ProductCategory < Category def all_products Product.find(:all, :conditions => { :product_category_id => tree.map(&:id) }) end + + def self.menu_categories(top_category, env) + top_category ? top_category.children : top_level_for(env) + end end diff --git a/app/views/search/products.rhtml b/app/views/search/products.rhtml index a860313..966f240 100644 --- a/app/views/search/products.rhtml +++ b/app/views/search/products.rhtml @@ -12,10 +12,10 @@ <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> -<% if @found_product_categories %> +<% if @categories_menu %>
diff --git a/script/fbes_populate_helper.rb b/script/fbes_populate_helper.rb index 5a63bd7..4992db7 100644 --- a/script/fbes_populate_helper.rb +++ b/script/fbes_populate_helper.rb @@ -37,7 +37,7 @@ COUNT = { :categories => 0, } -$default_env = Environment.default_ +$default_env = Environment.default def step(what) COUNT[what] += 1 puts "#{what}: #{COUNT[what]}" diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 1d32e8d..fff380f 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -785,35 +785,8 @@ class SearchControllerTest < Test::Unit::TestCase get :index, :find_in => 'products', :query => 'test' - assert_equal 1, assigns(:counts)[cat1.id][1] - assert_equal nil, assigns(:counts)[cat2.id] - end - - should 'not list ancestor if no product in it' do - cat1 = ProductCategory.create!(:name => 'pc test 1', :environment => Environment.default) - cat2 = ProductCategory.create!(:name => 'pc test 2', :environment => Environment.default, :parent => cat1) - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') - - cat1.products.create!(:name => 'prod test 1', :enterprise => ent) - - get :index, :find_in => 'products', :query => 'test' - - assert_equal 1, assigns(:counts)[cat1.id][1] - assert_equal nil, assigns(:counts)[cat2.id] - end - - should 'add hits of children in ancestor when it has products on results' do - cat1 = ProductCategory.create!(:name => 'pc test 1', :environment => Environment.default) - cat2 = ProductCategory.create!(:name => 'pc test 2', :environment => Environment.default, :parent => cat1) - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') - - cat1.products.create!(:name => 'prod test 1', :enterprise => ent) - cat2.products.create!(:name => 'prod test 2', :enterprise => ent) - - get :index, :find_in => 'products', :query => 'test' - - assert_equal 2, assigns(:counts)[cat1.id][1] - assert_equal 1, assigns(:counts)[cat2.id][1] + assert_includes assigns(:categories_menu).map(&:first), cat1 + assert_not_includes assigns(:categories_menu).map(&:first), cat2 end should 'display only within a product category when specified' do @@ -840,6 +813,31 @@ class SearchControllerTest < Test::Unit::TestCase assert_includes assigns(:results)[:products], p end + should 'display only top level product categories that has products when no product category filter is specified' do + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default) + cat2 = ProductCategory.create(:name => 'prod cat 2', :environment => Environment.default) + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + p = cat1.products.create!(:name => 'prod test 1', :enterprise => ent) + + get :index, :find_in => 'products' + + assert_includes assigns(:categories_menu).map(&:first), cat1 + assert_not_includes assigns(:categories_menu).map(&:first), cat2 + end + + should 'display children categories that has products when product category filter is selected' do + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default) + cat11 = ProductCategory.create(:name => 'prod cat 11', :environment => Environment.default, :parent => cat1) + cat12 = ProductCategory.create(:name => 'prod cat 12', :environment => Environment.default, :parent => cat1) + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + p = cat11.products.create!(:name => 'prod test 1', :enterprise => ent) + + get :index, :find_in => 'products', :product_category => cat1.id + + assert_includes assigns(:categories_menu).map(&:first), cat11 + assert_not_includes assigns(:categories_menu).map(&:first), cat12 + end + should 'provide calendar for events' do get :index, :find_in => [ 'events' ] assert_equal 0, assigns(:calendar).size % 7 diff --git a/test/unit/product_category_test.rb b/test/unit/product_category_test.rb index f5de630..bb550a2 100644 --- a/test/unit/product_category_test.rb +++ b/test/unit/product_category_test.rb @@ -61,4 +61,22 @@ class ProductCategoryTest < Test::Unit::TestCase assert_includes c.consumers, person end + should 'return top level product categories for environment when no parent product category specified' do + env1 = Environment.create!(:name => 'test env 1') + env2 = Environment.create!(:name => 'test env 2') + + c1 = ProductCategory.create!(:name => 'test cat 1', :environment => env1) + c2 = ProductCategory.create!(:name => 'test cat 2', :environment => env2) + + assert_equal [c1], ProductCategory.menu_categories(nil, env1) + end + + should 'return chlidren of parent category' do + c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default) + c11 = ProductCategory.create!(:name => 'test cat 11', :environment => Environment.default, :parent => c1) + c2 = ProductCategory.create!(:name => 'test cat 2', :environment => Environment.default) + + assert_equal [c11], ProductCategory.menu_categories(c1, nil) + end + end -- libgit2 0.21.2