Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added test for parent commit (removing :include from count in ActiveR…
…ecord 2.1)
  • Loading branch information
mislav committed May 16, 2008
1 parent 24260c3 commit b3caa0a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/will_paginate/finder.rb
Expand Up @@ -184,21 +184,23 @@ def wp_count(options, args, finder)
unless options[:select] and options[:select] =~ /^\s*DISTINCT\b/i
excludees << :select # only exclude the select param if it doesn't begin with DISTINCT
end

# we may be in a model or an association proxy!
klass = (@owner and @reflection) ? @reflection.klass : self

# count expects (almost) the same options as find
count_options = options.except *excludees

# remove :include option if it doesn't need
if count_options[:include] and !klass.send :references_eager_loaded_tables?, count_options
count_options.delete :include
end

# merge the hash found in :count
# this allows you to specify :select, :order, or anything else just for the count query
count_options.update options[:count] if options[:count]

# we may be in a model or an association proxy
klass = (@owner and @reflection) ? @reflection.klass : self

# forget about includes if they are irrelevant (Rails 2.1)
if count_options[:include] and
klass.private_methods.include?('references_eager_loaded_tables?') and
!klass.send(:references_eager_loaded_tables?, count_options)
count_options.delete :include
end

# we may have to scope ...
counter = Proc.new { count(count_options) }
Expand Down
18 changes: 18 additions & 0 deletions test/finder_test.rb
Expand Up @@ -412,5 +412,23 @@ def test_paginated_each

assert_equal 14, Developer.paginated_each(:page => '2') { }
end

# detect ActiveRecord 2.1
if ActiveRecord::Base.private_methods.include?('references_eager_loaded_tables?')
def test_removes_irrelevant_includes_in_count
Developer.expects(:find).returns([1])
Developer.expects(:count).with({}).returns(0)

Developer.paginate :page => 1, :per_page => 1, :include => :projects
end

def test_doesnt_remove_referenced_includes_in_count
Developer.expects(:find).returns([1])
Developer.expects(:count).with({ :include => :projects, :conditions => 'projects.id > 2' }).returns(0)

Developer.paginate :page => 1, :per_page => 1,
:include => :projects, :conditions => 'projects.id > 2'
end
end
end
end

0 comments on commit b3caa0a

Please sign in to comment.