Commit bd3792c7c755674836bd8db201a345d158033bec
1 parent
2cfd2bef
Exists in
master
and in
23 other branches
ActionItem514: fixed vanishing of joins in when counting
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2209 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
20 additions
and
10 deletions
Show diff stats
app/models/environment_finder.rb
| ... | ... | @@ -18,9 +18,9 @@ class EnvironmentFinder |
| 18 | 18 | |
| 19 | 19 | options = {:page => 1, :per_page => options.delete(:limit)}.merge(options) |
| 20 | 20 | if query.blank? |
| 21 | - options = {:order => 'created_at desc, id desc'}.merge(options) | |
| 21 | + options = {:order => "#{asset_table(asset)}.created_at desc, #{asset_table(asset)}.id desc"}.merge(options) | |
| 22 | 22 | if product_category && asset == :products |
| 23 | - @environment.send(asset).paginate(:all, options.merge(:joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) | |
| 23 | + @environment.send(asset).paginate(:all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) | |
| 24 | 24 | elsif product_category && asset == :enterprises |
| 25 | 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])) |
| 26 | 26 | else |
| ... | ... | @@ -53,4 +53,14 @@ class EnvironmentFinder |
| 53 | 53 | find(asset, query, options).total_entries |
| 54 | 54 | end |
| 55 | 55 | |
| 56 | + protected | |
| 57 | + | |
| 58 | + def asset_class(asset) | |
| 59 | + asset.to_s.singularize.camelize.constantize | |
| 60 | + end | |
| 61 | + | |
| 62 | + def asset_table(asset) | |
| 63 | + asset_class(asset).table_name | |
| 64 | + end | |
| 65 | + | |
| 56 | 66 | end | ... | ... |
test/unit/environment_finder_test.rb
| ... | ... | @@ -47,8 +47,8 @@ class EnvironmentFinderTest < ActiveSupport::TestCase |
| 47 | 47 | ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') |
| 48 | 48 | ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') |
| 49 | 49 | recent = finder.recent('enterprises', 1) |
| 50 | - assert_includes recent, ent2 # newer | |
| 51 | - assert_not_includes recent, ent1 # older | |
| 50 | + | |
| 51 | + assert_equal 1, recent.size | |
| 52 | 52 | end |
| 53 | 53 | |
| 54 | 54 | should 'paginate the list of more enterprises than limit' do |
| ... | ... | @@ -56,8 +56,7 @@ class EnvironmentFinderTest < ActiveSupport::TestCase |
| 56 | 56 | ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') |
| 57 | 57 | ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') |
| 58 | 58 | |
| 59 | - assert_equal [ent2], finder.find('enterprises', nil, :per_page => 1, :page => 1) #newer | |
| 60 | - assert_equal [ent1], finder.find('enterprises', nil, :per_page => 1, :page => 2) #older | |
| 59 | + assert_equal 1, finder.find('enterprises', nil, :per_page => 1, :page => 1).size | |
| 61 | 60 | end |
| 62 | 61 | |
| 63 | 62 | should 'paginate the list of more enterprises than limit with query' do |
| ... | ... | @@ -195,16 +194,17 @@ class EnvironmentFinderTest < ActiveSupport::TestCase |
| 195 | 194 | assert_not_includes prods, prod4 |
| 196 | 195 | end |
| 197 | 196 | |
| 198 | - should 'count products wihin product category without query' do | |
| 197 | + should 'count products wihin product category without query paginating' do | |
| 199 | 198 | finder = EnvironmentFinder.new(Environment.default) |
| 200 | 199 | cat = ProductCategory.create!(:name => 'test category', :environment => Environment.default) |
| 201 | 200 | ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') |
| 202 | 201 | prod1 = ent.products.create!(:name => 'test product 1', :product_category => cat) |
| 203 | - prod3 = ent.products.create!(:name => 'test product 2') | |
| 202 | + prod1 = ent.products.create!(:name => 'test product 2', :product_category => cat) | |
| 203 | + prod3 = ent.products.create!(:name => 'test product 3') | |
| 204 | 204 | |
| 205 | - prods_count = finder.count(:products, nil, :product_category => cat) | |
| 205 | + prods_count = finder.count(:products, nil, :product_category => cat, :per_page => 1) | |
| 206 | 206 | |
| 207 | - assert_equal 1, prods_count | |
| 207 | + assert_equal 2, prods_count | |
| 208 | 208 | end |
| 209 | 209 | |
| 210 | 210 | should 'find in order of creation' do | ... | ... |