Skip to content

Commit

Permalink
added how-to for custom count for custom scopes (#130); other minor a…
Browse files Browse the repository at this point in the history
…djustments
  • Loading branch information
ddnexus committed Feb 26, 2019
1 parent 4c7b50a commit 4156121
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
16 changes: 7 additions & 9 deletions docs/api/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ Pagy is i18n ready. That means that all its strings are stored in the dictionary

The pagy internal i18n implementation is ~18x faster and uses ~10x less memory than the standard `i18n` gem.

Since Pagy version 2.0, you can use it for both single-language and multi-language apps, with or without the `i18n` gem.

Notice: if your app is using i18n, it will work independently from it.
Since Pagy version 2.0, you can use it for both single-language and multi-language apps. If (the rest of) your app is using i18n, it will work independently from the pagy i18n.

The pagy internal i18n is implemented around the `Pagy::I18n` constant hash which contains the locales data needed to pagy and your app. You may need to configure it in the [pagy.rb](https://github.com/ddnexus/pagy/blob/master/lib/config/pagy.rb) initializer.

Expand All @@ -196,15 +194,15 @@ Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')
# load the "de", "en" and "es" built-in locales:
# the first :locale will be used also as the default_locale
Pagy::I18n.load({locale: 'de'},
{locale: 'en'},
{locale: 'es'})
{locale: 'en'},
{locale: 'es'})
# load the "en" built-in locale, a custom "es" locale, and a totally custom locale complete with the :pluralize proc:
Pagy::I18n.load({locale: 'en'},
{locale: 'es', filepath: 'path/to/pagy-es.yml'},
{locale: 'xyz', # not built-in
filepath: 'path/to/pagy-xyz.yml',
pluralize: lambda{|count| ... } )
{locale: 'es', filepath: 'path/to/pagy-es.yml'},
{locale: 'xyz', # not built-in
filepath: 'path/to/pagy-xyz.yml',
pluralize: lambda{|count| ... } )
```

**Notice**: You should use a custom `:pluralize` proc only for pluralization types not included in the built-in [p11n.rb](https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
Expand Down
12 changes: 12 additions & 0 deletions docs/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ The `pagy_get_vars` method works out of the box with `ActiveRecord` collections;
count = collection.count
```

## Custom count for custom scopes

Your scope might become complex and the default pagy `collection.count(:all)` may not get the actual count. In that case you can get the right count with some custom statement, and pass it to `pagy`:

```ruby
custom_scope = ...
custom_count = ...
@pagy, @records = pagy(custom_scope, count: custom_count)
```

**Notice**: pagy will efficiently skip its internal count query and will just use the passed `:count` variable

## Using the pagy_nav* helpers

These helpers take the Pagy object and return the HTML string with the pagination links, which are wrapped in a `nav` tag and are ready to use in your view. For example:
Expand Down

0 comments on commit 4156121

Please sign in to comment.