Commit 8c1ecd9612fe7d2fa2c64a9bbf2b647beaff5548

Authored by MoisesMachado
1 parent d2c2650d

ActionItem514: make search_controller to use new count method


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2215 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing 1 changed file with 13 additions and 6 deletions   Show diff stats
app/controllers/public/search_controller.rb
... ... @@ -91,20 +91,27 @@ class SearchController < ApplicationController
91 91 # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop
92 92  
93 93 cats = ProductCategory.menu_categories(@product_category, environment)
94   - cats += cats.map(:children).flatten
  94 + cats += cats.map(&:children).flatten
95 95 product_categories_ids = cats.map(&:id)
96 96  
  97 + object_ids = nil
  98 + 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}))
  100 + end
  101 +
  102 + counts = @finder.product_categories_count(asset, product_categories_ids, object_ids)
  103 +
97 104 @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat|
98   - hits = @finder.count(asset, @filtered_query, calculate_find_options(asset, nil, nil, cat, @region, params[:radius], nil, nil))
  105 + hits = counts[cat.id]
99 106 childs = []
100   - if hits > 0
  107 + if hits
101 108 childs = cat.children.map do |child|
102   - child_hits = @finder.count(asset, @filtered_query, calculate_find_options(asset, nil, nil, child, @region, params[:radius], nil, nil))
  109 + child_hits = counts[child.id]
103 110 [child, child_hits]
104   - end.select{|child, child_hits| child_hits > 0 }
  111 + end.select{|child, child_hits| child_hits }
105 112 end
106 113 [cat, hits, childs]
107   - end.select{|cat, hits| hits > 0 }
  114 + end.select{|cat, hits| hits }
108 115 end
109 116  
110 117 def calculate_find_options(asset, limit, page, product_category, region, radius, year, month)
... ...