Commit 94f572282e84a6f0263901432c2e2a59391ee21b
1 parent
43a90b83
Exists in
staging
and in
42 other branches
ActionItem507: changed the filtering by product category to act more like a menu
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2118 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
6 changed files
with
54 additions
and
37 deletions
Show diff stats
app/controllers/public/search_controller.rb
| @@ -155,12 +155,9 @@ class SearchController < ApplicationController | @@ -155,12 +155,9 @@ class SearchController < ApplicationController | ||
| 155 | 155 | ||
| 156 | def products | 156 | def products |
| 157 | @results[:products].uniq! | 157 | @results[:products].uniq! |
| 158 | - @categories = ProductCategory.menu_categories(@product_category) | ||
| 159 | - @categories.map do |cat| | 158 | + @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| |
| 160 | [cat, @finder.count(:products, @filtered_query, calculate_find_options(:products, nil, cat, @region, params[:radius]))] | 159 | [cat, @finder.count(:products, @filtered_query, calculate_find_options(:products, nil, cat, @region, params[:radius]))] |
| 161 | - end | ||
| 162 | - | ||
| 163 | - @found_product_categories = @counts.values.sort_by{|v|v[0].full_name} | 160 | + end.select{|cat, hits| hits > 0 } |
| 164 | end | 161 | end |
| 165 | 162 | ||
| 166 | alias :assets :index | 163 | alias :assets :index |
app/models/product_category.rb
| @@ -10,4 +10,8 @@ class ProductCategory < Category | @@ -10,4 +10,8 @@ class ProductCategory < Category | ||
| 10 | def all_products | 10 | def all_products |
| 11 | Product.find(:all, :conditions => { :product_category_id => tree.map(&:id) }) | 11 | Product.find(:all, :conditions => { :product_category_id => tree.map(&:id) }) |
| 12 | end | 12 | end |
| 13 | + | ||
| 14 | + def self.menu_categories(top_category, env) | ||
| 15 | + top_category ? top_category.children : top_level_for(env) | ||
| 16 | + end | ||
| 13 | end | 17 | end |
app/views/search/products.rhtml
| @@ -12,10 +12,10 @@ | @@ -12,10 +12,10 @@ | ||
| 12 | 12 | ||
| 13 | <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> | 13 | <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> |
| 14 | 14 | ||
| 15 | -<% if @found_product_categories %> | 15 | +<% if @categories_menu %> |
| 16 | <div id="product-categories-menu" class="product-search-filter"> | 16 | <div id="product-categories-menu" class="product-search-filter"> |
| 17 | <ul> | 17 | <ul> |
| 18 | - <% @found_product_categories.each do |cat, hits| %> | 18 | + <% @categories_menu.each do |cat, hits| %> |
| 19 | <li><%= link_to (cat.name + "(#{hits})"), params.merge({:product_category => cat.id}) %> </li> | 19 | <li><%= link_to (cat.name + "(#{hits})"), params.merge({:product_category => cat.id}) %> </li> |
| 20 | <% end %> | 20 | <% end %> |
| 21 | </ul> | 21 | </ul> |
script/fbes_populate_helper.rb
| @@ -37,7 +37,7 @@ COUNT = { | @@ -37,7 +37,7 @@ COUNT = { | ||
| 37 | :categories => 0, | 37 | :categories => 0, |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | -$default_env = Environment.default_ | 40 | +$default_env = Environment.default |
| 41 | def step(what) | 41 | def step(what) |
| 42 | COUNT[what] += 1 | 42 | COUNT[what] += 1 |
| 43 | puts "#{what}: #{COUNT[what]}" | 43 | puts "#{what}: #{COUNT[what]}" |
test/functional/search_controller_test.rb
| @@ -785,35 +785,8 @@ class SearchControllerTest < Test::Unit::TestCase | @@ -785,35 +785,8 @@ class SearchControllerTest < Test::Unit::TestCase | ||
| 785 | 785 | ||
| 786 | get :index, :find_in => 'products', :query => 'test' | 786 | get :index, :find_in => 'products', :query => 'test' |
| 787 | 787 | ||
| 788 | - assert_equal 1, assigns(:counts)[cat1.id][1] | ||
| 789 | - assert_equal nil, assigns(:counts)[cat2.id] | ||
| 790 | - end | ||
| 791 | - | ||
| 792 | - should 'not list ancestor if no product in it' do | ||
| 793 | - cat1 = ProductCategory.create!(:name => 'pc test 1', :environment => Environment.default) | ||
| 794 | - cat2 = ProductCategory.create!(:name => 'pc test 2', :environment => Environment.default, :parent => cat1) | ||
| 795 | - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') | ||
| 796 | - | ||
| 797 | - cat1.products.create!(:name => 'prod test 1', :enterprise => ent) | ||
| 798 | - | ||
| 799 | - get :index, :find_in => 'products', :query => 'test' | ||
| 800 | - | ||
| 801 | - assert_equal 1, assigns(:counts)[cat1.id][1] | ||
| 802 | - assert_equal nil, assigns(:counts)[cat2.id] | ||
| 803 | - end | ||
| 804 | - | ||
| 805 | - should 'add hits of children in ancestor when it has products on results' do | ||
| 806 | - cat1 = ProductCategory.create!(:name => 'pc test 1', :environment => Environment.default) | ||
| 807 | - cat2 = ProductCategory.create!(:name => 'pc test 2', :environment => Environment.default, :parent => cat1) | ||
| 808 | - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') | ||
| 809 | - | ||
| 810 | - cat1.products.create!(:name => 'prod test 1', :enterprise => ent) | ||
| 811 | - cat2.products.create!(:name => 'prod test 2', :enterprise => ent) | ||
| 812 | - | ||
| 813 | - get :index, :find_in => 'products', :query => 'test' | ||
| 814 | - | ||
| 815 | - assert_equal 2, assigns(:counts)[cat1.id][1] | ||
| 816 | - assert_equal 1, assigns(:counts)[cat2.id][1] | 788 | + assert_includes assigns(:categories_menu).map(&:first), cat1 |
| 789 | + assert_not_includes assigns(:categories_menu).map(&:first), cat2 | ||
| 817 | end | 790 | end |
| 818 | 791 | ||
| 819 | should 'display only within a product category when specified' do | 792 | should 'display only within a product category when specified' do |
| @@ -840,6 +813,31 @@ class SearchControllerTest < Test::Unit::TestCase | @@ -840,6 +813,31 @@ class SearchControllerTest < Test::Unit::TestCase | ||
| 840 | assert_includes assigns(:results)[:products], p | 813 | assert_includes assigns(:results)[:products], p |
| 841 | end | 814 | end |
| 842 | 815 | ||
| 816 | + should 'display only top level product categories that has products when no product category filter is specified' do | ||
| 817 | + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default) | ||
| 818 | + cat2 = ProductCategory.create(:name => 'prod cat 2', :environment => Environment.default) | ||
| 819 | + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') | ||
| 820 | + p = cat1.products.create!(:name => 'prod test 1', :enterprise => ent) | ||
| 821 | + | ||
| 822 | + get :index, :find_in => 'products' | ||
| 823 | + | ||
| 824 | + assert_includes assigns(:categories_menu).map(&:first), cat1 | ||
| 825 | + assert_not_includes assigns(:categories_menu).map(&:first), cat2 | ||
| 826 | + end | ||
| 827 | + | ||
| 828 | + should 'display children categories that has products when product category filter is selected' do | ||
| 829 | + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default) | ||
| 830 | + cat11 = ProductCategory.create(:name => 'prod cat 11', :environment => Environment.default, :parent => cat1) | ||
| 831 | + cat12 = ProductCategory.create(:name => 'prod cat 12', :environment => Environment.default, :parent => cat1) | ||
| 832 | + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') | ||
| 833 | + p = cat11.products.create!(:name => 'prod test 1', :enterprise => ent) | ||
| 834 | + | ||
| 835 | + get :index, :find_in => 'products', :product_category => cat1.id | ||
| 836 | + | ||
| 837 | + assert_includes assigns(:categories_menu).map(&:first), cat11 | ||
| 838 | + assert_not_includes assigns(:categories_menu).map(&:first), cat12 | ||
| 839 | + end | ||
| 840 | + | ||
| 843 | should 'provide calendar for events' do | 841 | should 'provide calendar for events' do |
| 844 | get :index, :find_in => [ 'events' ] | 842 | get :index, :find_in => [ 'events' ] |
| 845 | assert_equal 0, assigns(:calendar).size % 7 | 843 | assert_equal 0, assigns(:calendar).size % 7 |
test/unit/product_category_test.rb
| @@ -61,4 +61,22 @@ class ProductCategoryTest < Test::Unit::TestCase | @@ -61,4 +61,22 @@ class ProductCategoryTest < Test::Unit::TestCase | ||
| 61 | assert_includes c.consumers, person | 61 | assert_includes c.consumers, person |
| 62 | end | 62 | end |
| 63 | 63 | ||
| 64 | + should 'return top level product categories for environment when no parent product category specified' do | ||
| 65 | + env1 = Environment.create!(:name => 'test env 1') | ||
| 66 | + env2 = Environment.create!(:name => 'test env 2') | ||
| 67 | + | ||
| 68 | + c1 = ProductCategory.create!(:name => 'test cat 1', :environment => env1) | ||
| 69 | + c2 = ProductCategory.create!(:name => 'test cat 2', :environment => env2) | ||
| 70 | + | ||
| 71 | + assert_equal [c1], ProductCategory.menu_categories(nil, env1) | ||
| 72 | + end | ||
| 73 | + | ||
| 74 | + should 'return chlidren of parent category' do | ||
| 75 | + c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default) | ||
| 76 | + c11 = ProductCategory.create!(:name => 'test cat 11', :environment => Environment.default, :parent => c1) | ||
| 77 | + c2 = ProductCategory.create!(:name => 'test cat 2', :environment => Environment.default) | ||
| 78 | + | ||
| 79 | + assert_equal [c11], ProductCategory.menu_categories(c1, nil) | ||
| 80 | + end | ||
| 81 | + | ||
| 64 | end | 82 | end |