Skip to content

Commit

Permalink
pagy_info wrapped into span tag, and added pagy_id keyword arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Apr 26, 2021
1 parent 2faca63 commit ad350e1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -9,7 +9,7 @@
- `pagy*_nav_js(@pagy, pagy_id: 'my-id', link-extra: '...', steps: {...})`
- `pagy*_combo_nav_js(@pagy, pagy_id: 'my-id', link-extra: '...')`
- `pagy_items_selector_js(pagy, pagy_id: 'my-id', item_name: '...', i18n_key: '...')`
- `pagy_info(@pagy, item_name: '...', i18n_key: '...')`
- `pagy_info(@pagy, pagy_id: 'my-id', item_name: '...', i18n_key: '...')`
- `pagy_prev_link(@pagy, text: '...', link_extra: '...')`
- `pagy_next_link(@pagy, text: '...', link_extra: '...')`
- passing positional arguments (besides `@pagy`) to the public helpers is deprecated and it will be supported only until pagy 5.0 (with the exception is the internal `pagy_url_for` and `pagy_link_proc` methods that will keep using positional arguments)
Expand Down
7 changes: 4 additions & 3 deletions docs/api/frontend.md
Expand Up @@ -48,7 +48,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, item_name: ..., i18n_key: ...)
### pagy_info(pagy, pagy_id: ..., item_name: ..., i18n_key: ...)

This method provides the info about the content of the current pagination. For example:

Expand All @@ -58,9 +58,10 @@ This method provides the info about the content of the current pagination. For e

Will produce something like:

Displaying items <b>476-500</b> of <b>1000</b> in total
<span>Displaying items <b>476-500</b> of <b>1000</b> in total</span>

The method accepts also a couple of optional keyword arguments:
The method accepts also a few optional keyword arguments:
- `:pagy_id` which adds the `id` HTML attributedto the `span` tag wrapping the info
- `:item_name` an already pluralized string that will be used in place of the dedfault `item/items`
- `:i18n_key` the key to lookup in a dictionary

Expand Down
2 changes: 1 addition & 1 deletion docs/api/javascript.md
Expand Up @@ -84,7 +84,7 @@ import '../src/javascripts/pagy.js.erb'

- You may want to use `turbolinks:load` if your app uses turbolinks despite webpacker
- or you may want just `export { Pagy }` from the `pagy.js.erb` file and import and use it somewhere else.
- You may want to expose the `Pagy` namespace, if you need it available elsewhere (e.g. in js.erb templates):
- or you may want to expose the `Pagy` namespace, if you need it available elsewhere (e.g. in js.erb templates):
```js
global.Pagy = Pagy
```
Expand Down
6 changes: 5 additions & 1 deletion lib/pagy/frontend.rb
Expand Up @@ -61,15 +61,19 @@ def pagy_nav(pagy, pagy_id: nil, link_extra: '')
end

# Return examples: "Displaying items 41-60 of 324 in total" of "Displaying Products 41-60 of 324 in total"
def pagy_info(pagy, deprecated_item_name=nil, item_name: nil, i18n_key: nil)
def pagy_info(pagy, deprecated_item_name=nil, pagy_id: nil, item_name: nil, i18n_key: nil)
p_id = %( id="#{pagy_id}") if pagy_id
item_name = Pagy.deprecated_arg(:item_name, deprecated_item_name, :item_name, item_name) if deprecated_item_name
p_count = pagy.count
key = if p_count.zero? then 'pagy.info.no_items'
elsif pagy.pages == 1 then 'pagy.info.single_page'
else 'pagy.info.multiple_pages'
end

%(<span#{p_id}>#{
pagy_t key, item_name: item_name || pagy_t(i18n_key || pagy.vars[:i18n_key], count: p_count),
count: p_count, from: pagy.from, to: pagy.to
}</span>)
end

# Returns a performance optimized proc to generate the HTML links
Expand Down
24 changes: 12 additions & 12 deletions test/pagy/extras/i18n_test.rb
Expand Up @@ -24,23 +24,23 @@

