1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

Add LIMIT to total_count query when max_pages is present

closes #981
This commit is contained in:
Yuki Nishijima 2019-04-11 14:33:15 -04:00
parent b8bbfb1ce6
commit 1ba8e5f8fd
3 changed files with 35 additions and 7 deletions

View file

@ -29,6 +29,9 @@ module Kaminari
c = except(:offset, :limit, :order)
# Remove includes only if they are irrelevant
c = c.except(:includes) unless references_eager_loaded_tables?
c = c.limit(max_pages * limit_value) if max_pages && max_pages.respond_to?(:*)
# Handle grouping with a subquery
@total_count = if c.group_values.any?
c.model.from(c.except(:select).select("1")).count

View file

@ -274,7 +274,7 @@ if defined?(::Rails::Railtie) && defined?(::ActionView)
User.max_pages 4
users = User.page(4).per(10)
assert_equal 'Displaying users <b>31&nbsp;-&nbsp;40</b> of <b>50</b> in total', view.page_entries_info(users)
assert_equal 'Displaying users <b>31&nbsp;-&nbsp;40</b> of <b>40</b> in total', view.page_entries_info(users)
ensure
User.max_pages nil
end
@ -344,9 +344,8 @@ if defined?(::Rails::Railtie) && defined?(::ActionView)
end
test 'the last page with default entry name' do
User.max_pages 4
users = User.page(4).per(10)
assert_equal 'Displaying Benutzer <b>31&nbsp;-&nbsp;40</b> of <b>50</b> in total', view.page_entries_info(users, entry_name: 'Benutzer')
users = User.page(5).per(10)
assert_equal 'Displaying Benutzer <b>41&nbsp;-&nbsp;50</b> of <b>50</b> in total', view.page_entries_info(users, entry_name: 'Benutzer')
end
end
end
@ -393,9 +392,8 @@ if defined?(::Rails::Railtie) && defined?(::ActionView)
end
test 'the last page with default entry name' do
User.max_pages 4
users = User.page(4).per(10)
assert_equal 'Displaying utilisateurs <b>31&nbsp;-&nbsp;40</b> of <b>50</b> in total', view.page_entries_info(users, entry_name: 'utilisateur')
users = User.page(5).per(10)
assert_equal 'Displaying utilisateurs <b>41&nbsp;-&nbsp;50</b> of <b>50</b> in total', view.page_entries_info(users, entry_name: 'utilisateur')
end
end
end

View file

@ -99,6 +99,33 @@ if defined? ActiveRecord
test 'calculating total_count with GROUP BY ... HAVING clause' do
assert_equal 2, Authorship.group(:user_id).having("COUNT(book_id) >= 3").page(1).total_count
end
test 'total_count with max_pages does not add LIMIT' do
begin
subscriber = ActiveSupport::Notifications.subscribe 'sql.active_record' do |_, __, ___, ____, payload|
assert_not_match /LIMIT/, payload[:sql]
end
assert_equal 7, User.page.total_count
ensure
ActiveSupport::Notifications.unsubscribe subscriber
end
end
test 'total_count with max_pages adds "LIMIT (max_pages * per_page)" to the count query' do
begin
subscriber = ActiveSupport::Notifications.subscribe 'sql.active_record' do |_, __, ___, ____, payload|
assert_match /LIMIT/, payload[:sql]
end
User.max_pages 10
assert_equal 7, User.page.total_count
ensure
User.max_pages nil
ActiveSupport::Notifications.unsubscribe subscriber
end
end
end
end
end