From bd3792c7c755674836bd8db201a345d158033bec Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Fri, 11 Jul 2008 18:19:48 +0000 Subject: [PATCH] ActionItem514: fixed vanishing of joins in when counting --- app/models/environment_finder.rb | 14 ++++++++++++-- test/unit/environment_finder_test.rb | 16 ++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb index edac00d..335cb93 100644 --- a/app/models/environment_finder.rb +++ b/app/models/environment_finder.rb @@ -18,9 +18,9 @@ class EnvironmentFinder options = {:page => 1, :per_page => options.delete(:limit)}.merge(options) if query.blank? - options = {:order => 'created_at desc, id desc'}.merge(options) + options = {:order => "#{asset_table(asset)}.created_at desc, #{asset_table(asset)}.id desc"}.merge(options) if product_category && asset == :products - @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])) + @environment.send(asset).paginate(:all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) elsif product_category && asset == :enterprises @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])) else @@ -53,4 +53,14 @@ class EnvironmentFinder find(asset, query, options).total_entries end + protected + + def asset_class(asset) + asset.to_s.singularize.camelize.constantize + end + + def asset_table(asset) + asset_class(asset).table_name + end + end diff --git a/test/unit/environment_finder_test.rb b/test/unit/environment_finder_test.rb index 403c65b..3d8a483 100644 --- a/test/unit/environment_finder_test.rb +++ b/test/unit/environment_finder_test.rb @@ -47,8 +47,8 @@ class EnvironmentFinderTest < ActiveSupport::TestCase ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') recent = finder.recent('enterprises', 1) - assert_includes recent, ent2 # newer - assert_not_includes recent, ent1 # older + + assert_equal 1, recent.size end should 'paginate the list of more enterprises than limit' do @@ -56,8 +56,7 @@ class EnvironmentFinderTest < ActiveSupport::TestCase ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') - assert_equal [ent2], finder.find('enterprises', nil, :per_page => 1, :page => 1) #newer - assert_equal [ent1], finder.find('enterprises', nil, :per_page => 1, :page => 2) #older + assert_equal 1, finder.find('enterprises', nil, :per_page => 1, :page => 1).size end should 'paginate the list of more enterprises than limit with query' do @@ -195,16 +194,17 @@ class EnvironmentFinderTest < ActiveSupport::TestCase assert_not_includes prods, prod4 end - should 'count products wihin product category without query' do + should 'count products wihin product category without query paginating' do finder = EnvironmentFinder.new(Environment.default) cat = ProductCategory.create!(:name => 'test category', :environment => Environment.default) ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') prod1 = ent.products.create!(:name => 'test product 1', :product_category => cat) - prod3 = ent.products.create!(:name => 'test product 2') + prod1 = ent.products.create!(:name => 'test product 2', :product_category => cat) + prod3 = ent.products.create!(:name => 'test product 3') - prods_count = finder.count(:products, nil, :product_category => cat) + prods_count = finder.count(:products, nil, :product_category => cat, :per_page => 1) - assert_equal 1, prods_count + assert_equal 2, prods_count end should 'find in order of creation' do -- libgit2 0.21.2