From 82210dff18544824218dabf84c2309aaa71e06b3 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Sat, 12 Jul 2008 16:26:51 +0000 Subject: [PATCH] ActionItem514: caching the product categories menu --- app/controllers/public/search_controller.rb | 26 +++----------------------- app/helpers/search_helper.rb | 26 ++++++++++++++++++++++++++ app/views/search/enterprises.rhtml | 4 +++- app/views/search/products.rhtml | 4 +++- test/functional/search_controller_test.rb | 28 ++++++++++++++-------------- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index c133f33..eb0ecb7 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -74,6 +74,7 @@ class SearchController < ApplicationController end def enterprises load_product_categories_menu(:enterprises) + @categories_menu = true end def communities #nothing, just to enable @@ -84,38 +85,17 @@ class SearchController < ApplicationController def products load_product_categories_menu(:products) + @categories_menu = true end def load_product_categories_menu(asset) @results[asset].uniq! # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop - - cats = ProductCategory.menu_categories(@product_category, environment) - cats += cats.select { |c| c.children_count > 0 }.map(&:children).flatten - product_categories_ids = cats.map(&:id) - object_ids = nil if !@query.blank? || @region && !params[:radius].blank? - 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})) + @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})) end - counts = @finder.product_categories_count(asset, product_categories_ids, object_ids) - - @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| - hits = counts[cat.id] - childs = [] - if hits - if cat.children_count > 0 - childs = cat.children.map do |child| - child_hits = counts[child.id] - [child, child_hits] - end.select{|child, child_hits| child_hits } - else - childs = [] - end - end - [cat, hits, childs] - end.select{|cat, hits| hits } end def calculate_find_options(asset, limit, page, product_category, region, radius, year, month) diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index 06b74f3..d7eef23 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -100,4 +100,30 @@ module SearchHelper will_paginate(collection, options) end + def product_categories_menu(asset, product_category, object_ids = nil) + cats = ProductCategory.menu_categories(@product_category, environment) + cats += cats.select { |c| c.children_count > 0 }.map(&:children).flatten + product_categories_ids = cats.map(&:id) + + counts = @finder.product_categories_count(asset, product_categories_ids, object_ids) + + product_categories_menu = ProductCategory.menu_categories(product_category, environment).map do |cat| + hits = counts[cat.id] + childs = [] + if hits + if cat.children_count > 0 + childs = cat.children.map do |child| + child_hits = counts[child.id] + [child, child_hits] + end.select{|child, child_hits| child_hits } + else + childs = [] + end + end + [cat, hits, childs] + end.select{|cat, hits| hits } + + render(:partial => 'product_categories_menu', :object => product_categories_menu) + end + end diff --git a/app/views/search/enterprises.rhtml b/app/views/search/enterprises.rhtml index 8a6715d..b9a3541 100644 --- a/app/views/search/enterprises.rhtml +++ b/app/views/search/enterprises.rhtml @@ -16,7 +16,9 @@
<% end %> -<%= render :partial => 'product_categories_menu', :object => @categories_menu %> +<% cache(:action => 'assets', :asset => 'enterprises', :category_path => params[:category_path], :query => @query, :ragion => @region, :radius => params[:radius]) do %> + <%= product_categories_menu(:enterprises, @product_category, @result_ids) %> +<% end %> <%= display_results %> diff --git a/app/views/search/products.rhtml b/app/views/search/products.rhtml index 03574bd..f71bf56 100644 --- a/app/views/search/products.rhtml +++ b/app/views/search/products.rhtml @@ -10,7 +10,9 @@
<% end %> -<%= render :partial => 'product_categories_menu', :object => @categories_menu %> +<% cache(:action => 'assets', :asset => 'products', :category_path => params[:category_path], :query => @query, :ragion => @region, :radius => params[:radius]) do %> + <%= product_categories_menu(:products, @product_category, @result_ids) %> +<% end %> <%= display_results %> diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index b8cdbba..5911c13 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -729,8 +729,8 @@ class SearchControllerTest < Test::Unit::TestCase get :index, :find_in => 'products', :query => 'test' - assert_includes assigns(:categories_menu).map(&:first), cat1 - assert_not_includes assigns(:categories_menu).map(&:first), cat2 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /pc test 1/ } + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /pc test c/ } end should 'display only within a product category when specified' do @@ -765,8 +765,8 @@ class SearchControllerTest < Test::Unit::TestCase get :index, :find_in => 'products' - assert_includes assigns(:categories_menu).map(&:first), cat1 - assert_not_includes assigns(:categories_menu).map(&:first), cat2 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 1/ } + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 2/ } end should 'display children categories that has products when product category filter is selected' do @@ -778,8 +778,8 @@ class SearchControllerTest < Test::Unit::TestCase 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 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 11/ } + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 12/ } end should 'list only product categories with enterprises' do @@ -791,8 +791,8 @@ class SearchControllerTest < Test::Unit::TestCase get :index, :find_in => 'enterprises', :query => 'test' - assert_includes assigns(:categories_menu).map(&:first), cat1 - assert_not_includes assigns(:categories_menu).map(&:first), cat2 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /pc test 1/ } + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /pc test c/ } end should 'display only enterprises in the product category when its specified' do @@ -831,8 +831,8 @@ class SearchControllerTest < Test::Unit::TestCase get :index, :find_in => 'enterprises' - assert_includes assigns(:categories_menu).map(&:first), cat1 - assert_not_includes assigns(:categories_menu).map(&:first), cat2 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 1/ } + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 2/ } end should 'display children categories that has enterprises when product category filter is selected' do @@ -844,8 +844,8 @@ class SearchControllerTest < Test::Unit::TestCase get :index, :find_in => 'enterprises', :product_category => cat1.id - assert_includes assigns(:categories_menu).map(&:first), cat11 - assert_not_includes assigns(:categories_menu).map(&:first), cat12 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 11/ } + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 12/ } end should 'load two level of the product categories tree' do @@ -857,8 +857,8 @@ class SearchControllerTest < Test::Unit::TestCase get :index, :find_in => 'enterprises' - assert_includes assigns(:categories_menu).map{|a|a[2].map(&:first)}.flatten, cat11 - assert_not_includes assigns(:categories_menu).map{|a|a[2].map(&:first)}.flatten, cat12 + assert_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 11/ } + assert_no_tag :attributes => { :id => "product-categories-menu" }, :descendant => { :tag => 'a', :content => /prod cat 12/ } end should 'provide calendar for events' do -- libgit2 0.21.2