describe '#pagy_info with I18n' do
it 'renders info' do
_(view.pagy_info(Pagy.new(count: 0))).must_equal "No items found"
_(view.pagy_info(Pagy.new(count: 1))).must_equal "Displaying <b>1</b> item"
_(view.pagy_info(Pagy.new(count: 13))).must_equal "Displaying <b>13</b> items"
_(view.pagy_info(Pagy.new(count: 100, page: 3))).must_equal "Displaying items <b>41-60</b> of <b>100</b> in total"
_(view.pagy_info(Pagy.new(count: 0))).must_equal "<span>No items found</span>"
_(view.pagy_info(Pagy.new(count: 1))).must_equal "<span>Displaying <b>1</b> item</span>"
_(view.pagy_info(Pagy.new(count: 13))).must_equal "<span>Displaying <b>13</b> items</span>"
_(view.pagy_info(Pagy.new(count: 100, page: 3))).must_equal "<span>Displaying items <b>41-60</b> of <b>100</b> in total</span>"
end
it 'renders with existing i18n key' do
::I18n.locale = 'en'
custom_dictionary = Pagy.root.parent.join('test', 'files', 'i18n.yml')
::I18n.load_path += [custom_dictionary]
_(view.pagy_info(Pagy.new(count: 0, i18n_key: 'activerecord.models.product'))).must_equal "No Products found"
_(view.pagy_info(Pagy.new(count: 1, i18n_key: 'activerecord.models.product'))).must_equal "Displaying <b>1</b> Product"
_(view.pagy_info(Pagy.new(count: 13, i18n_key: 'activerecord.models.product'))).must_equal "Displaying <b>13</b> Products"
_(view.pagy_info(Pagy.new(count: 100, i18n_key: 'activerecord.models.product', page: 3))).must_equal "Displaying Products <b>41-60</b> of <b>100</b> in total"
_(view.pagy_info(Pagy.new(count: 0), i18n_key: 'activerecord.models.product')).must_equal "No Products found"
_(view.pagy_info(Pagy.new(count: 1), i18n_key: 'activerecord.models.product')).must_equal "Displaying <b>1</b> Product"
_(view.pagy_info(Pagy.new(count: 13), i18n_key: 'activerecord.models.product')).must_equal "Displaying <b>13</b> Products"
_(view.pagy_info(Pagy.new(count: 100, page: 3), i18n_key: 'activerecord.models.product')).must_equal "Displaying Products <b>41-60</b> of <b>100</b> in total"
_(view.pagy_info(Pagy.new(count: 0, i18n_key: 'activerecord.models.product'))).must_equal "<span>No Products found</span>"
_(view.pagy_info(Pagy.new(count: 1, i18n_key: 'activerecord.models.product'))).must_equal "<span>Displaying <b>1</b> Product</span>"
_(view.pagy_info(Pagy.new(count: 13, i18n_key: 'activerecord.models.product'))).must_equal "<span>Displaying <b>13</b> Products</span>"
_(view.pagy_info(Pagy.new(count: 100, i18n_key: 'activerecord.models.product', page: 3))).must_equal "<span>Displaying Products <b>41-60</b> of <b>100</b> in total</span>"
_(view.pagy_info(Pagy.new(count: 0), i18n_key: 'activerecord.models.product')).must_equal "<span>No Products found</span>"
_(view.pagy_info(Pagy.new(count: 1), i18n_key: 'activerecord.models.product')).must_equal "<span>Displaying <b>1</b> Product</span>"
_(view.pagy_info(Pagy.new(count: 13), i18n_key: 'activerecord.models.product')).must_equal "<span>Displaying <b>13</b> Products</span>"
_(view.pagy_info(Pagy.new(count: 100, page: 3), i18n_key: 'activerecord.models.product')).must_equal "<span>Displaying Products <b>41-60</b> of <b>100</b> in total</span>"
end
end
end
34 changes: 17 additions & 17 deletions test/pagy/frontend_test.rb
Expand Up @@ -102,29 +102,29 @@

