Commit 82210dff18544824218dabf84c2309aaa71e06b3

Authored by AntonioTerceiro
1 parent 6c70738c

ActionItem514: caching the product categories menu

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2222 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/search_controller.rb
@@ -74,6 +74,7 @@ class SearchController < ApplicationController @@ -74,6 +74,7 @@ class SearchController < ApplicationController
74 end 74 end
75 def enterprises 75 def enterprises
76 load_product_categories_menu(:enterprises) 76 load_product_categories_menu(:enterprises)
  77 + @categories_menu = true
77 end 78 end
78 def communities 79 def communities
79 #nothing, just to enable 80 #nothing, just to enable
@@ -84,38 +85,17 @@ class SearchController < ApplicationController @@ -84,38 +85,17 @@ class SearchController < ApplicationController
84 85
85 def products 86 def products
86 load_product_categories_menu(:products) 87 load_product_categories_menu(:products)
  88 + @categories_menu = true
87 end 89 end
88 90
89 def load_product_categories_menu(asset) 91 def load_product_categories_menu(asset)
90 @results[asset].uniq! 92 @results[asset].uniq!
91 # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop 93 # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop
92 -  
93 - cats = ProductCategory.menu_categories(@product_category, environment)  
94 - cats += cats.select { |c| c.children_count > 0 }.map(&:children).flatten  
95 - product_categories_ids = cats.map(&:id)  
96 94
97 - object_ids = nil  
98 if !@query.blank? || @region && !params[:radius].blank? 95 if !@query.blank? || @region && !params[:radius].blank?
99 - object_ids = @finder.find(asset, @filtered_query, calculate_find_options(asset, nil, params[:page], @product_category, @region, params[:radius], params[:year], params[:month]).merge({:limit => :all})) 96 + @result_ids = @finder.find(asset, @filtered_query, calculate_find_options(asset, nil, params[:page], @product_category, @region, params[:radius], params[:year], params[:month]).merge({:limit => :all}))
100 end 97 end
101 98
102 - counts = @finder.product_categories_count(asset, product_categories_ids, object_ids)  
103 -  
104 - @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat|  
105 - hits = counts[cat.id]  
106 - childs = []  
107 - if hits  
108 - if cat.children_count > 0  
109 - childs = cat.children.map do |child|  
110 - child_hits = counts[child.id]  
111 - [child, child_hits]  
112 - end.select{|child, child_hits| child_hits }  
113 - else  
114 - childs = []  
115 - end  
116 - end  
117 - [cat, hits, childs]  
118 - end.select{|cat, hits| hits }  
119 end 99 end
120 100
121 def calculate_find_options(asset, limit, page, product_category, region, radius, year, month) 101 def calculate_find_options(asset, limit, page, product_category, region, radius, year, month)
app/helpers/search_helper.rb
@@ -100,4 +100,30 @@ module SearchHelper @@ -100,4 +100,30 @@ module SearchHelper
100 will_paginate(collection, options) 100 will_paginate(collection, options)
101 end 101 end
102 102
  103 + def product_categories_menu(asset, product_category, object_ids = nil)
  104 + cats = ProductCategory.menu_categories(@product_category, environment)
  105 + cats += cats.select { |c| c.children_count > 0 }.map(&:children).flatten
  106 + product_categories_ids = cats.map(&:id)
  107 +
  108 + counts = @finder.product_categories_count(asset, product_categories_ids, object_ids)
  109 +
  110 + product_categories_menu = ProductCategory.menu_categories(product_category, environment).map do |cat|
  111 + hits = counts[cat.id]
  112 + childs = []
  113 + if hits
  114 + if cat.children_count > 0
  115 + childs = cat.children.map do |child|
  116 + child_hits = counts[child.id]
  117 + [child, child_hits]
  118 + end.select{|child, child_hits| child_hits }
  119 + else
  120 + childs = []
  121 + end
  122 + end
  123 + [cat, hits, childs]
  124 + end.select{|cat, hits| hits }
  125 +
  126 + render(:partial => 'product_categories_menu', :object => product_categories_menu)
  127 + end
  128 +
103 end 129 end
app/views/search/enterprises.rhtml
@@ -16,7 +16,9 @@ @@ -16,7 +16,9 @@
16 <div class="has_cat_list"> 16 <div class="has_cat_list">
17 <% end %> 17 <% end %>
18 18
19 -<%= render :partial => 'product_categories_menu', :object => @categories_menu %> 19 +<% cache(:action => 'assets', :asset => 'enterprises', :category_path => params[:category_path], :query => @query, :ragion => @region, :radius => params[:radius]) do %>
  20 + <%= product_categories_menu(:enterprises, @product_category, @result_ids) %>
  21 +<% end %>
