Skip to content

Commit

Permalink
item_name can be overridden by passing an already pluralized string t…
Browse files Browse the repository at this point in the history
…o pagy_info
  • Loading branch information
ddnexus committed Jun 12, 2020
1 parent 28913d6 commit 0a70110
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
8 changes: 7 additions & 1 deletion docs/api/frontend.md
Expand Up @@ -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:

Expand All @@ -60,6 +60,12 @@ or, if you use the `:i18n_key` variable a custom/collection-specific output:

Displaying Products <b>476-500</b> of <b>1000</b> 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)
Expand Down
7 changes: 6 additions & 1 deletion docs/how-to.md
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/frontend.rb
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/pagy/frontend_test.rb
Expand Up @@ -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 <b>1</b> Widget"
_(view.pagy_info(Pagy.new(count: 13), 'Widgets')).must_equal "Displaying <b>13</b> Widgets"
_(view.pagy_info(Pagy.new(count: 100, page: 3), 'Widgets')).must_equal "Displaying Widgets <b>41-60</b> of <b>100</b> in total"
end

end

describe '#pagy_url_for' do
Expand Down

0 comments on commit 0a70110

Please sign in to comment.