Skip to content

Commit

Permalink
[GEM] Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Apr 16, 2024
2 parents d7e3110 + d49c557 commit 873bab0
Show file tree
Hide file tree
Showing 31 changed files with 142 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/Code.yml
Expand Up @@ -19,7 +19,7 @@ body:
attributes:
label: 👀 Before submitting...
options:
- label: I upgraded to pagy version 8.1.2
- label: I upgraded to pagy version 8.2.0
required: true
- label: I searched through the [Documentation](https://ddnexus.github.io/pagy/)
required: true
Expand Down
6 changes: 4 additions & 2 deletions .github/latest_release_body.md
Expand Up @@ -11,10 +11,12 @@ If you wish to keep your favorites alive, please, [vote here](https://github.com
- Better frontend helpers
- See the [CHANGELOG](https://ddnexus.github.io/pagy/changelog) for possible breaking changes

### Changes in 8.1.2
### Changes in 8.2.0

<!-- changes start -->
- Added "da" locale for aria_label.nav (closes #583)
- Fix the '#pagy_url_for' method for calendar pagination (#688)
- Extend the use of pagy_get_page to the arel, array and countless extras
- Add the pagy_get_count method to the backend
<!-- changes end -->

[CHANGELOG](https://ddnexus.github.io/pagy/changelog)
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,12 @@ If you upgrade from version `< 8.0.0` see the following:

<hr>

## Version 8.2.0

- Fix the '#pagy_url_for' method for calendar pagination (#688)
- Extend the use of pagy_get_page to the arel, array and countless extras
- Add the pagy_get_count method to the backend

## Version 8.1.2

- Added "da" locale for aria_label.nav (closes #583)
Expand Down
2 changes: 1 addition & 1 deletion README.md

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions docs/api/backend.md
Expand Up @@ -72,14 +72,22 @@ self contained custom `pagy` method. (see [Writing your own Pagy methods](#writi
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 `:item_name` 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`.
the output _(see [How to customize the item name](/docs/how-to.md#customize-the-item-name))_.

!!!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.
If you override it, remember that `:count` and `:page` are the only 2 required Pagy core variables, so be careful not to remove them from the returned hash.
!!!

See also the [How To](/docs/how-to.md) page for some usage examples.

==- `pagy_get_count(collection, vars)`

Get the count from the collection, considering also the `:count_args` variable. Override it if you need to calculate the count in some specil way, or cache it.

==- `pagy_get_page(vars)`

Get the `page` from the param ,looking at the `:page_param` variable. See also [Customize the page_param](/docs/how-to.md#customize-the-page-param).

==- `pagy_get_items(collection, pagy)`

Sub-method called only by the `pagy` method, it returns the page items (i.e. the records belonging to the current page).
Expand Down
2 changes: 1 addition & 1 deletion docs/extras/tailwind.md
Expand Up @@ -9,4 +9,4 @@ image: none
# Tailwind Style

Tailwind allows to apply styles to any DOM element, so you don't actually need any special extra to produce a different output.
You can use the standard pagy helpers and `@apply` the tailwind classes by just using the [pagy.tailwind.css](../api/stylesheets/#pagy-tailwind-scss) stylesheet file.
You can use the standard pagy helpers and `@apply` the tailwind classes by just using the [pagy.tailwind.css](../api/stylesheets/#pagy-tailwind-css) stylesheet file.
16 changes: 5 additions & 11 deletions docs/how-to.md
Expand Up @@ -178,7 +178,7 @@ collections in the same action. Depending on the scope of the customization, you
2. `pagy(scope, page_param: :custom_param)` or `Pagy.new(count:100, page_param: :custom_param)` will be used for a single
instance (overriding the global default)

You can also override the `pagy_get_vars` if you need some special way to get the page number.
You can also override the [pagy_get_page](/docs/api/backend.md#pagy-get-page-vars) if you need some special way to get the page number.

## Customize the link attributes

Expand Down Expand Up @@ -731,19 +731,13 @@ You have 2 possible solutions in order to improve the performance.
Depending on the nature of the app, a possible cheap solution would be caching the count of the collection, and Pagy makes that
really simple.

Pagy gets the collection count through its `pagy_get_vars` method, so you can override it in your controller. Here is an example
Pagy gets the collection count through its `pagy_get_count` method, so you can override it in your controller. Here is an example
using the rails cache:

```ruby controller
# override the pagy_get_vars method so it will call your cache_count method
def pagy_get_vars(collection, vars)
vars[:count] ||= cache_count(collection)
vars[:page] ||= params[vars[:page_param] || Pagy::DEFAULT[:page_param]]
vars
end

# add Rails.cache wrapper around the count call
def cache_count(collection)
# override the pagy_get_count method adding
# the Rails.cache wrapper around the count call
def pagy_get_count(collection, _vars)
cache_key = "pagy-#{collection.model.name}:#{collection.to_sql}"
Rails.cache.fetch(cache_key, expires_in: 20 * 60) do
collection.count(:all)
Expand Down
2 changes: 1 addition & 1 deletion docs/migration-guide.md
Expand Up @@ -30,7 +30,7 @@ if that makes sense for Pagy:
- If it doesn't make sense, then just remove the legacy statement.

!!!primary Don't stress if you miss
Don't worry about missing something in this step: if anything won't work as before you can fix it later in the process.
Don't worry about missing something in this step: if anything won't work as before the next steps will fix it.
!!!

#### Preparation
Expand Down
2 changes: 1 addition & 1 deletion e2e/snapshots.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gem/apps/calendar.ru
Expand Up @@ -18,7 +18,7 @@
# DOC
# https://ddnexus.github.io/pagy/playground/#4-calendar-app

VERSION = '8.1.2'
VERSION = '8.2.0'

require 'bundler/inline'
gemfile(true) do
Expand Down
2 changes: 1 addition & 1 deletion gem/apps/demo.ru
Expand Up @@ -18,7 +18,7 @@
# DOC
# https://ddnexus.github.io/pagy/playground/#3-demo-app

VERSION = '8.1.2'
VERSION = '8.2.0'

require 'bundler/inline'
gemfile(true) do
Expand Down
2 changes: 1 addition & 1 deletion gem/apps/rails.ru
Expand Up @@ -15,7 +15,7 @@
# DOC
# https://ddnexus.github.io/pagy/playground/#2-rails-app

VERSION = '8.1.2'
VERSION = '8.2.0'

# Gemfile
require 'bundler/inline'
Expand Down
2 changes: 1 addition & 1 deletion gem/apps/repro.ru
Expand Up @@ -15,7 +15,7 @@
# DOC
# https://ddnexus.github.io/pagy/playground/#1-repro-app

VERSION = '8.1.2'
VERSION = '8.2.0'

require 'bundler/inline'
gemfile(true) do
Expand Down
2 changes: 1 addition & 1 deletion gem/bin/pagy
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

VERSION = '8.1.2'
VERSION = '8.2.0'
APPS = %w[repro rails demo calendar].freeze

require_relative '../lib/optimist'
Expand Down
2 changes: 1 addition & 1 deletion gem/config/pagy.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Pagy initializer file (8.1.2)
# Pagy initializer file (8.2.0)
# Customize only what you really need and notice that the core Pagy works also without any of the following lines.
# Should you just cherry pick part of this file, please maintain the require-order of the extras

Expand Down
4 changes: 2 additions & 2 deletions gem/javascripts/pagy-dev.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gem/javascripts/pagy-module.js
Expand Up @@ -81,7 +81,7 @@ const Pagy = (() => {
const trim = (a, param) => a.replace(new RegExp(`[?&]${param}=1\\b(?!&)|\\b${param}=1&`), "");
// Public interface
return {
version: "8.1.2",
version: "8.2.0",
// Scan for elements with a "data-pagy" attribute and call their init functions with the decoded args
init(arg) {
const target = arg instanceof Element ? arg : document;
Expand Down
2 changes: 1 addition & 1 deletion gem/javascripts/pagy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gem/lib/pagy.rb
Expand Up @@ -5,7 +5,7 @@

# Core class
class Pagy
VERSION = '8.1.2'
VERSION = '8.2.0'

# Gem root pathname to get the path of Pagy files stylesheets, javascripts, apps, locales, etc.
def self.root
Expand Down
9 changes: 7 additions & 2 deletions gem/lib/pagy/backend.rb
Expand Up @@ -18,12 +18,17 @@ def pagy(collection, vars = {})
# You may need to override the count call for non AR collections
def pagy_get_vars(collection, vars)
pagy_set_items_from_params(vars) if defined?(ItemsExtra)
count_args = vars[:count_args] || DEFAULT[:count_args]
vars[:count] ||= (count = collection.count(*count_args)).is_a?(Hash) ? count.size : count
vars[:count] ||= pagy_get_count(collection, vars)
vars[:page] ||= pagy_get_page(vars)
vars
end

# Get the count from the collection
def pagy_get_count(collection, vars)
count_args = vars[:count_args] || DEFAULT[:count_args]
(count = collection.count(*count_args)).is_a?(Hash) ? count.size : count
end

# Get the page integer from the params
# Overridable by the jsonapi extra
def pagy_get_page(vars)
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/arel.rb
Expand Up @@ -16,7 +16,7 @@ def pagy_arel(collection, vars = {})
def pagy_arel_get_vars(collection, vars)
pagy_set_items_from_params(vars) if defined?(ItemsExtra)
vars[:count] ||= pagy_arel_count(collection)
vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
vars[:page] ||= pagy_get_page(vars)
vars
end

Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/array.rb
Expand Up @@ -16,7 +16,7 @@ def pagy_array(array, vars = {})
def pagy_array_get_vars(array, vars)
pagy_set_items_from_params(vars) if defined?(ItemsExtra)
vars[:count] ||= array.size
vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
vars[:page] ||= pagy_get_page(vars)
vars
end
end
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/countless.rb
Expand Up @@ -19,7 +19,7 @@ def pagy_countless(collection, vars = {})
# Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
def pagy_countless_get_vars(_collection, vars)
pagy_set_items_from_params(vars) if defined?(ItemsExtra)
vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
vars[:page] ||= pagy_get_page(vars)
vars
end

Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/url_helpers.rb
Expand Up @@ -8,7 +8,7 @@ module UrlHelpers
# For non-rack environments you can use the standalone extra
def pagy_url_for(pagy, page, absolute: false, **_)
vars = pagy.vars
query_params = request.GET
query_params = request.GET.clone
query_params.merge!(vars[:params].transform_keys(&:to_s)) if vars[:params].is_a?(Hash)
pagy_set_query_params(page, vars, query_params)
query_params = vars[:params].(query_params) if vars[:params].is_a?(Proc)
Expand Down
2 changes: 1 addition & 1 deletion quick-start.md
Expand Up @@ -40,7 +40,7 @@ If you use Bundler, add the gem in the Gemfile, optionally avoiding the next maj
see [RubyGem Specifiers](http://guides.rubygems.org/patterns/#pessimistic-version-constraint)):

```ruby Gemfile
gem 'pagy', '~> 8.1' # omit patch digit
gem 'pagy', '~> 8.2' # omit patch digit
```

+++ Without Bundler
Expand Down
2 changes: 1 addition & 1 deletion retype.yml
Expand Up @@ -8,7 +8,7 @@ url: https://ddnexus.github.io/pagy

branding:
title: Pagy
label: 8.1.2
label: 8.2.0
colors:
label:
text: "#FFFFFF"
Expand Down
25 changes: 25 additions & 0 deletions scripts/hooks/README.md
@@ -0,0 +1,25 @@
# Git Hooks

A few hooks to ensure consistency and automation. Please setup the hooks if you contribute to pagy with PRs.

See also the brief description inside each hook file.

## Setup

### Symlink

Symlink the hooks into the `.git/hooks` dir:

```shell
# from the repo root
ln -s ../../scripts/hooks/{pre-commit,commit-msg} .git/hooks
```

### Copy

Only for filesystem that don't support symlinks: copy the hooks into the `.git/hooks` dir:

```shell
# from the repo root
cp scripts/hooks/{pre-commit,commit-msg} .git/hooks
```
18 changes: 18 additions & 0 deletions scripts/hooks/commit-msg
@@ -0,0 +1,18 @@
#!/bin/bash

# Ensure the commit message includes the prefix when it changes any file in the gem root path
# Used to automate the filtering of the commit messages in the CHANGELOG

set -e

prefix='[GEM] '

gem_changes=$(git diff --cached --name-status --relative=gem)
if [[ -n "$gem_changes" ]]
then
msg=$(cat "$1")
if [[ $msg != "$prefix"* ]]
then
echo "${prefix}${msg}" > "$1"
fi
fi
27 changes: 27 additions & 0 deletions scripts/hooks/pre-commit
@@ -0,0 +1,27 @@
#!/bin/bash

# Ensure no gem changes are committed directly to master
# Used to ensure proper testing and the gem code in master consistent with the last release

set -e

# Redirect output to stderr
exec 1>&2

gem_changes=$(git diff --cached --name-status --relative=gem)
if [[ -n "$gem_changes" ]]
then
branch=$(git rev-parse --abbrev-ref HEAD)
if [[ $branch == master* ]]
then
cat <<-EOF
Error: The following changes in the "gem" root path
should not be committed directly to the "$branch" branch,
but to a dev branch which will be tested and merged
into the "$branch" branch only on release:
$gem_changes
EOF
exit 1
fi
fi

0 comments on commit 873bab0

Please sign in to comment.