diff --git a/docs/api/frontend.md b/docs/api/frontend.md index d863e2e22..93192108a 100644 --- a/docs/api/frontend.md +++ b/docs/api/frontend.md @@ -44,7 +44,7 @@ The `nav.*` templates produce the same output, and can be used as an easier (but See also [Using templates](../how-to.md#using-templates). -### pagy_info(pagy) +### pagy_info(pagy, item_name=nil) This method provides the info about the content of the current pagination. For example: @@ -60,6 +60,12 @@ or, if you use the `:i18n_key` variable a custom/collection-specific output: Displaying Products 476-500 of 1000 in total +You can also overwrite the `item_name` entirely by passing an already pluralized string directly to the helper: + +```erb +<%== pagy_info(@pagy, 'Widget'.pluralized(@pagy.count) %> +``` + _(see [Customizing the item name](../how-to.md#customizing-the-item-name))_ ### pagy_url_for(page, pagy) diff --git a/docs/how-to.md b/docs/how-to.md index 61ad8f144..0ea55a804 100644 --- a/docs/how-to.md +++ b/docs/how-to.md @@ -299,7 +299,12 @@ You have a few ways to do that: @pagy, @record = pagy(my_scope, i18n_key: 'activerecord.models.product' ) ``` -**Notice**: The variables passed to a Pagy object have the precedence over the variables returned by the `pagy_get_vars`. The fastest way is passing a literal string to the `pagy` method, the most convenient way is using `pagy_get_vars`. +3. you can override entirely the `:item_name` by passing an already pluralized string directly to the helper call: + ```erb + <%== pagy_info(@pagy, 'Widgets'.pluralize(@pagy.count) %> + ``` + +**Notice**: The variables passed to a Pagy object have the precedence over the variables returned by the `pagy_get_vars`. The fastest way to set the `i18n_key` is passing a literal string to the `pagy` method, the most convenient way is using `pagy_get_vars`, the most flexible way is passing a pluralized string to the helper. ## Paginate an Array diff --git a/lib/pagy/frontend.rb b/lib/pagy/frontend.rb index 3ecf3a3f1..9034e6041 100644 --- a/lib/pagy/frontend.rb +++ b/lib/pagy/frontend.rb @@ -50,11 +50,11 @@ def pagy_nav(pagy) end # Return examples: "Displaying items 41-60 of 324 in total" of "Displaying Products 41-60 of 324 in total" - def pagy_info(pagy) + def pagy_info(pagy, item_name=nil) path = if (count = pagy.count) == 0 ; 'pagy.info.no_items' else pagy.pages == 1 ? 'pagy.info.single_page' : 'pagy.info.multiple_pages' end - pagy_t(path, item_name: pagy_t(pagy.vars[:i18n_key], count: count), count: count, from: pagy.from, to: pagy.to) + pagy_t(path, item_name: item_name || pagy_t(pagy.vars[:i18n_key], count: count), count: count, from: pagy.from, to: pagy.to) end # Returns a performance optimized proc to generate the HTML links diff --git a/test/pagy/frontend_test.rb b/test/pagy/frontend_test.rb index 1fd752362..6a852e039 100644 --- a/test/pagy/frontend_test.rb +++ b/test/pagy/frontend_test.rb @@ -142,6 +142,13 @@ Pagy::I18n.load(locale: 'en') # reset for other tests end + it 'overrides the item_name' do + _(view.pagy_info(Pagy.new(count: 0), 'Widgets')).must_equal "No Widgets found" + _(view.pagy_info(Pagy.new(count: 1), 'Widget')).must_equal "Displaying 1 Widget" + _(view.pagy_info(Pagy.new(count: 13), 'Widgets')).must_equal "Displaying 13 Widgets" + _(view.pagy_info(Pagy.new(count: 100, page: 3), 'Widgets')).must_equal "Displaying Widgets 41-60 of 100 in total" + end + end describe '#pagy_url_for' do