From 750f4cab0dc29a1a9b29ac7f6c37f60167b44d47 Mon Sep 17 00:00:00 2001 From: Dinesh Majrekar Date: Tue, 15 Nov 2011 16:08:20 +0000 Subject: [PATCH] Display correct pagination figures when custom @per_page is set in the controller --- features/index/pagination.feature | 20 ++++++++++++++++++- .../views/components/paginated_collection.rb | 8 ++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/features/index/pagination.feature b/features/index/pagination.feature index d797e35420b..e23376dc296 100644 --- a/features/index/pagination.feature +++ b/features/index/pagination.feature @@ -1,17 +1,35 @@ Feature: Index Pagination Background: + Scenario: Viewing index when one page of resources exist Given an index configuration of: """ ActiveAdmin.register Post """ - Scenario: Viewing index when one page of resources exist Given 20 posts exist When I am on the index page for posts Then I should see "Displaying all 20 Posts" And I should not see pagination Scenario: Viewing index when multiple pages of resources exist + Given an index configuration of: + """ + ActiveAdmin.register Post + """ Given 31 posts exist When I am on the index page for posts Then I should see pagination with 2 pages + + Scenario: Viewing index with a custom per page set + Given an index configuration of: + """ + ActiveAdmin.register Post do + before_filter :only => :index do |controller| + @per_page = 10 + end + end + """ + Given 11 posts exist + When I am on the index page for posts + Then I should see pagination with 2 pages + And I should see "Displaying Posts 1 - 10 of 11 in total" diff --git a/lib/active_admin/views/components/paginated_collection.rb b/lib/active_admin/views/components/paginated_collection.rb index a5b7f1a6a14..d89b480bc61 100644 --- a/lib/active_admin/views/components/paginated_collection.rb +++ b/lib/active_admin/views/components/paginated_collection.rb @@ -42,7 +42,7 @@ def build(collection, options = {}) unless collection.respond_to?(:num_pages) raise(StandardError, "Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.") end - + div(page_entries_info(options).html_safe, :class => "pagination_information") @contents = div(:class => "paginated_collection_contents") build_pagination_with_formats @@ -70,7 +70,7 @@ def build_pagination_with_formats def build_pagination options = request.query_parameters.except(:commit, :format) options[:param_name] = @param_name if @param_name - + text_node paginate(collection, options.symbolize_keys) end @@ -107,9 +107,9 @@ def page_entries_info(options = {}) else; I18n.t('active_admin.pagination.one_page', :model => entries_name, :n => collection.size) end else - offset = collection.current_page * active_admin_application.default_per_page + offset = collection.current_page * collection.size total = collection.total_count - I18n.t('active_admin.pagination.multiple', :model => entries_name, :from => (offset - active_admin_application.default_per_page + 1), :to => offset > total ? total : offset, :total => total) + I18n.t('active_admin.pagination.multiple', :model => entries_name, :from => (offset - collection.size + 1), :to => offset > total ? total : offset, :total => total) end end