Commit d59d6ea6a1862d1082618db2a87d0ac16a478008

Authored by MoisesMachado
1 parent a885775d

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
app/controllers/public/search_controller.rb
... ... @@ -89,7 +89,14 @@ class SearchController < ApplicationController
89 89 def load_product_categories_menu(asset)
90 90 @results[asset].uniq!
91 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 100 end.select{|cat, hits| hits > 0 }
94 101 end
95 102  
... ...
app/views/search/_product_categories_menu.rhtml
1 1 <% if product_categories_menu %>
2 2 <div id="product-categories-menu" class="product-search-filter">
3 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 15 <% end %>
7 16 </ul>
8 17 </div>
... ...
test/functional/search_controller_test.rb
... ... @@ -904,6 +904,19 @@ class SearchControllerTest &lt; Test::Unit::TestCase
904 904 assert_not_includes assigns(:categories_menu).map(&:first), cat12
905 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 920 should 'provide calendar for events' do
908 921 get :index, :find_in => [ 'events' ]
909 922 assert_equal 0, assigns(:calendar).size % 7
... ...
test/unit/product_category_test.rb
... ... @@ -71,12 +71,11 @@ class ProductCategoryTest &lt; Test::Unit::TestCase
71 71 assert_equal [c1], ProductCategory.menu_categories(nil, env1)
72 72 end
73 73  
74   - should 'return chlidren of parent category' do
  74 + should 'return children of parent category' do
75 75 c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default)
76 76 c11 = ProductCategory.create!(:name => 'test cat 11', :environment => Environment.default, :parent => c1)
77 77 c2 = ProductCategory.create!(:name => 'test cat 2', :environment => Environment.default)
78 78  
79 79 assert_equal [c11], ProductCategory.menu_categories(c1, nil)
80 80 end
81   -
82 81 end
... ...