Commit db2488c581857f0b9a9a0cc7e9b8b18f27b39936
1 parent
bd3792c7
Exists in
master
and in
29 other branches
ActionItem514: reduce by half the number of queries to count search results
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2210 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
1 changed file
with
15 additions
and
9 deletions
Show diff stats
app/models/environment_finder.rb
... | ... | @@ -4,7 +4,7 @@ class EnvironmentFinder |
4 | 4 | @environment = env |
5 | 5 | end |
6 | 6 | |
7 | - def find(asset, query = nil, options={}) | |
7 | + def find(asset, query = nil, options={}, finder_method = 'paginate') | |
8 | 8 | @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) |
9 | 9 | if @region && options[:within] |
10 | 10 | options[:origin] = [@region.lat, @region.lng] |
... | ... | @@ -16,18 +16,21 @@ class EnvironmentFinder |
16 | 16 | |
17 | 17 | date_range = options.delete(:date_range) |
18 | 18 | |
19 | - options = {:page => 1, :per_page => options.delete(:limit)}.merge(options) | |
19 | + if finder_method == 'paginate' | |
20 | + options = {:page => 1, :per_page => options.delete(:limit)}.merge(options) | |
21 | + end | |
22 | + | |
20 | 23 | if query.blank? |
21 | 24 | options = {:order => "#{asset_table(asset)}.created_at desc, #{asset_table(asset)}.id desc"}.merge(options) |
22 | 25 | if product_category && asset == :products |
23 | - @environment.send(asset).paginate(:all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) | |
26 | + @environment.send(asset).send(finder_method, :all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) | |
24 | 27 | elsif product_category && asset == :enterprises |
25 | - @environment.send(asset).paginate(:all, options.merge(:order => 'profiles.created_at desc, profiles.id desc', :include => 'products', :joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) | |
28 | + @environment.send(asset).send(finder_method, :all, options.merge(:order => 'profiles.created_at desc, profiles.id desc', :include => 'products', :joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) | |
26 | 29 | else |
27 | 30 | if (asset == :events) && date_range |
28 | - @environment.send(asset).paginate(:all, options.merge(:conditions => { :start_date => date_range})) | |
31 | + @environment.send(asset).send(finder_method, :all, options.merge(:conditions => { :start_date => date_range})) | |
29 | 32 | else |
30 | - @environment.send(asset).paginate(:all, options) | |
33 | + @environment.send(asset).send(finder_method, :all, options) | |
31 | 34 | end |
32 | 35 | end |
33 | 36 | else |
... | ... | @@ -48,9 +51,12 @@ class EnvironmentFinder |
48 | 51 | end |
49 | 52 | |
50 | 53 | def count(asset, query = '', options = {}) |
51 | - # because will_paginate needs a page | |
52 | - options = {:page => 1}.merge(options) | |
53 | - find(asset, query, options).total_entries | |
54 | + if query.blank? | |
55 | + find(asset, query, options.except(:page, :per_page), 'count') | |
56 | + else | |
57 | + # will_paginate needs a page | |
58 | + find(asset, query, {:page => 1}.merge(options)).total_entries | |
59 | + end | |
54 | 60 | end |
55 | 61 | |
56 | 62 | protected | ... | ... |