Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination without finding total count #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/kaminari/mongoid/mongoid_criteria_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def total_count #:nodoc:
end
end

def without_count
extend ::Kaminari::PaginatableWithoutCount
end

private

def unpage
clone.tap do |crit|
crit.options.delete :limit
Expand All @@ -41,3 +46,25 @@ def unpage
end
end
end

module Kaminari
module PaginatableWithoutCount
# Method used to find last page, force setting to false and then use @records.present?
# in views to determine if link_to_next_page needs to be displayed.
def last_page?
false
end

# Method used to check if current page greater than total pages, force setting to false and then use @records.present?
# in views to determine if link_to_next_page needs to be displayed.
def out_of_range?
false
end

# Force to raise an exception if #total_count is called explicitly.
def total_count
raise "This scope is marked as a non-count paginable scope and can't be used in combination " \
"with `#paginate' or `#page_entries_info'. Use #link_to_next_page or #link_to_previous_page instead."
end
end
end
14 changes: 14 additions & 0 deletions test/models/mongoid/mongoid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ class MongoidCriteriaMethodsTest < ActiveSupport::TestCase
assert_equal 1, User.page.tap(&:total_count).where(salary: 1).total_count
end
end

sub_test_case '#without_count' do
setup do
5.times { User.with(database: 'without_count_db') { |u| u.create!(salary: 1) } }
end

test 'it should use default settings for last_page and out_of_range' do
users = User.max_scan(20).page(1).without_count

assert_instance_of Mongoid::Criteria, users
assert_equal false, users.last_page?
assert_equal false, users.out_of_range?
end
end
end

class MongoidExtensionTest < ActiveSupport::TestCase
Expand Down