From 15fc073f52ecd4881f83861876d0d7cde1ab9ca4 Mon Sep 17 00:00:00 2001 From: JoseLuis Torres Date: Tue, 9 Jul 2013 23:13:31 -0500 Subject: [PATCH] adding the pagination_count option for index page #2283 --- config/locales/bg.yml | 1 + config/locales/ca.yml | 1 + config/locales/cs.yml | 1 + config/locales/da.yml | 1 + config/locales/de-CH.yml | 1 + config/locales/de.yml | 1 + config/locales/en-GB.yml | 1 + config/locales/en.yml | 1 + config/locales/es.yml | 1 + config/locales/fr.yml | 1 + config/locales/he.yml | 1 + config/locales/hr.yml | 1 + config/locales/hu.yml | 1 + config/locales/it.yml | 1 + config/locales/ja.yml | 1 + config/locales/ko.yml | 1 + config/locales/lt.yml | 1 + config/locales/lv.yml | 1 + config/locales/nl.yml | 1 + config/locales/no-NB.yml | 1 + config/locales/pl.yml | 1 + config/locales/pt-BR.yml | 1 + config/locales/pt-PT.yml | 1 + config/locales/ro.yml | 1 + config/locales/ru.yml | 1 + config/locales/sv-SE.yml | 1 + config/locales/tr.yml | 1 + config/locales/uk.yml | 1 + config/locales/vi.yml | 1 + config/locales/zh-CN.yml | 1 + config/locales/zh-TW.yml | 1 + docs/3-index-pages.md | 12 ++++++ features/index/pagination.feature | 15 +++++++ .../views/components/paginated_collection.rb | 11 ++++- lib/active_admin/views/pages/index.rb | 4 +- spec/spec_helper.rb | 4 ++ .../components/paginated_collection_spec.rb | 42 +++++++++++++++++++ 37 files changed, 116 insertions(+), 3 deletions(-) diff --git a/config/locales/bg.yml b/config/locales/bg.yml index 7b3820d17c5..7f15dc4100b 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -43,6 +43,7 @@ bg: one: "Показване на 1 %{model}" one_page: "Показване на всички %{n} %{model}" multiple: "Показване %{model} %{from} - %{to} от общо %{total}" + multiple_without_total: "Показване %{model} %{from} - %{to}" entry: one: "запис" other: "записи" diff --git a/config/locales/ca.yml b/config/locales/ca.yml index 29f50aa265a..aed4e3f4835 100644 --- a/config/locales/ca.yml +++ b/config/locales/ca.yml @@ -42,6 +42,7 @@ ca: one: "S'està mostrant 1 %{model}" one_page: "S'estan mostrant tots %{n} %{model}" multiple: "S'estan mostrant %{model} %{from} - %{to} de %{total} en total" + multiple_without_total: "S'estan mostrant %{model} %{from} - %{to}" entry: one: "entrada" other: "entrades" diff --git a/config/locales/cs.yml b/config/locales/cs.yml index 244f6ead851..827d490133b 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -43,6 +43,7 @@ cs: one: "Zobrazena 1 položka" one_page: "Počet zobrazených položek %{n}" multiple: "%{from} - %{to} z %{total}" + multiple_without_total: "%{from} - %{to}" entry: one: "položka" few: "položky" diff --git a/config/locales/da.yml b/config/locales/da.yml index caa6e1b5b44..cf5570bf2bc 100644 --- a/config/locales/da.yml +++ b/config/locales/da.yml @@ -43,6 +43,7 @@ da: one: "Viser 1 %{model}" one_page: "Viser alle %{n} %{model}" multiple: "Viser %{model} %{from} - %{to} af %{total} i alt" + multiple_without_total: "Viser %{model} %{from} - %{to}" entry: one: "post" other: "poster" diff --git a/config/locales/de-CH.yml b/config/locales/de-CH.yml index 2330e9be3d9..5f9497fe14e 100644 --- a/config/locales/de-CH.yml +++ b/config/locales/de-CH.yml @@ -43,6 +43,7 @@ one: "Zeige 1 %{model}" one_page: "Zeige alle %{n} %{model}" multiple: "Zeige %{model} %{from} – %{to} von %{total}" + multiple_without_total: "Zeige %{model} %{from} – %{to}" entry: one: "Eintrag" other: "Einträge" diff --git a/config/locales/de.yml b/config/locales/de.yml index 69dea0dd3cd..8321712c679 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -43,6 +43,7 @@ de: one: "Zeige 1 %{model}" one_page: "Zeige alle %{n} %{model}" multiple: "Zeige %{model} %{from} – %{to} von %{total}" + multiple_without_total: "Zeige %{model} %{from} – %{to}" entry: one: "Eintrag" other: "Einträge" diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index 3e8f500c9ea..3a3911c34a7 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -43,6 +43,7 @@ one: "Displaying 1 %{model}" one_page: "Displaying all %{n} %{model}" multiple: "Displaying %{model} %{from} - %{to} of %{total} in total" + multiple_without_total: "Displaying %{model} %{from} - %{to}" entry: one: "entry" other: "entries" diff --git a/config/locales/en.yml b/config/locales/en.yml index 12373978ea9..2037eecc219 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -43,6 +43,7 @@ en: one: "Displaying 1 %{model}" one_page: "Displaying all %{n} %{model}" multiple: "Displaying %{model} %{from} - %{to} of %{total} in total" + multiple_without_total: "Displaying %{model} %{from} - %{to}" entry: one: "entry" other: "entries" diff --git a/config/locales/es.yml b/config/locales/es.yml index ca0d57801ac..dfddbb50449 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -43,6 +43,7 @@ es: one: "Mostrando 1 %{model}" one_page: "Mostrando un total de %{n} %{model}" multiple: "Mostrando %{model} %{from} - %{to} de un total de %{total}" + multiple_without_total: "Mostrando %{model} %{from} - %{to}" blank_slate: content: "No hay %{resource_name} aún." link: "Añadir" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index bf5f9d6c04a..5157f7b4be4 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -43,6 +43,7 @@ fr: one: "Affichage de 1 %{model}" one_page: "Affichage de tous les %{n} %{model}" multiple: "Affichage de %{model} %{from} - %{to} sur un total de %{total}" + multiple_without_total: "Affichage de %{model} %{from} - %{to}" entry: one: "entrée" other: "entrées" diff --git a/config/locales/he.yml b/config/locales/he.yml index 36c3dd5539a..baada339358 100644 --- a/config/locales/he.yml +++ b/config/locales/he.yml @@ -43,6 +43,7 @@ he: one: "מציג 1 %{model}" one_page: "הצגת כל %{n} %{model}" multiple: "מציג %{model} %{from} - %{to} מתוך %{total} בסך הכל" + multiple_without_total: "מציג %{model} %{from} - %{to}" entry: one: "רשומה בודדה" other: "רשומות" diff --git a/config/locales/hr.yml b/config/locales/hr.yml index 8cf4cfce257..d820677b79c 100644 --- a/config/locales/hr.yml +++ b/config/locales/hr.yml @@ -43,6 +43,7 @@ hr: one: "Prikazan 1 %{model}" one_page: "Prikazano svih %{n} %{model}" multiple: "Prikazani %{model} %{from} - %{to} od ukupno %{total}" + multiple_without_total: "Prikazani %{model} %{from} - %{to}" entry: one: "zapis" few: "zapisa" diff --git a/config/locales/hu.yml b/config/locales/hu.yml index f791ffb17d8..5a1e9f9df87 100644 --- a/config/locales/hu.yml +++ b/config/locales/hu.yml @@ -43,6 +43,7 @@ hu: one: "Egy %{model} megjelenítése" one_page: "Az összes (%{n} db) %{model} megjelenítése" multiple: "%{model} listájának megjelenítése, %{from} - %{to}/%{total} " + multiple_without_total: "%{model} listájának megjelenítése, %{from} - %{to} " entry: one: "elem" other: "elem" diff --git a/config/locales/it.yml b/config/locales/it.yml index 6a8e6eb13c1..aec7b4a685f 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -43,6 +43,7 @@ it: one: "Sto mostrando 1 %{model}" one_page: "Sto mostrando %{n} %{model}. Lista completa." multiple: "Sto mostrando %{model} %{from} - %{to} di %{total} in totale" + multiple_without_total: "Sto mostrando %{model} %{from} - %{to}" entry: one: "voce" other: "voci" diff --git a/config/locales/ja.yml b/config/locales/ja.yml index e5c7ec23545..e1925ab9847 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -43,6 +43,7 @@ ja: one: "1 件の %{model} を表示しています" one_page: "全 %{n} 件の %{model} を表示しています" multiple: "全 %{total} 件中 %{from} - %{to} 件の %{model} を表示しています" + multiple_without_total: "%{from} - %{to} 件の %{model} を表示しています" entry: レコード any: "任意" blank_slate: diff --git a/config/locales/ko.yml b/config/locales/ko.yml index 78e4f2481c4..8d8c83554a7 100644 --- a/config/locales/ko.yml +++ b/config/locales/ko.yml @@ -41,6 +41,7 @@ ko: one: "1 %{model} 표시중" one_page: "전체 %{n} %{model} 표시중" multiple: "전체 %{total}%{from} - %{to} %{model} 표시중" + multiple_without_total: "중 %{from} - %{to} %{model} 표시중" any: "어떤" blank_slate: content: "아직 %{resource_name} 이/가 없습니다." diff --git a/config/locales/lt.yml b/config/locales/lt.yml index 999982a785f..4087da8cec1 100644 --- a/config/locales/lt.yml +++ b/config/locales/lt.yml @@ -43,6 +43,7 @@ lt: one: 'Rodoma 1 %{model}' one_page: 'Rodoma visi %{n} %{model}' multiple: 'Rodomi %{model} %{iš} - %{to} %{total} iš viso' + multiple_without_total: 'Rodomi %{model} %{iš} - %{to} ' entry: one: 'įrašas' other: 'įrašai' diff --git a/config/locales/lv.yml b/config/locales/lv.yml index ca79327a4c0..b5bc2d8093e 100644 --- a/config/locales/lv.yml +++ b/config/locales/lv.yml @@ -43,6 +43,7 @@ lv: one: "1 ieraksts" one_page: "%{n} ieraksti" multiple: "%{from} - %{to} ieraksti no %{total} kopā" + multiple_without_total: "%{from} - %{to}" entry: one: "ieraksts" other: "ieraksti" diff --git a/config/locales/nl.yml b/config/locales/nl.yml index f1a68a35eb6..56ba0096834 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -43,6 +43,7 @@ nl: one: "Geeft 1 %{model} weer" one_page: "Geeft %{n} %{model} weer" multiple: "Geeft %{model} %{from} - %{to} van de %{total} weer" + multiple_without_total: "Geeft %{model} %{from} - %{to}" entry: one: "entry" other: "entries" diff --git a/config/locales/no-NB.yml b/config/locales/no-NB.yml index 70198adaa54..d79619c5687 100644 --- a/config/locales/no-NB.yml +++ b/config/locales/no-NB.yml @@ -41,6 +41,7 @@ one: "Viser 1 %{model}" one_page: "Viser alle %{n} %{model}" multiple: "Viser %{model} %{from} - %{to} av %{total} totalt" + multiple_without_total: "Viser %{model} %{from} - %{to}" any: "Noen" blank_slate: content: "Her er det ingen %{resource_name} enda." diff --git a/config/locales/pl.yml b/config/locales/pl.yml index 17a0c01496b..bb659f067be 100644 --- a/config/locales/pl.yml +++ b/config/locales/pl.yml @@ -43,6 +43,7 @@ pl: one: "Wyświetlanie 1 %{model}" one_page: "Wyświetlanie wszystkich %{n} %{model}" multiple: "Wyświetlanie %{model} %{from} - %{to} z %{total}" + multiple_without_total: "Wyświetlanie %{model} %{from} - %{to}" any: "Jakikolwiek" blank_slate: content: "Nie ma jeszcze zasobu %{resource_name}." diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index 6fe4519427d..6c45e055ef7 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -43,6 +43,7 @@ one: "Exibindo 1 %{model}" one_page: "Exibindo todos(as) os(as) %{n} %{model}" multiple: "Exibindo %{model} %{from} - %{to} de um total de %{total}" + multiple_without_total: "Exibindo %{model} %{from} - %{to}" entry: one: "registro" other: "registros" diff --git a/config/locales/pt-PT.yml b/config/locales/pt-PT.yml index fb0a1625066..b9d11b036d5 100644 --- a/config/locales/pt-PT.yml +++ b/config/locales/pt-PT.yml @@ -43,6 +43,7 @@ one: "Mostrando 1 %{model}" one_page: "Mostrando todos(as) os(as) %{n} %{model}" multiple: "Mostrando %{model} %{from} - %{to} de um total de %{total}" + multiple_without_total: "Mostrando %{model} %{from} - %{to}" entry: one: "registro" other: "registros" diff --git a/config/locales/ro.yml b/config/locales/ro.yml index 341c968ee75..8fe0f54b9a4 100644 --- a/config/locales/ro.yml +++ b/config/locales/ro.yml @@ -43,6 +43,7 @@ ro: one: "Afisare 1 %{model}" one_page: "Sunt afisate toate %{n} inregistrarile" multiple: "Sunt afisate %{from} - %{to} din %{total} inregistrari" + multiple_without_total: "Sunt afisate %{from} - %{to}" entry: one: "inregistrare" other: "inregistrari" diff --git a/config/locales/ru.yml b/config/locales/ru.yml index a13d5701be1..046a6206716 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -43,6 +43,7 @@ ru: one: "Результат: 1 %{model}" one_page: "Результат: %{n} %{model}" multiple: "Результат: %{model} %{from} - %{to} из %{total}" + multiple_without_total: "Результат: %{model} %{from} - %{to}" entry: one: "запись" other: "записей" diff --git a/config/locales/sv-SE.yml b/config/locales/sv-SE.yml index 47b4f5af1c4..5a7b6655d65 100644 --- a/config/locales/sv-SE.yml +++ b/config/locales/sv-SE.yml @@ -43,6 +43,7 @@ one: "Visar 1 utav %{model}" one_page: "Visar alla %{n} utav %{model}" multiple: "Visar %{model} %{from} - %{to} av %{total} totalt" + multiple_without_total: "Visar %{model} %{from} - %{to}" entry: one: "inlägg" other: "inlägg" diff --git a/config/locales/tr.yml b/config/locales/tr.yml index e05547eb7bc..8d5c91a9672 100644 --- a/config/locales/tr.yml +++ b/config/locales/tr.yml @@ -43,6 +43,7 @@ tr: one: "1 %{model} kaydı görüntüleniyor" one_page: "%{n} kayıt %{model} modelinde görüntüleniyor" multiple: "%{model} toplam %{total} kayıt bulundu. %{from} ile %{to} arası arası görüntüleniyor" + multiple_without_total: "%{model}. %{from} ile %{to} arası arası görüntüleniyor" entry: one: "Girdi" other: "Girdiler" diff --git a/config/locales/uk.yml b/config/locales/uk.yml index ead005a796c..d376975dc67 100644 --- a/config/locales/uk.yml +++ b/config/locales/uk.yml @@ -40,6 +40,7 @@ uk: one: "Результат: 1 %{model}" one_page: "Результат: %{n} %{model}" multiple: "Результат: %{model} %{from} - %{to} з %{total}" + multiple_without_total: "Результат: %{model} %{from} - %{to}" entry: one: "запис" other: "записів" diff --git a/config/locales/vi.yml b/config/locales/vi.yml index a8b05e5f7ec..0012c4d0265 100644 --- a/config/locales/vi.yml +++ b/config/locales/vi.yml @@ -43,6 +43,7 @@ vi: one: "Đang hiển thị 1 %{model}" one_page: "Đang hiển thị tất cả %{n} %{model}" multiple: "Đang hiển thị %{model} %{from} - %{to} of %{total} trong tất cả." + multiple_without_total: "Đang hiển thị %{model} %{from} - %{to}." entry: one: "entry" other: "entries" diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml index 6500249c696..08c11196a24 100644 --- a/config/locales/zh-CN.yml +++ b/config/locales/zh-CN.yml @@ -43,6 +43,7 @@ one: "显示 1 %{model}" one_page: "显示 所有 %{n} %{model}" multiple: "显示所有 %{total} %{model}中的%{from} - %{to} 条" + multiple_without_total: "%{model}中的%{from} - %{to} 条" entry: one: "条目" other: "条目" diff --git a/config/locales/zh-TW.yml b/config/locales/zh-TW.yml index cd353599ae4..4b256506ac3 100644 --- a/config/locales/zh-TW.yml +++ b/config/locales/zh-TW.yml @@ -43,6 +43,7 @@ one: "顯示 1 %{model}" one_page: "顯示 全部 %{n} %{model}" multiple: "總計 %{total} 顯示 %{model} 中%{from} - %{to} 筆" + multiple_without_total: "顯示 %{model} 中%{from} - %{to} 筆" entry: one: "筆" other: "筆" diff --git a/docs/3-index-pages.md b/docs/3-index-pages.md index 9872e51c801..c52799cdabc 100644 --- a/docs/3-index-pages.md +++ b/docs/3-index-pages.md @@ -143,6 +143,17 @@ You can also disable pagination: config.paginate = false end +If you have a very large database, you might want to disable SELECT COUNT(*) queries caused by the pagination info at the bottom of the page: + + ActiveAdmin.register Post do + + # disable pagination total + index :pagination_total => false do + #... + end + + end + ## Disable CSV, XML and JSON export You can remove links to download CSV, XML and JSON exports: @@ -177,3 +188,4 @@ If you want to customize download links for every resource throughout the applic config.download_links = [:csv, :xml, :json, :pdf] end + diff --git a/features/index/pagination.feature b/features/index/pagination.feature index 53807be569b..d2d0c07216e 100644 --- a/features/index/pagination.feature +++ b/features/index/pagination.feature @@ -42,3 +42,18 @@ Feature: Index Pagination Given 31 posts exist When I am on the index page for posts Then I should not see pagination + + Scenario: Viewing index with pagination_total set to false + Given an index configuration of: + """ + ActiveAdmin.register Post do + index :pagination_total => false do + end + end + """ + Given 100 posts exist + When I am on the index page for posts + Then I should see pagination with 2 pages + Then I should see "Displaying Posts 1 - 30" + And I should not see "Displaying Posts 1 - 30 of 100 in total" + diff --git a/lib/active_admin/views/components/paginated_collection.rb b/lib/active_admin/views/components/paginated_collection.rb index de6cd1d8d54..3b1711d94b3 100644 --- a/lib/active_admin/views/components/paginated_collection.rb +++ b/lib/active_admin/views/components/paginated_collection.rb @@ -39,6 +39,7 @@ def build(collection, options = {}) @collection = collection @param_name = options.delete(:param_name) @download_links = options.delete(:download_links) + @display_total = options.delete(:pagination_total) { true } 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.") @@ -106,8 +107,14 @@ def page_entries_info(options = {}) end else offset = (collection.current_page - 1) * collection.limit_value - total = collection.total_count - I18n.t('active_admin.pagination.multiple', :model => entries_name, :from => offset + 1, :to => offset + collection_size, :total => total) + if @display_total + total = collection.total_count + I18n.t 'active_admin.pagination.multiple', :model => entries_name, :total => total, + :from => offset + 1, :to => offset + collection_size + else + I18n.t 'active_admin.pagination.multiple_without_total', :model => entries_name, + :from => offset + 1, :to => offset + collection_size + end end end diff --git a/lib/active_admin/views/pages/index.rb b/lib/active_admin/views/pages/index.rb index f5f725e55dc..fe37c71f285 100644 --- a/lib/active_admin/views/pages/index.rb +++ b/lib/active_admin/views/pages/index.rb @@ -138,11 +138,13 @@ def render_index renderer_class = find_index_renderer_class(config[:as]) paginator = config[:paginator].nil? ? true : config[:paginator] download_links = config[:download_links].nil? ? active_admin_config.namespace.download_links : config[:download_links] + pagination_total = config[:pagination_total].nil? ? true : config[:pagination_total] paginated_collection(collection, :entry_name => active_admin_config.resource_label, :entries_name => active_admin_config.plural_resource_label(:count => collection_size), :download_links => download_links, - :paginator => paginator) do + :paginator => paginator, + :pagination_total => pagination_total) do div :class => 'index_content' do insert_tag(renderer_class, config, collection) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a021f0dea3a..74d3ccc44ca 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,6 +9,10 @@ def load_defaults! ActiveAdmin.register(Category) ActiveAdmin.register(User) ActiveAdmin.register(Post){ belongs_to :user, :optional => true } + ActiveAdmin.register(Store){ + index :pagination_total => false do + end + } reload_menus! end diff --git a/spec/unit/views/components/paginated_collection_spec.rb b/spec/unit/views/components/paginated_collection_spec.rb index 0148c4686a9..715c94af4eb 100644 --- a/spec/unit/views/components/paginated_collection_spec.rb +++ b/spec/unit/views/components/paginated_collection_spec.rb @@ -200,6 +200,48 @@ def paginated_collection(*args) end end + context "when having the param :pagination_total set to true " do + let(:view) do + view = mock_action_view + view.request.stub!(:query_parameters).and_return({:controller => 'admin/stores', :action => 'index', :page => '1'}) + view.controller.params = {:controller => 'admin/stores', :action => 'index'} + view + end + + let(:collection) do + stores = [Store.new] * 1000 + Kaminari.paginate_array(stores).page(1).per(30) + end + + let(:pagination) { paginated_collection(collection, :pagination_total => true) } + it "should show the total item counts" do + info = pagination.find_by_class('pagination_information').first.content.gsub(' ',' ') + info.should eq "Displaying Bookstores 1 - 30 of 1000 in total" + end + + end + + context "when having the param :pagination_total set to false " do + let(:view) do + view = mock_action_view + view.request.stub!(:query_parameters).and_return({:controller => 'admin/stores', :action => 'index', :page => '1'}) + view.controller.params = {:controller => 'admin/stores', :action => 'index'} + view + end + + let(:collection) do + stores = [Store.new] * 1000 + Kaminari.paginate_array(stores).page(1).per(30) + end + + let(:pagination) { paginated_collection(collection, :pagination_total => false) } + + it "should not show the total item counts" do + info = pagination.find_by_class('pagination_information').first.content.gsub(' ',' ') + info.should eq "Displaying Bookstores 1 - 30" + end + + end end end