Commit 58e344051418bb87c149820b9b8f17a8fe8a4251
1 parent
e0f46608
Exists in
master
and in
29 other branches
ActionItem507: added count method to finders
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2116 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
5 changed files
with
133 additions
and
31 deletions
Show diff stats
app/controllers/public/search_controller.rb
@@ -82,6 +82,16 @@ class SearchController < ApplicationController | @@ -82,6 +82,16 @@ class SearchController < ApplicationController | ||
82 | #nothins, just to enable | 82 | #nothins, just to enable |
83 | end | 83 | end |
84 | 84 | ||
85 | + def calculate_find_options(asset, limit, product_category, region, radius) | ||
86 | + | ||
87 | + result = { :limit => limit, :product_category => product_category} | ||
88 | + if [:enterprises, :people].include?(asset) && region | ||
89 | + result.merge!(:within => radius, :region => region.id) | ||
90 | + end | ||
91 | + | ||
92 | + result | ||
93 | + end | ||
94 | + | ||
85 | public | 95 | public |
86 | 96 | ||
87 | include SearchHelper | 97 | include SearchHelper |
@@ -118,20 +128,15 @@ class SearchController < ApplicationController | @@ -118,20 +128,15 @@ class SearchController < ApplicationController | ||
118 | number_of_result_assets = @searching.values.select{|v| v}.size | 128 | number_of_result_assets = @searching.values.select{|v| v}.size |
119 | 129 | ||
120 | # apply limit when searching for only one type of asset | 130 | # apply limit when searching for only one type of asset |
121 | -# limit = (number_of_result_assets == 1) ? LIST_LIMIT : nil | 131 | + limit = (number_of_result_assets == 1) ? LIST_LIMIT : nil |
122 | # apply limit to all searches | 132 | # apply limit to all searches |
123 | - limit = nil | 133 | +# limit = nil |
124 | 134 | ||
125 | @results = {} | 135 | @results = {} |
126 | @names = {} | 136 | @names = {} |
127 | - SEARCH_IN.each do |key, description| | ||
128 | - if @searching[key] | ||
129 | - if [:enterprises, :people].include?(key) && @region | ||
130 | - @results[key] = @finder.find(key, @filtered_query, :within => params[:radius], :region => @region.id, :product_category => @product_category, :limit => limit) | ||
131 | - else | ||
132 | - @results[key] = @finder.find(key, @filtered_query, :product_category => @product_category, :limit => limit) | ||
133 | - end | ||
134 | - end | 137 | + |
138 | + SEARCH_IN.select { |key,description| @searching[key] }.each do |key, description| | ||
139 | + @results[key] = @finder.find(key, @filtered_query, calculate_find_options(key, limit, @product_category, @region, params[:radius])) | ||
135 | @names[key] = gettext(description) | 140 | @names[key] = gettext(description) |
136 | end | 141 | end |
137 | 142 | ||
@@ -150,21 +155,9 @@ class SearchController < ApplicationController | @@ -150,21 +155,9 @@ class SearchController < ApplicationController | ||
150 | 155 | ||
151 | def products | 156 | def products |
152 | @results[:products].uniq! | 157 | @results[:products].uniq! |
153 | -# if !(@category || @product_category || @region || (!@query.blank?)) | ||
154 | -# # not searching, no menu | ||
155 | -# return | ||
156 | -# end | ||
157 | - | ||
158 | - @categories = @results[:products].map(&:product_category).compact | ||
159 | - @counts = @categories.uniq.inject({}) do |h, cat| | ||
160 | - h[cat.id] = [cat, 0] | ||
161 | - h | ||
162 | - end | ||
163 | - | ||
164 | - @categories.each do |cat| | ||
165 | - cat.hierarchy.each do |each_cat| | ||
166 | - @counts[each_cat.id][1] += 1 if @counts[each_cat.id] | ||
167 | - end | 158 | + @categories = ProductCategory.menu_categories(@product_category) |
159 | + @categories.map do |cat| | ||
160 | + [cat, @finder.count(:products, @filtered_query, calculate_find_options(:products, nil, cat, @region, params[:radius]))] | ||
168 | end | 161 | end |
169 | 162 | ||
170 | @found_product_categories = @counts.values.sort_by{|v|v[0].full_name} | 163 | @found_product_categories = @counts.values.sort_by{|v|v[0].full_name} |
app/models/category_finder.rb
@@ -32,8 +32,12 @@ class CategoryFinder | @@ -32,8 +32,12 @@ class CategoryFinder | ||
32 | asset_class(asset).find(:all, options_for_find_by_initial(asset_class(asset), initial)) | 32 | asset_class(asset).find(:all, options_for_find_by_initial(asset_class(asset), initial)) |
33 | end | 33 | end |
34 | 34 | ||
35 | - def count(asset) | ||
36 | - asset_class(asset).count(:all, options_for_find(asset_class(asset), :select => "#{asset_table(asset)}.id")) | 35 | + def count(asset, query='', options={}) |
36 | + if query.blank? | ||
37 | + find(asset, query, options).size | ||
38 | + else | ||
39 | + find(asset, query, options).total_hits | ||
40 | + end | ||
37 | end | 41 | end |
38 | 42 | ||
39 | def most_commented_articles(limit=10) | 43 | def most_commented_articles(limit=10) |
app/models/environment_finder.rb
@@ -43,8 +43,13 @@ class EnvironmentFinder | @@ -43,8 +43,13 @@ class EnvironmentFinder | ||
43 | @environment.send(asset).find_by_initial(initial) | 43 | @environment.send(asset).find_by_initial(initial) |
44 | end | 44 | end |
45 | 45 | ||
46 | - def count(asset) | ||
47 | - @environment.send(asset).count | 46 | + def count(asset, query = '', options = {}) |
47 | + if query.blank? | ||
48 | + # SLOW | ||
49 | + find(asset, query, options).size | ||
50 | + else | ||
51 | + find(asset, query, options).total_hits | ||
52 | + end | ||
48 | end | 53 | end |
49 | 54 | ||
50 | end | 55 | end |
test/unit/category_finder_test.rb
@@ -157,6 +157,60 @@ class CategoryFinderTest < ActiveSupport::TestCase | @@ -157,6 +157,60 @@ class CategoryFinderTest < ActiveSupport::TestCase | ||
157 | ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) | 157 | ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) |
158 | assert_equal count+1, @finder.count('enterprises') | 158 | assert_equal count+1, @finder.count('enterprises') |
159 | end | 159 | end |
160 | + | ||
161 | + should 'count people' do | ||
162 | + count = @finder.count('people') | ||
163 | + p = create_user('testinguser').person | ||
164 | + p.category_ids = [@category.id] | ||
165 | + p.save! | ||
166 | + | ||
167 | + assert_equal count+1, @finder.count('people') | ||
168 | + end | ||
169 | + should 'count products' do | ||
170 | + count = @finder.count('products') | ||
171 | + | ||
172 | + ent = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) | ||
173 | + ent.products.create!(:name => 'test prodduct') | ||
174 | + | ||
175 | + assert_equal count+1, @finder.count('products') | ||
176 | + end | ||
177 | + should 'count articles' do | ||
178 | + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
179 | + | ||
180 | + count = @finder.count('articles') | ||
181 | + ent1.articles.create!(:name => 'teste1', :category_ids => [@category.id]) | ||
182 | + | ||
183 | + assert_equal count+1, @finder.count('articles') | ||
184 | + end | ||
185 | + should 'count events' do | ||
186 | + count = @finder.count('events') | ||
187 | + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
188 | + | ||
189 | + Event.create!(:name => 'teste2', :profile => ent1, :start_date => Date.today, :category_ids => [@category.id]) | ||
190 | + assert_equal count+1, @finder.count('events') | ||
191 | + end | ||
192 | + | ||
193 | + should 'count enterprises with query and options' do | ||
194 | + options = mock | ||
195 | + results = mock | ||
196 | + | ||
197 | + @finder.expects(:find).with('people', 'my query', options).returns(results) | ||
198 | + | ||
199 | + results.expects(:total_hits).returns(99) | ||
200 | + | ||
201 | + assert_equal 99, @finder.count('people', 'my query', options) | ||
202 | + end | ||
203 | + | ||
204 | + should 'count enterprises without query but with options' do | ||
205 | + options = mock | ||
206 | + results = mock | ||
207 | + | ||
208 | + @finder.expects(:find).with('people', nil, options).returns(results) | ||
209 | + | ||
210 | + results.expects(:size).returns(99) | ||
211 | + | ||
212 | + assert_equal 99, @finder.count('people', nil, options) | ||
213 | + end | ||
160 | 214 | ||
161 | should 'not list more people than limit' do | 215 | should 'not list more people than limit' do |
162 | p1 = create_user('test1').person; p1.add_category(@category) | 216 | p1 = create_user('test1').person; p1.add_category(@category) |
test/unit/environment_finder_test.rb
@@ -51,12 +51,58 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | @@ -51,12 +51,58 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | ||
51 | assert_not_includes recent, ent1 # older | 51 | assert_not_includes recent, ent1 # older |
52 | end | 52 | end |
53 | 53 | ||
54 | - should 'count entrprises' do | 54 | + should 'count enterprises' do |
55 | finder = EnvironmentFinder.new(Environment.default) | 55 | finder = EnvironmentFinder.new(Environment.default) |
56 | count = finder.count('enterprises') | 56 | count = finder.count('enterprises') |
57 | - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | 57 | + Enterprise.create!(:name => 'teste1', :identifier => 'teste1') |
58 | assert_equal count+1, finder.count('enterprises') | 58 | assert_equal count+1, finder.count('enterprises') |
59 | end | 59 | end |
60 | + should 'count people' do | ||
61 | + finder = EnvironmentFinder.new(Environment.default) | ||
62 | + count = finder.count('people') | ||
63 | + create_user('testinguser') | ||
64 | + assert_equal count+1, finder.count('people') | ||
65 | + end | ||
66 | + should 'count products' do | ||
67 | + finder = EnvironmentFinder.new(Environment.default) | ||
68 | + count = finder.count('products') | ||
69 | + | ||
70 | + ent = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
71 | + ent.products.create!(:name => 'test prodduct') | ||
72 | + | ||
73 | + assert_equal count+1, finder.count('products') | ||
74 | + end | ||
75 | + should 'count articles' do | ||
76 | + finder = EnvironmentFinder.new(Environment.default) | ||
77 | + | ||
78 | + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
79 | + | ||
80 | + count = finder.count('articles') | ||
81 | + ent1.articles.create!(:name => 'teste1') | ||
82 | + | ||
83 | + assert_equal count+1, finder.count('articles') | ||
84 | + end | ||
85 | + should 'count events' do | ||
86 | + finder = EnvironmentFinder.new(Environment.default) | ||
87 | + count = finder.count('events') | ||
88 | + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
89 | + | ||
90 | + Event.create!(:name => 'teste2', :profile => ent1, :start_date => Date.today) | ||
91 | + assert_equal count+1, finder.count('events') | ||
92 | + end | ||
93 | + | ||
94 | + should 'count enterprises with query and options' do | ||
95 | + env = Environment.default | ||
96 | + finder = EnvironmentFinder.new(env) | ||
97 | + options = mock | ||
98 | + results = mock | ||
99 | + | ||
100 | + finder.expects(:find).with('people', 'my query', options).returns(results) | ||
101 | + | ||
102 | + results.expects(:total_hits).returns(99) | ||
103 | + | ||
104 | + assert_equal 99, finder.count('people', 'my query', options) | ||
105 | + end | ||
60 | 106 | ||
61 | should 'find articles by initial' do | 107 | should 'find articles by initial' do |
62 | person = create_user('teste').person | 108 | person = create_user('teste').person |