Skip to content

Commit

Permalink
Update pagination wording when pagination_total is false
Browse files Browse the repository at this point in the history
  • Loading branch information
drcapulet committed Apr 13, 2023
1 parent d0d4fa2 commit 9f7a8ba
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/active_admin/views/components/paginated_collection.rb
Expand Up @@ -50,6 +50,15 @@ def build(collection, options = {})
raise(StandardError, "Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.")
end

unless @display_total
# The #paginate method in kaminari will query the resource with a
# count(*) to determine how many pages there should be unless
# you pass in the :total_pages option. We issue a query to determine
# if there is another page or not, but the limit/offset make this
# query fast.
@has_next_page = collection.offset(collection.current_page * collection.limit_value).limit(1).count > 0
end

@contents = div(class: "paginated_collection_contents")
build_pagination_with_formats(options)
@built = true
Expand Down Expand Up @@ -98,13 +107,7 @@ def build_pagination
options[:param_name] = @param_name if @param_name

if !@display_total
# The #paginate method in kaminari will query the resource with a
# count(*) to determine how many pages there should be unless
# you pass in the :total_pages option. We issue a query to determine
# if there is another page or not, but the limit/offset make this
# query fast.
offset = collection.offset(collection.current_page * collection.limit_value).limit(1).count
options[:total_pages] = collection.current_page + offset
options[:total_pages] = collection.current_page + (@has_next_page ? 1 : 0)
options[:right] = 0
end

Expand All @@ -129,13 +132,18 @@ def page_entries_info(options = {})
entries_name = I18n.translate key, count: collection.size, default: entry_name.pluralize
end

case
when collection_size == 0
return I18n.t("active_admin.pagination.empty", model: entries_name)
when collection_size == 1
return I18n.t("active_admin.pagination.one", model: entry_name)
when collection.current_page == 1 && ((collection_size < collection.limit_value) || (!@display_total && !@has_next_page))
return I18n.t("active_admin.pagination.one_page", model: entries_name, n: collection_size)
end

if @display_total
if collection.total_pages < 2
case collection_size
when 0; I18n.t("active_admin.pagination.empty", model: entries_name)
when 1; I18n.t("active_admin.pagination.one", model: entry_name)
else; I18n.t("active_admin.pagination.one_page", model: entries_name, n: collection.total_count)
end
I18n.t("active_admin.pagination.one_page", model: entries_name, n: collection.total_count)
else
offset = (collection.current_page - 1) * collection.limit_value
total = collection.total_count
Expand Down
80 changes: 80 additions & 0 deletions spec/unit/views/components/paginated_collection_spec.rb
Expand Up @@ -168,6 +168,14 @@ def paginated_collection(*args)
it "should display 'No entries found'" do
expect(pagination.find_by_class("pagination_information").first.content).to eq "No entries found"
end

context "with :pagination_total set to false" do
let(:pagination) { paginated_collection(collection, pagination_total: false) }

it "should display 'No entries found'" do
expect(pagination.find_by_class("pagination_information").first.content).to eq "No entries found"
end
end
end

context "when collection comes from find with GROUP BY" do
Expand Down Expand Up @@ -216,6 +224,45 @@ def paginated_collection(*args)
info = pagination.find_by_class("pagination_information").first.content.gsub("&nbsp;", " ")
expect(info).to eq "Displaying posts <b>1 - 30</b>"
end

describe "one item" do
let(:collection) do
Kaminari.paginate_array([Post.new]).page(1).per(30)
end

it "should not show the total item counts" do
expect(collection).not_to receive(:total_pages)
pagination = paginated_collection(collection, pagination_total: false)
info = pagination.find_by_class("pagination_information").first.content.gsub("&nbsp;", " ")
expect(info).to eq "Displaying <b>1</b> post"
end
end

describe "fewer than page size items" do
let(:collection) do
Kaminari.paginate_array([Post.new] * 29).page(1).per(30)
end

it "should not show the total item counts" do
expect(collection).not_to receive(:total_pages)
pagination = paginated_collection(collection, pagination_total: false)
info = pagination.find_by_class("pagination_information").first.content.gsub("&nbsp;", " ")
expect(info).to eq "Displaying <b>all 29</b> posts"
end
end

describe "page size items" do
let(:collection) do
Kaminari.paginate_array([Post.new] * 30).page(1).per(30)
end

it "should not show the total item counts" do
expect(collection).not_to receive(:total_pages)
pagination = paginated_collection(collection, pagination_total: false)
info = pagination.find_by_class("pagination_information").first.content.gsub("&nbsp;", " ")
expect(info).to eq "Displaying <b>all 30</b> posts"
end
end
end

describe "set to true" do
Expand All @@ -225,6 +272,39 @@ def paginated_collection(*args)
info = pagination.find_by_class("pagination_information").first.content.gsub("&nbsp;", " ")
expect(info).to eq "Displaying posts <b>1 - 30</b> of <b>256</b> in total"
end

describe "one item" do
let(:collection) do
Kaminari.paginate_array([Post.new]).page(1).per(30)
end

it "should not show the total item counts" do
info = pagination.find_by_class("pagination_information").first.content.gsub("&nbsp;", " ")
expect(info).to eq "Displaying <b>1</b> post"
end
end

describe "fewer than page size items" do
let(:collection) do
Kaminari.paginate_array([Post.new] * 29).page(1).per(30)
end

it "should not show the total item counts" do
info = pagination.find_by_class("pagination_information").first.content.gsub("&nbsp;", " ")
expect(info).to eq "Displaying <b>all 29</b> posts"
end
end

describe "page size items" do
let(:collection) do
Kaminari.paginate_array([Post.new] * 30).page(1).per(30)
end

it "should not show the total item counts" do
info = pagination.find_by_class("pagination_information").first.content.gsub("&nbsp;", " ")
expect(info).to eq "Displaying <b>all 30</b> posts"
end
end
end
end

Expand Down

0 comments on commit 9f7a8ba

Please sign in to comment.