Skip to content

Commit

Permalink
Rename var :i18n_key > :item_i18n_key
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 19, 2024
1 parent 7120bed commit 4e90666
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 69 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ None
### Breaking changes

- Dropped old rubies support: Pagy follows the [ruby end-of-life](https://endoflife.date/ruby) supported rubies now.
- Renamed `:i18n_key` > `:item_i18n_key`

### Default changes possibly breaking test/views

Expand Down
6 changes: 1 addition & 5 deletions docs/api/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ If you need to use multiple different types of collections in the same app or ac

Sub-method called only by the `pagy` method, it returns the hash of variables used to initialize the Pagy object.

Override it if you need to add or change some variable. For example you may want to add the `:i18n_key` in order to customize
output _(see [How to customize the item name](/docs/how-to.md#customize-the-item-name))_, or even cache the `count`.

Override it if you need to add or change some variable. For example you may want to add the `:item_i18n_key` in order to customize the output _(see [How to customize the item name](/docs/how-to.md#customize-the-item-name))_, or even cache the `count`.

!!!warning Don't remove `:count` and `:page`
`:count` and `:page` are the only 2 required Pagy core variables, so be careful not to remove them from the returned hash.
Expand Down Expand Up @@ -118,5 +116,3 @@ You can easily write a `pagy` method for _any possible_ environment: please read
!!!success PRs Welcome
If you write some useful backend customizations, please [let us know](https://github.com/ddnexus/pagy/discussions/categories/feature-requests) if you can submit a PR for a specific extra or if you need any help to do it.
!!!


9 changes: 4 additions & 5 deletions docs/api/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ The `nav.*` templates produce the same output, and can be used as an easier (but

See also [Using templates](/docs/how-to.md#use-templates).

==- `pagy_info(pagy, pagy_id: ..., item_name: ..., i18n_key: ...)`
==- `pagy_info(pagy, pagy_id: ..., item_name: ..., item_i18n_key: ...)`

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

Expand All @@ -76,14 +76,14 @@ The method accepts also a few optional keyword arguments:

- `:pagy_id` which adds the `id` HTML attribute to the `span` tag wrapping the info
- `:item_name` an already pluralized string that will be used in place of the default `item/items`
- `:i18n_key` the key to lookup in a dictionary
- `:item_i18n_key` the key to lookup in a dictionary

Notice the `:i18n_key` can be passed also to the constructor or be a less useful global variable (i.e. `Pagy::DEFAULT[:i18n_key]`
Notice the `:item_i18n_key` can be passed also to the constructor or be a global variable (i.e. `Pagy::DEFAULT[:item_i18n_key]`

||| View
```erb
<%== pagy_info(@pagy, item_name: 'Product'.pluralize(@pagy.count)) %>
<%== pagy_info(@pagy, i18n_key: 'activerecord.model.product' %>
<%== pagy_info(@pagy, item_i18n_key: 'activerecord.model.product' %>
```
|||

Expand Down Expand Up @@ -203,4 +203,3 @@ Specifically do not add a `class` attribute that will end up in the `pagy_bootst
This method is similar to the `I18n.t` and its equivalent rails `t` helper. It is called internally (from helpers and templates) in order to get the interpolated strings out of a YAML dictionary file. _(see the [Pagy::I18n](i18n.md) doc for details)_

===

2 changes: 1 addition & 1 deletion docs/api/pagy.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ They are all integers:
| `:params` | It can be a `Hash` of params to add to the URL, or a `Proc` that can edit/add/delete the request params _(see [How to customize the params](/docs/how-to.md#customize-the-params))_ | `{}` |
| `:fragment` | The arbitrary fragment string (including the "#") to add to the url. _(see [How to customize the params](/docs/how-to.md#customize-the-params))_ | `""` |
| `:link_extra` | The extra attributes string (formatted as a valid HTML attribute/value pairs) added to the page links _(see [How to customize the link attributes](/docs/how-to.md#customize-the-link-attributes))_ | `""` |
| `:i18n_key` | The i18n key to lookup the `item_name` that gets interpolated in a few helper outputs (see [How to customize the item name](/docs/how-to.md#customize-the-item-name)) | `"pagy.item_name"` |
| `:item_i18n_key` | The i18n key to lookup the `item_name` that gets interpolated in a few helper outputs (see [How to customize the item name](/docs/how-to.md#customize-the-item-name)) | `"pagy.item_name"` |
| `:cycle` | Enable cycling/circular/infinite pagination: `true` sets `next` to `1` when the current page is the last page | `false` |
| `:request_path` | Allows overriding the request path for pagination links. If left blank, helpers will use `request.path`. NB: Do not pass in a full URL, but the path: For example, given `https://ddnexus.github.io/pagy/docs/api/pagy/` the path to be passed in is: `pagy/docs/api/pagy/`. (See: [Customize the request path](/docs/how-to.md#customize-the-request-path) ) | `request.path` |
| `jsonapi` | Enable `jsonapi` compliance of the pagy query params | `false` |
Expand Down
6 changes: 3 additions & 3 deletions docs/extras/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ The method accepts also a few optional keyword arguments:

- `:pagy_id` which adds the `id` HTML attribute to the `nav` tag
- `:item_name` an already pluralized string that will be used in place of the default `item/items`
- `:i18n_key` the key to lookup in a dictionary
- `:item_i18n_key` the key to lookup in a dictionary
- `:link_extra` which add a verbatim string to the `a` tag (e.g. `'data-remote="true"'`)


!!!info Info
The `:i18n_key` can be passed also to the constructor or be a less useful global variable (i.e. `Pagy::DEFAULT[:i18n_key]`
The `:item_i18n_key` can be passed also to the constructor or be a less useful global variable (i.e. `Pagy::DEFAULT[:item_i18n_key]`

||| some_view.html.erb

```erb
<%== pagy_items_selector_js(@pagy, item_name: 'Product'.pluralize(@pagy.count)) %>
<%== pagy_items_selector_js(@pagy, i18n_key: 'activerecord.model.product' %>
<%== pagy_items_selector_js(@pagy, item_i18n_key: 'activerecord.model.product' %>
```
|||
!!!
Expand Down
18 changes: 9 additions & 9 deletions docs/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ The `pagy_info` and the `pagy_items_selector_js` helpers use the "item"/"items"
by editing the values of the `"pagy.item_name"` i18n key in
the [dictionary files](https://github.com/ddnexus/pagy/blob/master/lib/locales) that your app is using.

Besides you can also (dynamically) set the `:i18n_key` variable to let Pagy know where to lookup the item name in some dictionary
Besides you can also (dynamically) set the `:item_i18n_key` variable to let Pagy know where to lookup the item name in some dictionary
file (instead of looking it up in the default `"pagy.item_name"` key).

You have a few ways to do that:

1. you can override the `pagy_get_vars` method in your controller, adding the dynamically set `:i18n_key`. For example with
1. you can override the `pagy_get_vars` method in your controller, adding the dynamically set `:item_i18n_key`. For example with
ActiveRecord (mostly useful with the [i18n extra](extras/i18n.md) or if you copy over the AR keys into the pagy dictionary):

||| controller
Expand All @@ -371,23 +371,23 @@ You have a few ways to do that:
def pagy_get_vars(collection, vars)
{ count: ...,
page: ...,
i18n_key: "activerecord.models.#{collection.model_name.i18n_key}" }.merge!(vars)
item_i18n_key: "activerecord.models.#{collection.model_name.item_i18n_key}" }.merge!(vars)
end
```

|||

2. you can set the `:i18n_key` variable, either globally using the `Pagy::DEFAULT` hash or per instance with the `Pagy.new` method
2. you can set the `:item_i18n_key` variable, either globally using the `Pagy::DEFAULT` hash or per instance with the `Pagy.new` method
or with the `pagy` controller method:

||| initializer (pagy.rb)

```ruby
# all the Pagy instances will have the default
Pagy::DEFAULT[:i18n_key] = 'activerecord.models.product'
Pagy::DEFAULT[:item_i18n_key] = 'activerecord.models.product'
# or single Pagy instance
@pagy, @record = pagy(my_scope, i18n_key: 'activerecord.models.product')
@pagy, @record = pagy(my_scope, item_i18n_key: 'activerecord.models.product')
```

|||
Expand All @@ -397,8 +397,8 @@ or passing it as an optional keyword argument to the helper:
||| View

```erb
<%== pagy_info(@pagy, i18n_key: 'activerecord.models.product') %>
<%== pagy_items_selector_js(@pagy, i18n_key: 'activerecord.models.product') %>
<%== pagy_info(@pagy, item_i18n_key: 'activerecord.models.product') %>
<%== pagy_items_selector_js(@pagy, item_i18n_key: 'activerecord.models.product') %>
```

|||
Expand All @@ -416,7 +416,7 @@ or passing it as an optional keyword argument to the helper:

!!!warning Parameters have precedence
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
set the `item_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.
!!!

Expand Down
4 changes: 2 additions & 2 deletions lib/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# Pagy::DEFAULT[:params] = {} # default
# Pagy::DEFAULT[:fragment] = '#fragment' # example
# Pagy::DEFAULT[:link_extra] = 'data-remote="true"' # example
# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default
# Pagy::DEFAULT[:item_i18n_key] = 'pagy.item_name' # default
# Pagy::DEFAULT[:cycle] = true # example
# Pagy::DEFAULT[:request_path] = "/foo" # example

Expand Down Expand Up @@ -249,7 +249,7 @@
# require 'pagy/extras/i18n'

# Default i18n key
# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default
# Pagy::DEFAULT[:item_i18n_key] = 'pagy.item_name' # default


# When you are done setting your own default freeze it, so it will not get changed accidentally
Expand Down
22 changes: 11 additions & 11 deletions lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ def self.root
end

# Default core vars: constant for easy access, but mutable for customizable defaults
DEFAULT = { page: 1, # rubocop:disable Style/MutableConstant
items: 20,
outset: 0,
size: 7,
page_param: :page,
params: {},
fragment: '',
link_extra: '',
i18n_key: 'pagy.item_name',
cycle: false,
request_path: '' }
DEFAULT = { page: 1, # rubocop:disable Style/MutableConstant
items: 20,
outset: 0,
size: 7,
page_param: :page,
params: {},
fragment: '',
link_extra: '',
item_i18n_key: 'pagy.item_name',
cycle: false,
request_path: '' }

attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :in, :from, :to, :prev, :next, :params, :request_path

Expand Down
9 changes: 5 additions & 4 deletions lib/pagy/extras/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Frontend
ITEMS_PLACEHOLDER = '__pagy_items__'

# Return the items selector HTML. For example "Show [20] items per page"
def pagy_items_selector_js(pagy, pagy_id: nil, item_name: nil, i18n_key: nil, link_extra: '')
def pagy_items_selector_js(pagy, pagy_id: nil, item_name: nil, item_i18n_key: nil, link_extra: '')
return '' unless pagy.vars[:items_extra]

p_id = %( id="#{pagy_id}") if pagy_id
Expand All @@ -48,9 +48,10 @@ def pagy_items_selector_js(pagy, pagy_id: nil, item_name: nil, i18n_key: nil, li
html = +%(<span#{p_id} class="pagy-items-selector-js" #{pagy_data(pagy, :selector, pagy.from, link)}>)
input = %(<input type="number" min="1" max="#{p_vars[:max_items]}" value="#{
p_items}" style="padding: 0; text-align: center; width: #{p_items.to_s.length + 1}rem;">)
html << pagy_t('pagy.items_selector_js', item_name: item_name || pagy_t(i18n_key || p_vars[:i18n_key], count: p_items),
items_input: input,
count: p_items)
html << pagy_t('pagy.items_selector_js',
item_name: item_name || pagy_t(item_i18n_key || p_vars[:item_i18n_key], count: p_items),
items_input: input,
count: p_items)
html << %(</span>)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def pagy_nav(pagy, pagy_id: nil, link_extra: '', **vars)
end

# Return examples: "Displaying items 41-60 of 324 in total" or "Displaying Products 41-60 of 324 in total"
def pagy_info(pagy, pagy_id: nil, item_name: nil, i18n_key: nil)
def pagy_info(pagy, pagy_id: nil, item_name: nil, item_i18n_key: nil)
p_id = %( id="#{pagy_id}") if pagy_id
p_count = pagy.count
key = if p_count.zero? then 'pagy.info.no_items'
Expand All @@ -53,7 +53,7 @@ def pagy_info(pagy, pagy_id: nil, item_name: nil, i18n_key: nil)
end

%(<span#{p_id} class="pagy-info">#{
pagy_t key, item_name: item_name || pagy_t(i18n_key || pagy.vars[:i18n_key], count: p_count),
pagy_t key, item_name: item_name || pagy_t(item_i18n_key || pagy.vars[:item_i18n_key], count: p_count),
count: p_count, from: pagy.from, to: pagy.to
}</span>)
end
Expand Down
16 changes: 8 additions & 8 deletions test/pagy/extras/i18n_calendar_extra_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
I18n.locale = :en
custom_dictionary = Pagy.root.parent.join('test', 'files', 'i18n.yml')
I18n.load_path += [custom_dictionary]
_(app.pagy_info(Pagy.new(count: 0, i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 1, i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 13, i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 100, i18n_key: 'activerecord.models.product', page: 3))).must_rematch
_(app.pagy_info(Pagy.new(count: 0), i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 1), i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 13), i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 100, page: 3), i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 0, item_i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 1, item_i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 13, item_i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 100, item_i18n_key: 'activerecord.models.product', page: 3))).must_rematch
_(app.pagy_info(Pagy.new(count: 0), item_i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 1), item_i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 13), item_i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 100, page: 3), item_i18n_key: 'activerecord.models.product')).must_rematch
end
end

Expand Down
16 changes: 8 additions & 8 deletions test/pagy/extras/i18n_extra_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
I18n.locale = 'en'
custom_dictionary = Pagy.root.parent.join('test', 'files', 'i18n.yml')
I18n.load_path += [custom_dictionary]
_(app.pagy_info(Pagy.new(count: 0, i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 1, i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 13, i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 100, i18n_key: 'activerecord.models.product', page: 3))).must_rematch
_(app.pagy_info(Pagy.new(count: 0), i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 1), i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 13), i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 100, page: 3), i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 0, item_i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 1, item_i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 13, item_i18n_key: 'activerecord.models.product'))).must_rematch
_(app.pagy_info(Pagy.new(count: 100, item_i18n_key: 'activerecord.models.product', page: 3))).must_rematch
_(app.pagy_info(Pagy.new(count: 0), item_i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 1), item_i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 13), item_i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_info(Pagy.new(count: 100, page: 3), item_i18n_key: 'activerecord.models.product')).must_rematch
end
end
end
2 changes: 1 addition & 1 deletion test/pagy/extras/items_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_items_vars_params(items, vars, params)
_(app.pagy_items_selector_js(pagy)).must_rematch
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id', item_name: 'products')).must_rematch
Pagy::I18n::DATA['en'][0]['elasticsearch.product.other'] = 'products'
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id', i18n_key: 'elasticsearch.product')).must_rematch
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id', item_i18n_key: 'elasticsearch.product')).must_rematch
pagy = Pagy.new count: 1000, page: 3, items_extra: false
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id')).must_equal ''
end
Expand Down
2 changes: 1 addition & 1 deletion test/pagy/extras/items_trim_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id')).must_rematch
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id', item_name: 'products')).must_rematch
Pagy::I18n::DATA['en'][0]['activerecord.models.product.other'] = 'products'
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id', i18n_key: 'activerecord.models.product')).must_rematch
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id', item_i18n_key: 'activerecord.models.product')).must_rematch
pagy = Pagy.new count: 1000, page: 3, items_extra: false
_(app.pagy_items_selector_js(pagy, pagy_id: 'test-id')).must_equal ''
end
Expand Down
2 changes: 1 addition & 1 deletion test/pagy/extras/metadata_test.rb.rematch
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
:params: {}
:fragment: ''
:link_extra: ''
:i18n_key: pagy.item_name
:item_i18n_key: pagy.item_name
:cycle: false
:request_path: ''
:steps: false
Expand Down
Loading

0 comments on commit 4e90666

Please sign in to comment.