Commit 81fab8d7f66f12d464a80bf71991d01887057e31

Authored by AntonioTerceiro
1 parent 7aaeb63f

ActionItem514: avoiding uneeded queries

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2220 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing 1 changed file with 9 additions and 5 deletions   Show diff stats
app/controllers/public/search_controller.rb
... ... @@ -91,7 +91,7 @@ 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.select { |c| c.children_count > 0 }.map(&:children).flatten
95 95 product_categories_ids = cats.map(&:id)
96 96  
97 97 object_ids = nil
... ... @@ -105,10 +105,14 @@ class SearchController < ApplicationController
105 105 hits = counts[cat.id]
106 106 childs = []
107 107 if hits
108   - childs = cat.children.map do |child|
109   - child_hits = counts[child.id]
110   - [child, child_hits]
111   - end.select{|child, child_hits| child_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
112 116 end
113 117 [cat, hits, childs]
114 118 end.select{|cat, hits| hits }
... ...