20 22
21 <%= display_results %> 23 <%= display_results %>
22 24
app/views/search/products.rhtml
@@ -10,7 +10,9 @@ @@ -10,7 +10,9 @@
10 <div class="has_cat_list"> 10 <div class="has_cat_list">
11 <% end %> 11 <% end %>
12 12
13 -<%= render :partial => 'product_categories_menu', :object => @categories_menu %> 13 +<% cache(:action => 'assets', :asset => 'products', :category_path => params[:category_path], :query => @query, :ragion => @region, :radius => params[:radius]) do %>
  14 + <%= product_categories_menu(:products, @product_category, @result_ids) %>
  15 +<% end %>
14 16
15 <%= display_results %> 17 <%= display_results %>
16 18
test/functional/search_controller_test.rb
@@ -729,8 +729,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -729,8 +729,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
729 729
730 get :index, :find_in => 'products', :query => 'test' 730 get :index, :find_in => 'products', :query => 'test'
731 731
732 - assert_includes assigns(:categories_menu).map(&:first), cat1  
733 - assert_not_includes assigns(:categories_menu).map(&:first), cat2 732 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /pc test 1/ }
  733 + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /pc test c/ }
734 end 734 end
735 735
736 should 'display only within a product category when specified' do 736 should 'display only within a product category when specified' do
@@ -765,8 +765,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -765,8 +765,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
765 765
766 get :index, :find_in => 'products' 766 get :index, :find_in => 'products'
767 767
768 - assert_includes assigns(:categories_menu).map(&:first), cat1  
769 - assert_not_includes assigns(:categories_menu).map(&:first), cat2 768 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 1/ }
  769 + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 2/ }
770 end 770 end
771 771
772 should 'display children categories that has products when product category filter is selected' do 772 should 'display children categories that has products when product category filter is selected' do
@@ -778,8 +778,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -778,8 +778,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
778 778
779 get :index, :find_in => 'products', :product_category => cat1.id 779 get :index, :find_in => 'products', :product_category => cat1.id
780 780
781 - assert_includes assigns(:categories_menu).map(&:first), cat11  
782 - assert_not_includes assigns(:categories_menu).map(&:first), cat12 781 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 11/ }
  782 + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 12/ }
783 end 783 end
784 784
785 should 'list only product categories with enterprises' do 785 should 'list only product categories with enterprises' do
@@ -791,8 +791,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -791,8 +791,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
791 791
792 get :index, :find_in => 'enterprises', :query => 'test' 792 get :index, :find_in => 'enterprises', :query => 'test'
793 793
794 - assert_includes assigns(:categories_menu).map(&:first), cat1  
795 - assert_not_includes assigns(:categories_menu).map(&:first), cat2 794 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /pc test 1/ }
  795 + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /pc test c/ }
796 end 796 end
797 797
798 should 'display only enterprises in the product category when its specified' do 798 should 'display only enterprises in the product category when its specified' do
@@ -831,8 +831,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -831,8 +831,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
831 831
832 get :index, :find_in => 'enterprises' 832 get :index, :find_in => 'enterprises'
833 833
834 - assert_includes assigns(:categories_menu).map(&:first), cat1  
835 - assert_not_includes assigns(:categories_menu).map(&:first), cat2 834 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 1/ }
  835 + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 2/ }
836 end 836 end
837 837
838 should 'display children categories that has enterprises when product category filter is selected' do 838 should 'display children categories that has enterprises when product category filter is selected' do
@@ -844,8 +844,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -844,8 +844,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
844 844
845 get :index, :find_in => 'enterprises', :product_category => cat1.id 845 get :index, :find_in => 'enterprises', :product_category => cat1.id
846 846
847 - assert_includes assigns(:categories_menu).map(&:first), cat11  
848 - assert_not_includes assigns(:categories_menu).map(&:first), cat12 847 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 11/ }
  848 + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 12/ }
849 end 849 end
850 850
851 should 'load two level of the product categories tree' do 851 should 'load two level of the product categories tree' do
@@ -857,8 +857,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -857,8 +857,8 @@ class SearchControllerTest &lt; Test::Unit::TestCase
857 857
858 get :index, :find_in => 'enterprises' 858 get :index, :find_in => 'enterprises'
859 859
860 - assert_includes assigns(:categories_menu).map{|a|a[2].map(&:first)}.flatten, cat11  
861 - assert_not_includes assigns(:categories_menu).map{|a|a[2].map(&:first)}.flatten, cat12 860 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 11/ }
  861 + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 12/ }
862 end 862 end
863 863
864 should 'provide calendar for events' do 864 should 'provide calendar for events' do