Commit d2c2650deb5b9982e6119c81108fd5989a442e49
1 parent
aa2cdafb
Exists in
master
and in
28 other branches
ActionItem514: made a category finder method to calculate count for
product categories inside a filter git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2214 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
3 changed files
with
146 additions
and
64 deletions
Show diff stats
app/controllers/public/search_controller.rb
| ... | ... | @@ -89,6 +89,11 @@ class SearchController < ApplicationController |
| 89 | 89 | def load_product_categories_menu(asset) |
| 90 | 90 | @results[asset].uniq! |
| 91 | 91 | # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop |
| 92 | + | |
| 93 | + cats = ProductCategory.menu_categories(@product_category, environment) | |
| 94 | + cats += cats.map(:children).flatten | |
| 95 | + product_categories_ids = cats.map(&:id) | |
| 96 | + | |
| 92 | 97 | @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| |
| 93 | 98 | hits = @finder.count(asset, @filtered_query, calculate_find_options(asset, nil, nil, cat, @region, params[:radius], nil, nil)) |
| 94 | 99 | childs = [] | ... | ... |
app/models/category_finder.rb
| ... | ... | @@ -30,12 +30,6 @@ class CategoryFinder |
| 30 | 30 | find(asset, nil, :limit => limit) |
| 31 | 31 | end |
| 32 | 32 | |
| 33 | - def count(asset, query='', options={}) | |
| 34 | - # because will_paginate needs a page | |
| 35 | - options = {:page => 1}.merge(options) | |
| 36 | - find(asset, query, options).total_entries | |
| 37 | - end | |
| 38 | - | |
| 39 | 33 | def most_commented_articles(limit=10, options={}) |
| 40 | 34 | options = {:page => 1, :per_page => limit, :order => 'comments_count DESC'}.merge(options) |
| 41 | 35 | Article.paginate(:all, options_for_find(Article, options)) |
| ... | ... | @@ -54,6 +48,41 @@ class CategoryFinder |
| 54 | 48 | Event.paginate(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => 'start_date' }.merge(options)) |
| 55 | 49 | end |
| 56 | 50 | |
| 51 | + def product_categories_count(asset, product_categories_ids, objects_ids=nil) | |
| 52 | + conditions = [ "product_categorizations.category_id in (?) and #{ProfileCategorization.table_name}.category_id = ?", product_categories_ids, category_id] | |
| 53 | + | |
| 54 | + if asset == :products | |
| 55 | + if objects_ids | |
| 56 | + conditions[0] += ' and product_categorizations.product_id in (?)' | |
| 57 | + conditions << objects_ids | |
| 58 | + end | |
| 59 | + ProductCategory.find( | |
| 60 | + :all, | |
| 61 | + :select => 'categories.id, count(*) as total', | |
| 62 | + :joins => "inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id) inner join #{ProfileCategorization.table_name} on (#{ProfileCategorization.table_name}.profile_id = products.enterprise_id)", | |
| 63 | + :group => 'categories.id', | |
| 64 | + :conditions => conditions | |
| 65 | + ) | |
| 66 | + elsif asset == :enterprises | |
| 67 | + if objects_ids | |
| 68 | + conditions[0] += ' and products.enterprise_id in (?)' | |
| 69 | + conditions << objects_ids | |
| 70 | + end | |
| 71 | + ProductCategory.find( | |
| 72 | + :all, | |
| 73 | + :select => 'categories.id, count(distinct products.enterprise_id) as total', | |
| 74 | + :joins => "inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id) inner join #{ProfileCategorization.table_name} on (#{ProfileCategorization.table_name}.profile_id = products.enterprise_id)", | |
| 75 | + :group => 'categories.id', | |
| 76 | + :conditions => conditions | |
| 77 | + ) | |
| 78 | + else | |
| 79 | + raise ArgumentError, 'only products and enterprises supported' | |
| 80 | + end.inject({}) do |results,pc| | |
| 81 | + results[pc.id]= pc.total.to_i | |
| 82 | + results | |
| 83 | + end | |
| 84 | + end | |
| 85 | + | |
| 57 | 86 | protected |
| 58 | 87 | |
| 59 | 88 | def options_for_find(klass, options={}, date_range = nil) | ... | ... |
test/unit/category_finder_test.rb
| ... | ... | @@ -148,64 +148,6 @@ class CategoryFinderTest < ActiveSupport::TestCase |
| 148 | 148 | assert_respond_to p2, :total_entries |
| 149 | 149 | assert (p1 == [ent1] && p2 == [ent2]) || (p1 == [ent2] && p2 == [ent1]) # consistent paging |
| 150 | 150 | end |
| 151 | - | |
| 152 | - should 'count enterprises' do | |
| 153 | - count = @finder.count('enterprises') | |
| 154 | - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) | |
| 155 | - assert_equal count+1, @finder.count('enterprises') | |
| 156 | - end | |
| 157 | - | |
| 158 | - should 'count people' do | |
| 159 | - count = @finder.count('people') | |
| 160 | - p = create_user('testinguser').person | |
| 161 | - p.category_ids = [@category.id] | |
| 162 | - p.save! | |
| 163 | - | |
| 164 | - assert_equal count+1, @finder.count('people') | |
| 165 | - end | |
| 166 | - should 'count products' do | |
| 167 | - count = @finder.count('products') | |
| 168 | - | |
| 169 | - ent = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) | |
| 170 | - ent.products.create!(:name => 'test prodduct') | |
| 171 | - | |
| 172 | - assert_equal count+1, @finder.count('products') | |
| 173 | - end | |
| 174 | - should 'count articles' do | |
| 175 | - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | |
| 176 | - | |
| 177 | - count = @finder.count('articles') | |
| 178 | - ent1.articles.create!(:name => 'teste1', :category_ids => [@category.id]) | |
| 179 | - | |
| 180 | - assert_equal count+1, @finder.count('articles') | |
| 181 | - end | |
| 182 | - should 'count events' do | |
| 183 | - count = @finder.count('events') | |
| 184 | - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') | |
| 185 | - | |
| 186 | - Event.create!(:name => 'teste2', :profile => ent1, :start_date => Date.today, :category_ids => [@category.id]) | |
| 187 | - assert_equal count+1, @finder.count('events') | |
| 188 | - end | |
| 189 | - | |
| 190 | - should 'count enterprises with query and options' do | |
| 191 | - results = mock | |
| 192 | - | |
| 193 | - @finder.expects(:find).with('people', 'my query', kind_of(Hash)).returns(results) | |
| 194 | - | |
| 195 | - results.expects(:total_entries).returns(99) | |
| 196 | - | |
| 197 | - assert_equal 99, @finder.count('people', 'my query', {}) | |
| 198 | - end | |
| 199 | - | |
| 200 | - should 'count enterprises without query but with options' do | |
| 201 | - results = mock | |
| 202 | - | |
| 203 | - @finder.expects(:find).with('people', nil, kind_of(Hash)).returns(results) | |
| 204 | - | |
| 205 | - results.expects(:total_entries).returns(99) | |
| 206 | - | |
| 207 | - assert_equal 99, @finder.count('people', nil, {}) | |
| 208 | - end | |
| 209 | 151 | |
| 210 | 152 | should 'not list more people than limit' do |
| 211 | 153 | p1 = create_user('test1').person; p1.add_category(@category) |
| ... | ... | @@ -384,4 +326,110 @@ class CategoryFinderTest < ActiveSupport::TestCase |
| 384 | 326 | assert_not_includes ents, ent2 |
| 385 | 327 | end |
| 386 | 328 | |
| 329 | + should 'count product categories results by products' do | |
| 330 | + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) | |
| 331 | + pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) | |
| 332 | + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) | |
| 333 | + pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) | |
| 334 | + | |
| 335 | + ent = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) | |
| 336 | + p1 = ent.products.create!(:name => 'test product 1', :product_category => pc1) | |
| 337 | + p2 = ent.products.create!(:name => 'test product 2', :product_category => pc11) | |
| 338 | + p3 = ent.products.create!(:name => 'test product 3', :product_category => pc2) | |
| 339 | + p4 = ent.products.create!(:name => 'test product 4', :product_category => pc2) # not in the count | |
| 340 | + p5 = ent.products.create!(:name => 'test product 5', :product_category => pc3) # not in the count | |
| 341 | + | |
| 342 | + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') | |
| 343 | + p6 = ent2.products.create!(:name => 'test product 6', :product_category => pc1) | |
| 344 | + | |
| 345 | + counts = @finder.product_categories_count(:products, [pc1.id, pc11.id, pc2.id], [p1.id, p2.id, p3.id, p5.id, p6.id] ) | |
| 346 | + | |
| 347 | + assert_equal 2, counts[pc1.id] | |
| 348 | + assert_equal 1, counts[pc11.id] | |
| 349 | + assert_equal 1, counts[pc2.id] | |
| 350 | + assert_nil counts[pc3.id] | |
| 351 | + end | |
| 352 | + | |
| 353 | + should 'count product categories results by all products' do | |
| 354 | + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) | |
| 355 | + pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) | |
| 356 | + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) | |
| 357 | + pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) | |
| 358 | + | |
| 359 | + ent = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) | |
| 360 | + p1 = ent.products.create!(:name => 'test product 1', :product_category => pc1) | |
| 361 | + p2 = ent.products.create!(:name => 'test product 2', :product_category => pc11) | |
| 362 | + p3 = ent.products.create!(:name => 'test product 3', :product_category => pc2) | |
| 363 | + p4 = ent.products.create!(:name => 'test product 4', :product_category => pc3) # not in the count | |
| 364 | + | |
| 365 | + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') | |
| 366 | + p6 = ent2.products.create!(:name => 'test product 6', :product_category => pc1) | |
| 367 | + | |
| 368 | + | |
| 369 | + counts = @finder.product_categories_count(:products, [pc1.id, pc11.id, pc2.id] ) | |
| 370 | + | |
| 371 | + assert_equal 2, counts[pc1.id] | |
| 372 | + assert_equal 1, counts[pc11.id] | |
| 373 | + assert_equal 1, counts[pc2.id] | |
| 374 | + assert_nil counts[pc3.id] | |
| 375 | + end | |
| 376 | + | |
| 377 | + should 'count product categories results by enterprises' do | |
| 378 | + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) | |
| 379 | + pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) | |
| 380 | + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) | |
| 381 | + pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) | |
| 382 | + | |
| 383 | + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) | |
| 384 | + ent1.products.create!(:name => 'test product 1', :product_category => pc1) | |
| 385 | + ent1.products.create!(:name => 'test product 2', :product_category => pc1) | |
| 386 | + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2', :category_ids => [@category.id]) | |
| 387 | + ent2.products.create!(:name => 'test product 2', :product_category => pc11) | |
| 388 | + ent3 = Enterprise.create!(:name => 'test enterprise 3', :identifier => 'test_ent3', :category_ids => [@category.id]) | |
| 389 | + ent3.products.create!(:name => 'test product 3', :product_category => pc2) | |
| 390 | + ent4 = Enterprise.create!(:name => 'test enterprise 4', :identifier => 'test_ent4', :category_ids => [@category.id]) | |
| 391 | + ent4.products.create!(:name => 'test product 4', :product_category => pc2) | |
| 392 | + ent5 = Enterprise.create!(:name => 'test enterprise 5', :identifier => 'test_ent5', :category_ids => [@category.id]) | |
| 393 | + ent5.products.create!(:name => 'test product 5', :product_category => pc2) | |
| 394 | + ent5.products.create!(:name => 'test product 6', :product_category => pc3) | |
| 395 | + | |
| 396 | + ent6 = Enterprise.create!(:name => 'test enterprise 6', :identifier => 'test_ent6') | |
| 397 | + p6 = ent2.products.create!(:name => 'test product 6', :product_category => pc1) | |
| 398 | + | |
| 399 | + counts = @finder.product_categories_count(:enterprises, [pc1.id, pc11.id, pc2.id], [ent1.id, ent2.id, ent3.id, ent4.id] ) | |
| 400 | + | |
| 401 | + assert_equal 2, counts[pc1.id] | |
| 402 | + assert_equal 1, counts[pc11.id] | |
| 403 | + assert_equal 2, counts[pc2.id] | |
| 404 | + assert_nil counts[pc3.id] | |
| 405 | + end | |
| 406 | + | |
| 407 | + should 'count product categories results by all enterprises' do | |
| 408 | + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) | |
| 409 | + pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) | |
| 410 | + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) | |
| 411 | + pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) | |
| 412 | + | |
| 413 | + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) | |
| 414 | + ent1.products.create!(:name => 'test product 1', :product_category => pc1) | |
| 415 | + ent1.products.create!(:name => 'test product 2', :product_category => pc1) | |
| 416 | + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2', :category_ids => [@category.id]) | |
| 417 | + ent2.products.create!(:name => 'test product 2', :product_category => pc11) | |
| 418 | + ent3 = Enterprise.create!(:name => 'test enterprise 3', :identifier => 'test_ent3', :category_ids => [@category.id]) | |
| 419 | + ent3.products.create!(:name => 'test product 3', :product_category => pc2) | |
| 420 | + ent4 = Enterprise.create!(:name => 'test enterprise 4', :identifier => 'test_ent4', :category_ids => [@category.id]) | |
| 421 | + ent4.products.create!(:name => 'test product 4', :product_category => pc2) | |
| 422 | + ent4.products.create!(:name => 'test product 5', :product_category => pc3) | |
| 423 | + | |
| 424 | + ent5 = Enterprise.create!(:name => 'test enterprise 5', :identifier => 'test_ent5') | |
| 425 | + p6 = ent2.products.create!(:name => 'test product 6', :product_category => pc1) | |
| 426 | + | |
| 427 | + counts = @finder.product_categories_count(:enterprises, [pc1.id, pc11.id, pc2.id] ) | |
| 428 | + | |
| 429 | + assert_equal 2, counts[pc1.id] | |
| 430 | + assert_equal 1, counts[pc11.id] | |
| 431 | + assert_equal 2, counts[pc2.id] | |
| 432 | + assert_nil counts[pc3.id] | |
| 433 | + end | |
| 434 | + | |
| 387 | 435 | end | ... | ... |