Commit d59d6ea6a1862d1082618db2a87d0ac16a478008
1 parent
a885775d
Exists in
master
and in
22 other branches
ActionItem510: made product menu list a two level list
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2124 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
4 changed files
with
33 additions
and
5 deletions
Show diff stats
app/controllers/public/search_controller.rb
@@ -89,7 +89,14 @@ class SearchController < ApplicationController | @@ -89,7 +89,14 @@ class SearchController < ApplicationController | ||
89 | def load_product_categories_menu(asset) | 89 | def load_product_categories_menu(asset) |
90 | @results[asset].uniq! | 90 | @results[asset].uniq! |
91 | @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| | 91 | @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| |
92 | - [cat, @finder.count(:products, @filtered_query, calculate_find_options(asset, nil, cat, @region, params[:radius]))] | 92 | + hits = @finder.count(:products, @filtered_query, calculate_find_options(asset, nil, cat, @region, params[:radius])) |
93 | + childs = [] | ||
94 | + # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop | ||
95 | + childs = cat.children.map do |child| | ||
96 | + child_hits = @finder.count(:products, @filtered_query, calculate_find_options(asset, nil, child, @region, params[:radius])) | ||
97 | + [child, child_hits] | ||
98 | + end.select{|child, child_hits| child_hits > 0 } | ||
99 | + [cat, hits, childs] | ||
93 | end.select{|cat, hits| hits > 0 } | 100 | end.select{|cat, hits| hits > 0 } |
94 | end | 101 | end |
95 | 102 |
app/views/search/_product_categories_menu.rhtml
1 | <% if product_categories_menu %> | 1 | <% if product_categories_menu %> |
2 | <div id="product-categories-menu" class="product-search-filter"> | 2 | <div id="product-categories-menu" class="product-search-filter"> |
3 | <ul> | 3 | <ul> |
4 | - <% product_categories_menu.each do |cat, hits| %> | ||
5 | - <li><%= link_to (cat.name + "(#{hits})"), params.merge({:product_category => cat.id}) %> </li> | 4 | + <% product_categories_menu.each do |cat, hits, childs| %> |
5 | + <li> | ||
6 | + <%= link_to (cat.name + "(#{hits})"), params.merge({:product_category => cat.id}) %> | ||
7 | + <% if !childs.blank? %> | ||
8 | + <ul> | ||
9 | + <% childs.each do |child, child_hits| %> | ||
10 | + <li> <%= link_to (child.name + "(#{child_hits})"), params.merge({:product_category => child.id}) %> </li> | ||
11 | + <% end %> | ||
12 | + </ul> | ||
13 | + <% end %> | ||
14 | + </li> | ||
6 | <% end %> | 15 | <% end %> |
7 | </ul> | 16 | </ul> |
8 | </div> | 17 | </div> |
test/functional/search_controller_test.rb
@@ -904,6 +904,19 @@ class SearchControllerTest < Test::Unit::TestCase | @@ -904,6 +904,19 @@ class SearchControllerTest < Test::Unit::TestCase | ||
904 | assert_not_includes assigns(:categories_menu).map(&:first), cat12 | 904 | assert_not_includes assigns(:categories_menu).map(&:first), cat12 |
905 | end | 905 | end |
906 | 906 | ||
907 | + should 'load two level of the product categories tree' do | ||
908 | + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default) | ||
909 | + cat11 = ProductCategory.create(:name => 'prod cat 11', :environment => Environment.default, :parent => cat1) | ||
910 | + cat12 = ProductCategory.create(:name => 'prod cat 12', :environment => Environment.default, :parent => cat1) | ||
911 | + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') | ||
912 | + p = cat11.products.create!(:name => 'prod test 1', :enterprise => ent) | ||
913 | + | ||
914 | + get :index, :find_in => 'enterprises' | ||
915 | + | ||
916 | + assert_includes assigns(:categories_menu).map{|a|a[2].map(&:first)}.flatten, cat11 | ||
917 | + assert_not_includes assigns(:categories_menu).map{|a|a[2].map(&:first)}.flatten, cat12 | ||
918 | + end | ||
919 | + | ||
907 | should 'provide calendar for events' do | 920 | should 'provide calendar for events' do |
908 | get :index, :find_in => [ 'events' ] | 921 | get :index, :find_in => [ 'events' ] |
909 | assert_equal 0, assigns(:calendar).size % 7 | 922 | assert_equal 0, assigns(:calendar).size % 7 |
test/unit/product_category_test.rb
@@ -71,12 +71,11 @@ class ProductCategoryTest < Test::Unit::TestCase | @@ -71,12 +71,11 @@ class ProductCategoryTest < Test::Unit::TestCase | ||
71 | assert_equal [c1], ProductCategory.menu_categories(nil, env1) | 71 | assert_equal [c1], ProductCategory.menu_categories(nil, env1) |
72 | end | 72 | end |
73 | 73 | ||
74 | - should 'return chlidren of parent category' do | 74 | + should 'return children of parent category' do |
75 | c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default) | 75 | c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default) |
76 | c11 = ProductCategory.create!(:name => 'test cat 11', :environment => Environment.default, :parent => c1) | 76 | c11 = ProductCategory.create!(:name => 'test cat 11', :environment => Environment.default, :parent => c1) |
77 | c2 = ProductCategory.create!(:name => 'test cat 2', :environment => Environment.default) | 77 | c2 = ProductCategory.create!(:name => 'test cat 2', :environment => Environment.default) |
78 | 78 | ||
79 | assert_equal [c11], ProductCategory.menu_categories(c1, nil) | 79 | assert_equal [c11], ProductCategory.menu_categories(c1, nil) |
80 | end | 80 | end |
81 | - | ||
82 | end | 81 | end |