Commit aa2cdafb841f014371e90cd18dc38d55a45b8e14
1 parent
cdfe18f7
Exists in
master
and in
28 other branches
ActionItem514: changed the counting of product categories hits in
environment finder git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2213 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
130 additions
and
72 deletions
Show diff stats
app/models/environment_finder.rb
| @@ -50,13 +50,34 @@ class EnvironmentFinder | @@ -50,13 +50,34 @@ class EnvironmentFinder | ||
| 50 | find(asset, nil, :limit => limit) | 50 | find(asset, nil, :limit => limit) |
| 51 | end | 51 | end |
| 52 | 52 | ||
| 53 | - def count(asset, query = '', options = {}) | ||
| 54 | - if query.blank? | ||
| 55 | - find(asset, query, options.except(:page, :per_page), 'count') | 53 | + def product_categories_count(asset, product_categories_ids, objects_ids=nil) |
| 54 | + conditions = ['product_categorizations.category_id in (?)', product_categories_ids] | ||
| 55 | + | ||
| 56 | + if asset == :products | ||
| 57 | + if objects_ids | ||
| 58 | + conditions[0] += ' and product_categorizations.product_id in (?)' | ||
| 59 | + conditions << objects_ids | ||
| 60 | + end | ||
| 61 | + ProductCategory.find(:all, :select => 'categories.id, count(*) as total', :joins => 'inner join product_categorizations on (product_categorizations.category_id = categories.id)', :group => 'categories.id', :conditions => conditions ) | ||
| 62 | + elsif asset == :enterprises | ||
| 63 | + if objects_ids | ||
| 64 | + conditions[0] += ' and products.enterprise_id in (?)' | ||
| 65 | + conditions << objects_ids | ||
| 66 | + end | ||
| 67 | + ProductCategory.find( | ||
| 68 | + :all, | ||
| 69 | + :select => 'categories.id, count(distinct products.enterprise_id) as total', | ||
| 70 | + :joins => 'inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id)', | ||
| 71 | + :group => 'categories.id', | ||
| 72 | + :conditions => conditions | ||
| 73 | + ) | ||
| 56 | else | 74 | else |
| 57 | - # will_paginate needs a page | ||
| 58 | - find(asset, query, {:page => 1}.merge(options)).total_entries | 75 | + raise ArgumentError, 'only products and enterprises supported' |
| 76 | + end.inject({}) do |results,pc| | ||
| 77 | + results[pc.id]= pc.total.to_i | ||
| 78 | + results | ||
| 59 | end | 79 | end |
| 80 | + | ||
| 60 | end | 81 | end |
| 61 | 82 | ||
| 62 | protected | 83 | protected |
test/unit/environment_finder_test.rb
| @@ -70,60 +70,7 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | @@ -70,60 +70,7 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | ||
| 70 | 70 | ||
| 71 | assert (p1 == [ent1] && p2 == [ent2]) || (p1 == [ent2] && p2 == [ent1]) | 71 | assert (p1 == [ent1] && p2 == [ent2]) || (p1 == [ent2] && p2 == [ent1]) |
| 72 | end | 72 | end |
| 73 | - | ||
| 74 | - should 'count enterprises' do | ||
| 75 | - finder = EnvironmentFinder.new(Environment.default) | ||
| 76 | - count = finder.count('enterprises') | ||
| 77 | - Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
| 78 | - assert_equal count+1, finder.count('enterprises') | ||
| 79 | - end | ||
| 80 | - | ||
| 81 | - should 'count people' do | ||
| 82 | - finder = EnvironmentFinder.new(Environment.default) | ||
| 83 | - count = finder.count('people') | ||
| 84 | - create_user('testinguser') | ||
| 85 | - assert_equal count+1, finder.count('people') | ||
| 86 | - end | ||
| 87 | - should 'count products' do | ||
| 88 | - finder = EnvironmentFinder.new(Environment.default) | ||
| 89 | - count = finder.count('products') | ||
| 90 | - | ||
| 91 | - ent = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
| 92 | - ent.products.create!(:name => 'test prodduct') | ||
| 93 | - | ||
| 94 | - assert_equal count+1, finder.count('products') | ||
| 95 | - end | ||
| 96 | - should 'count articles' do | ||
| 97 | - finder = EnvironmentFinder.new(Environment.default) | ||
| 98 | - | ||
| 99 | - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
| 100 | - | ||
| 101 | - count = finder.count('articles') | ||
| 102 | - ent1.articles.create!(:name => 'teste1') | ||
| 103 | - | ||
| 104 | - assert_equal count+1, finder.count('articles') | ||
| 105 | - end | ||
| 106 | - should 'count events' do | ||
| 107 | - finder = EnvironmentFinder.new(Environment.default) | ||
| 108 | - count = finder.count('events') | ||
| 109 | - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | ||
| 110 | - | ||
| 111 | - Event.create!(:name => 'teste2', :profile => ent1, :start_date => Date.today) | ||
| 112 | - assert_equal count+1, finder.count('events') | ||
| 113 | - end | ||
| 114 | - | ||
| 115 | - should 'count enterprises with query and options' do | ||
| 116 | - env = Environment.default | ||
| 117 | - finder = EnvironmentFinder.new(env) | ||
| 118 | - results = mock | ||
| 119 | - | ||
| 120 | - finder.expects(:find).with('people', 'my query', kind_of(Hash)).returns(results) | ||
| 121 | - | ||
| 122 | - results.expects(:total_entries).returns(99) | ||
| 123 | - | ||
| 124 | - assert_equal 99, finder.count('people', 'my query', {}) | ||
| 125 | - end | ||
| 126 | - | 73 | + |
| 127 | should 'find person and enterprise by radius and region' do | 74 | should 'find person and enterprise by radius and region' do |
| 128 | finder = EnvironmentFinder.new(Environment.default) | 75 | finder = EnvironmentFinder.new(Environment.default) |
| 129 | 76 | ||
| @@ -194,19 +141,6 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | @@ -194,19 +141,6 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | ||
| 194 | assert_not_includes prods, prod4 | 141 | assert_not_includes prods, prod4 |
| 195 | end | 142 | end |
| 196 | 143 | ||
| 197 | - should 'count products wihin product category without query paginating' do | ||
| 198 | - finder = EnvironmentFinder.new(Environment.default) | ||
| 199 | - cat = ProductCategory.create!(:name => 'test category', :environment => Environment.default) | ||
| 200 | - ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') | ||
| 201 | - prod1 = ent.products.create!(:name => 'test product 1', :product_category => cat) | ||
| 202 | - prod1 = ent.products.create!(:name => 'test product 2', :product_category => cat) | ||
| 203 | - prod3 = ent.products.create!(:name => 'test product 3') | ||
| 204 | - | ||
| 205 | - prods_count = finder.count(:products, nil, :product_category => cat, :per_page => 1) | ||
| 206 | - | ||
| 207 | - assert_equal 2, prods_count | ||
| 208 | - end | ||
| 209 | - | ||
| 210 | should 'find in order of creation' do | 144 | should 'find in order of creation' do |
| 211 | finder = EnvironmentFinder.new(Environment.default) | 145 | finder = EnvironmentFinder.new(Environment.default) |
| 212 | ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') | 146 | ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') |
| @@ -267,4 +201,107 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | @@ -267,4 +201,107 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | ||
| 267 | assert_includes ents, ent1 | 201 | assert_includes ents, ent1 |
| 268 | assert_not_includes ents, ent2 | 202 | assert_not_includes ents, ent2 |
| 269 | end | 203 | end |
| 204 | + | ||
| 205 | + should 'count product categories results by products' do | ||
| 206 | + finder = EnvironmentFinder.new(Environment.default) | ||
| 207 | + | ||
| 208 | + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) | ||
| 209 | + pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) | ||
| 210 | + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) | ||
| 211 | + pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) | ||
| 212 | + | ||
| 213 | + | ||
| 214 | + ent = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') | ||
| 215 | + p1 = ent.products.create!(:name => 'test product 1', :product_category => pc1) | ||
| 216 | + p2 = ent.products.create!(:name => 'test product 2', :product_category => pc11) | ||
| 217 | + p3 = ent.products.create!(:name => 'test product 3', :product_category => pc2) | ||
| 218 | + p4 = ent.products.create!(:name => 'test product 4', :product_category => pc2) # not in the count | ||
| 219 | + p5 = ent.products.create!(:name => 'test product 5', :product_category => pc3) # not in the count | ||
| 220 | + | ||
| 221 | + counts = finder.product_categories_count(:products, [pc1.id, pc11.id, pc2.id], [p1.id, p2.id, p3.id] ) | ||
| 222 | + | ||
| 223 | + assert_equal 2, counts[pc1.id] | ||
| 224 | + assert_equal 1, counts[pc11.id] | ||
| 225 | + assert_equal 1, counts[pc2.id] | ||
| 226 | + assert_nil counts[pc3.id] | ||
| 227 | + end | ||
| 228 | + | ||
| 229 | + should 'count product categories results by all products' do | ||
| 230 | + finder = EnvironmentFinder.new(Environment.default) | ||
| 231 | + | ||
| 232 | + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) | ||
| 233 | + pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) | ||
| 234 | + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) | ||
| 235 | + pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) | ||
| 236 | + | ||
| 237 | + | ||
| 238 | + ent = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') | ||
| 239 | + p1 = ent.products.create!(:name => 'test product 1', :product_category => pc1) | ||
| 240 | + p2 = ent.products.create!(:name => 'test product 2', :product_category => pc11) | ||
| 241 | + p3 = ent.products.create!(:name => 'test product 3', :product_category => pc2) | ||
| 242 | + p4 = ent.products.create!(:name => 'test product 4', :product_category => pc3) # not in the count | ||
| 243 | + | ||
| 244 | + counts = finder.product_categories_count(:products, [pc1.id, pc11.id, pc2.id] ) | ||
| 245 | + | ||
| 246 | + assert_equal 2, counts[pc1.id] | ||
| 247 | + assert_equal 1, counts[pc11.id] | ||
| 248 | + assert_equal 1, counts[pc2.id] | ||
| 249 | + assert_nil counts[pc3.id] | ||
| 250 | + end | ||
| 251 | + | ||
| 252 | + should 'count product categories results by enterprises' do | ||
| 253 | + finder = EnvironmentFinder.new(Environment.default) | ||
| 254 | + | ||
| 255 | + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) | ||
| 256 | + pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) | ||
| 257 | + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) | ||
| 258 | + pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) | ||
| 259 | + | ||
| 260 | + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') | ||
| 261 | + ent1.products.create!(:name => 'test product 1', :product_category => pc1) | ||
| 262 | + ent1.products.create!(:name => 'test product 2', :product_category => pc1) | ||
| 263 | + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') | ||
| 264 | + ent2.products.create!(:name => 'test product 2', :product_category => pc11) | ||
| 265 | + ent3 = Enterprise.create!(:name => 'test enterprise 3', :identifier => 'test_ent3') | ||
| 266 | + ent3.products.create!(:name => 'test product 3', :product_category => pc2) | ||
| 267 | + ent4 = Enterprise.create!(:name => 'test enterprise 4', :identifier => 'test_ent4') | ||
| 268 | + ent4.products.create!(:name => 'test product 4', :product_category => pc2) | ||
| 269 | + ent5 = Enterprise.create!(:name => 'test enterprise 5', :identifier => 'test_ent5') # not in the count | ||
| 270 | + ent5.products.create!(:name => 'test product 5', :product_category => pc2) | ||
| 271 | + ent5.products.create!(:name => 'test product 6', :product_category => pc3) | ||
| 272 | + | ||
| 273 | + counts = finder.product_categories_count(:enterprises, [pc1.id, pc11.id, pc2.id], [ent1.id, ent2.id, ent3.id, ent4.id] ) | ||
| 274 | + | ||
| 275 | + assert_equal 2, counts[pc1.id] | ||
| 276 | + assert_equal 1, counts[pc11.id] | ||
| 277 | + assert_equal 2, counts[pc2.id] | ||
| 278 | + assert_nil counts[pc3.id] | ||
| 279 | + end | ||
| 280 | + | ||
| 281 | + should 'count product categories results by all enterprises' do | ||
| 282 | + finder = EnvironmentFinder.new(Environment.default) | ||
| 283 | + | ||
| 284 | + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) | ||
| 285 | + pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) | ||
| 286 | + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) | ||
| 287 | + pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) | ||
| 288 | + | ||
| 289 | + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') | ||
| 290 | + ent1.products.create!(:name => 'test product 1', :product_category => pc1) | ||
| 291 | + ent1.products.create!(:name => 'test product 2', :product_category => pc1) | ||
| 292 | + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') | ||
| 293 | + ent2.products.create!(:name => 'test product 2', :product_category => pc11) | ||
| 294 | + ent3 = Enterprise.create!(:name => 'test enterprise 3', :identifier => 'test_ent3') | ||
| 295 | + ent3.products.create!(:name => 'test product 3', :product_category => pc2) | ||
| 296 | + ent4 = Enterprise.create!(:name => 'test enterprise 4', :identifier => 'test_ent4') | ||
| 297 | + ent4.products.create!(:name => 'test product 4', :product_category => pc2) | ||
| 298 | + ent4.products.create!(:name => 'test product 5', :product_category => pc3) | ||
| 299 | + | ||
| 300 | + counts = finder.product_categories_count(:enterprises, [pc1.id, pc11.id, pc2.id] ) | ||
| 301 | + | ||
| 302 | + assert_equal 2, counts[pc1.id] | ||
| 303 | + assert_equal 1, counts[pc11.id] | ||
| 304 | + assert_equal 2, counts[pc2.id] | ||
| 305 | + assert_nil counts[pc3.id] | ||
| 306 | + end | ||
| 270 | end | 307 | end |