describe '#pagy_info' do
it 'renders without i18n key' do
_(view.pagy_info(Pagy.new(count: 0))).must_equal "No items found"
_(view.pagy_info(Pagy.new(count: 1))).must_equal "Displaying <b>1</b> item"
_(view.pagy_info(Pagy.new(count: 13))).must_equal "Displaying <b>13</b> items"
_(view.pagy_info(Pagy.new(count: 100, page: 3))).must_equal "Displaying items <b>41-60</b> of <b>100</b> in total"
_(view.pagy_info(Pagy.new(count: 0))).must_equal '<span>No items found</span>'
_(view.pagy_info(Pagy.new(count: 1))).must_equal '<span>Displaying <b>1</b> item</span>'
_(view.pagy_info(Pagy.new(count: 13))).must_equal '<span>Displaying <b>13</b> items</span>'
_(view.pagy_info(Pagy.new(count: 100, page: 3))).must_equal '<span>Displaying items <b>41-60</b> of <b>100</b> in total</span>'
end
it 'renders with existing i18n key' do
Pagy::I18n['en'][0]['pagy.info.product.one'] = ->(_){ 'Product'}
Pagy::I18n['en'][0]['pagy.info.product.other'] = ->(_){ 'Products'}
_(view.pagy_info(Pagy.new(count: 0, i18n_key: 'pagy.info.product'))).must_equal "No Products found"
_(view.pagy_info(Pagy.new(count: 1, i18n_key: 'pagy.info.product'))).must_equal "Displaying <b>1</b> Product"
_(view.pagy_info(Pagy.new(count: 13, i18n_key: 'pagy.info.product'))).must_equal "Displaying <b>13</b> Products"
_(view.pagy_info(Pagy.new(count: 100, i18n_key: 'pagy.info.product', page: 3))).must_equal "Displaying Products <b>41-60</b> of <b>100</b> in total"
_(view.pagy_info(Pagy.new(count: 0), i18n_key: 'pagy.info.product')).must_equal "No Products found"
_(view.pagy_info(Pagy.new(count: 1), i18n_key: 'pagy.info.product')).must_equal "Displaying <b>1</b> Product"
_(view.pagy_info(Pagy.new(count: 13), i18n_key: 'pagy.info.product')).must_equal "Displaying <b>13</b> Products"
_(view.pagy_info(Pagy.new(count: 100, page: 3), i18n_key: 'pagy.info.product')).must_equal "Displaying Products <b>41-60</b> of <b>100</b> in total"
_(view.pagy_info(Pagy.new(count: 0, i18n_key: 'pagy.info.product'))).must_equal '<span>No Products found</span>'
_(view.pagy_info(Pagy.new(count: 1, i18n_key: 'pagy.info.product'))).must_equal '<span>Displaying <b>1</b> Product</span>'
_(view.pagy_info(Pagy.new(count: 13, i18n_key: 'pagy.info.product'))).must_equal '<span>Displaying <b>13</b> Products</span>'
_(view.pagy_info(Pagy.new(count: 100, i18n_key: 'pagy.info.product', page: 3))).must_equal '<span>Displaying Products <b>41-60</b> of <b>100</b> in total</span>'
_(view.pagy_info(Pagy.new(count: 0), i18n_key: 'pagy.info.product')).must_equal '<span>No Products found</span>'
_(view.pagy_info(Pagy.new(count: 1), i18n_key: 'pagy.info.product')).must_equal '<span>Displaying <b>1</b> Product</span>'
_(view.pagy_info(Pagy.new(count: 13), i18n_key: 'pagy.info.product')).must_equal '<span>Displaying <b>13</b> Products</span>'
_(view.pagy_info(Pagy.new(count: 100, page: 3), i18n_key: 'pagy.info.product')).must_equal '<span>Displaying Products <b>41-60</b> of <b>100</b> in total</span>'
Pagy::I18n.load(locale: 'en') # reset for other tests
end
it 'overrides the item_name' do
_(view.pagy_info(Pagy.new(count: 0), item_name: 'Widgets')).must_equal "No Widgets found"
_(view.pagy_info(Pagy.new(count: 1), item_name: 'Widget')).must_equal "Displaying <b>1</b> Widget"
_(view.pagy_info(Pagy.new(count: 13), item_name: 'Widgets')).must_equal "Displaying <b>13</b> Widgets"
_(view.pagy_info(Pagy.new(count: 100, page: 3), item_name: 'Widgets')).must_equal "Displaying Widgets <b>41-60</b> of <b>100</b> in total"
it 'overrides the item_name and set pagy_id' do
_(view.pagy_info(Pagy.new(count: 0), pagy_id: 'pagy-info', item_name: 'Widgets')).must_equal '<span id="pagy-info">No Widgets found</span>'
_(view.pagy_info(Pagy.new(count: 1), pagy_id: 'pagy-info', item_name: 'Widget')).must_equal '<span id="pagy-info">Displaying <b>1</b> Widget</span>'
_(view.pagy_info(Pagy.new(count: 13), pagy_id: 'pagy-info', item_name: 'Widgets')).must_equal '<span id="pagy-info">Displaying <b>13</b> Widgets</span>'
_(view.pagy_info(Pagy.new(count: 100, page: 3), pagy_id: 'pagy-info', item_name: 'Widgets')).must_equal '<span id="pagy-info">Displaying Widgets <b>41-60</b> of <b>100</b> in total</span>'
end
end

Expand Down

0 comments on commit ad350e1

Please sign in to comment.