Skip to content

Commit

Permalink
deprecated :anchor variable in favor of :fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Apr 29, 2021
1 parent 034afe8 commit d791a04
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 38 deletions.
18 changes: 9 additions & 9 deletions docs/api/pagy.md
Expand Up @@ -81,15 +81,15 @@ They are all integers:

### Other Variables

| Variable | Description | Default |
|:--------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-------------------|
| `:size` | the size of the page links to show: array of initial pages, before current page, after current page, final pages. _(see also [Control the page links](../how-to.md#controlling-the-page-links))_ | `[1,4,4,1]` |
| `:page_param` | the name of the page param name used in the url. _(see [Customizing the page param](../how-to.md#customizing-the-page-param))_ | `:page` |
| `:params` | the arbitrary param hash to add to the url. _(see [Customizing the params](../how-to.md#customizing-the-params))_ | `{}` |
| `:anchor` | the arbitrary anchor string (including the "#") to add to the url. _(see [Customizing the params](../how-to.md#customizing-the-params))_ | `""` |
| `:link_extra` | the extra attributes string (formatted as a valid HTML attribute/value pairs) added to the page links _(see [Customizing the link attributes](../how-to.md#customizing-the-link-attributes))_ | `""` |
| `:i18n_key` | the i18n key to lookup the `item_name` that gets interpolated in a few helper outputs _(see [Customizing the item name](../how-to.md#customizing-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` |
| Variable | Description | Default |
|:---------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-------------------|
| `:size` | the size of the page links to show: array of initial pages, before current page, after current page, final pages. _(see also [Control the page links](../how-to.md#controlling-the-page-links))_ | `[1,4,4,1]` |
| `:page_param` | the name of the page param name used in the url. _(see [Customizing the page param](../how-to.md#customizing-the-page-param))_ | `:page` |
| `:params` | the arbitrary param hash to add to the url. _(see [Customizing the params](../how-to.md#customizing-the-params))_ | `{}` |
| `:fragment` | the arbitrary fragment string (including the "#") to add to the url. _(see [Customizing the params](../how-to.md#customizing-the-params))_ | `""` |
| `:link_extra` | the extra attributes string (formatted as a valid HTML attribute/value pairs) added to the page links _(see [Customizing the link attributes](../how-to.md#customizing-the-link-attributes))_ | `""` |
| `:i18n_key` | the i18n key to lookup the `item_name` that gets interpolated in a few helper outputs _(see [Customizing the item name](../how-to.md#customizing-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` |

There is no specific validation for non-instance variables.

Expand Down
8 changes: 4 additions & 4 deletions docs/how-to.md
Expand Up @@ -242,13 +242,13 @@ def pagy_get_params(params)
end
```

You can also use the `:params` and : `:anchor` variables to add arbitrary params to the URLs of the pages. For example:
You can also use the `:params` and : `:fragment` variables to add arbitrary params to the URLs of the pages. For example:

```ruby
@pagy, @records = pagy(some_scope, params: {custom: 'param'}, anchor: '#your-anchor')
@pagy, @records = pagy(some_scope, params: {custom: 'param'}, fragment: '#your-fragment')
```

**IMPORTANT**: For performance reasons the `:anchor` string must include the `"#"`.
**IMPORTANT**: For performance reasons the `:fragment` string must include the `"#"`.

## Customizing the URL

Expand Down Expand Up @@ -313,7 +313,7 @@ You have a few ways to do that:
3. you can override entirely the `:item_name` by passing an already pluralized string directly to the helper call:
```erb
<%== pagy_info(@pagy, item_name: 'Product'.pluralize(@pagy.count)) %>
<%== pagy_items_selector_js(@pagy, item_name: 'Product'.pluralize(@pagy.count)) %>
<%== pagy_items_selector_js(@pagy, item_name: 'Product'.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.
Expand Down
2 changes: 1 addition & 1 deletion lib/config/pagy.rb
Expand Up @@ -23,7 +23,7 @@
# Pagy::VARS[:size] = [1,4,4,1] # default
# Pagy::VARS[:page_param] = :page # default
# Pagy::VARS[:params] = {} # default
# Pagy::VARS[:anchor] = '#anchor' # example
# Pagy::VARS[:fragment] = '#fragment' # example
# Pagy::VARS[:link_extra] = 'data-remote="true"' # example
# Pagy::VARS[:i18n_key] = 'pagy.item_name' # default
# Pagy::VARS[:cycle] = true # example
Expand Down
5 changes: 3 additions & 2 deletions lib/pagy.rb
Expand Up @@ -14,7 +14,7 @@ def self.root

# default vars
VARS = { page: 1, items: 20, outset: 0, size: [1, 4, 4, 1], page_param: :page, # rubocop:disable Style/MutableConstant
params: {}, anchor: '', link_extra: '', i18n_key: 'pagy.item_name', cycle: false }
params: {}, fragment: '', link_extra: '', i18n_key: 'pagy.item_name', cycle: false }

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

Expand All @@ -23,6 +23,7 @@ def self.root
# Merge and validate the options, do some simple arithmetic and set the instance variables
def initialize(vars)
@vars = VARS.merge( vars.delete_if{|_,v| v.nil? || v == '' } )
@vars[:fragment] = deprecated_var(:anchor, @vars[:anchor], :fragment, @vars[:fragment]) if @vars[:anchor]

INSTANCE_VARS_MIN.each do |name,min|
raise VariableError.new(self), "expected :#{name} >= #{min}; got #{@vars[name].inspect}" \
Expand Down Expand Up @@ -66,7 +67,7 @@ def series(size=@vars[:size])
series[series.index(@page)] = @page.to_s
series
end

end

require 'pagy/deprecation'
Expand Down
11 changes: 9 additions & 2 deletions lib/pagy/deprecation.rb
Expand Up @@ -2,14 +2,21 @@
class Pagy
class << self

# deprecated pagy_url_for rgument order
# deprecated variables
def deprecated_var(var, val, new_var, new_val)
value = new_val || val
Warning.warn %([PAGY WARNING] deprecated use of `#{var}` var will not be supported in 5.0! Use `#{new_var}: #{value.inspect}` instead.)
value
end

# deprecated pagy_url_for argument order
def deprecated_order(pagy, page)
Warning.warn '[PAGY WARNING] inverted use of pagy/page in pagy_url_for will not be supported in 5.0! Use pagy_url_for(pagy, page) instead.'
[page, pagy]
end


# deprecated arguments to remove in 5.0
# deprecated posiitioal arguments
def deprecated_arg(arg, val, new_key, new_val)
value = new_val || val # we use the new_val if present
Warning.warn %([PAGY WARNING] deprecated use of positional `#{arg}` arg will not be supported in 5.0! Use only the keyword arg `#{new_key}: #{value.inspect}` instead.)
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/frontend.rb
Expand Up @@ -21,7 +21,7 @@ def pagy_url_for(pagy, page, deprecated_url=nil, absolute: nil)
params[p_vars[:page_param].to_s] = page
params[p_vars[:items_param].to_s] = p_vars[:items] if defined?(UseItemsExtra)
query_string = "?#{Rack::Utils.build_nested_query(pagy_get_params(params))}" unless params.empty?
"#{request.base_url if absolute}#{request.path}#{query_string}#{p_vars[:anchor]}"
"#{request.base_url if absolute}#{request.path}#{query_string}#{p_vars[:fragment]}"
end

# Sub-method called only by #pagy_url_for: here for easy customization of params by overriding
Expand Down
11 changes: 9 additions & 2 deletions test/pagy/derecation_test.rb
Expand Up @@ -3,7 +3,14 @@
require_relative '../test_helper'

describe 'pagy/deprecation' do


describe 'Pagy.deprecated_var' do
it 'deprecate arg and returns right value' do
_ { _(Pagy.deprecated_var(:var, 'deprecated-val', :new_var, 'new-value')).must_equal 'new-value' }.must_output nil, \
"[PAGY WARNING] deprecated use of `var` var will not be supported in 5.0! Use `new_var: \"new-value\"` instead."
end
end

describe 'Pagy.deprecated_arg' do
it 'deprecate arg and returns right value' do
_ { _(Pagy.deprecated_arg(:arg, 'deprecated-val', :new_key, 'new-value')).must_equal 'new-value' }.must_output nil, \
Expand All @@ -15,6 +22,6 @@
it 'deprecate arg order and returns them inverted' do
_ { _(Pagy.deprecated_order('page', 'pagy')).must_equal %w[pagy page] }.must_output nil, \
"[PAGY WARNING] inverted use of pagy/page in pagy_url_for will not be supported in 5.0! Use pagy_url_for(pagy, page) instead."
end
end
end
end
12 changes: 6 additions & 6 deletions test/pagy/extras/items_test.rb
Expand Up @@ -122,13 +122,13 @@ def test_items_vars_params(items, vars, params)
pagy = Pagy.new count: 1000, page: 3, items_param: :custom
_(view.pagy_url_for(pagy, 5)).must_equal '/foo?page=5&custom=20'
end
it 'renders url with anchor' do
pagy = Pagy.new count: 1000, page: 3, anchor: '#anchor'
_(view.pagy_url_for(pagy, 6)).must_equal '/foo?page=6&items=20#anchor'
it 'renders url with fragment' do
pagy = Pagy.new count: 1000, page: 3, fragment: '#fragment'
_(view.pagy_url_for(pagy, 6)).must_equal '/foo?page=6&items=20#fragment'
end
it 'renders url with params and anchor' do
pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, anchor: '#anchor', items: 40
_(view.pagy_url_for(pagy, 5)).must_equal "/foo?page=5&a=3&b=4&items=40#anchor"
it 'renders url with params and fragment' do
pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, fragment: '#fragment', items: 40
_(view.pagy_url_for(pagy, 5)).must_equal "/foo?page=5&a=3&b=4&items=40#fragment"
end
end
it 'renders items selector' do
Expand Down
2 changes: 1 addition & 1 deletion test/pagy/extras/metadata_test.rb
Expand Up @@ -18,7 +18,7 @@
it 'returns the full pagy metadata' do
pagy, _records = @controller.send(:pagy, @collection)
_(@controller.send(:pagy_metadata, pagy)).must_equal(
{:scaffold_url=>"/foo?page=__pagy_page__", :first_url=>"/foo?page=1", :prev_url=>"/foo?page=2", :page_url=>"/foo?page=3", :next_url=>"/foo?page=4", :last_url=>"/foo?page=50", :count=>1000, :page=>3, :items=>20, :vars=>{:page=>3, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :anchor=>"", :link_extra=>"", :i18n_key=>"pagy.item_name", :cycle=>false, :steps=>false, :metadata=>[:scaffold_url, :first_url, :prev_url, :page_url, :next_url, :last_url, :count, :page, :items, :vars, :pages, :last, :from, :to, :prev, :next, :series, :sequels], :count=>1000}, :pages=>50, :last=>50, :from=>41, :to=>60, :prev=>2, :next=>4, :series=>[1, 2, "3", 4, 5, 6, 7, :gap, 50], :sequels=>{"0"=>[1, 2, "3", 4, 5, 6, 7, :gap, 50]}}
{:scaffold_url=>"/foo?page=__pagy_page__", :first_url=>"/foo?page=1", :prev_url=>"/foo?page=2", :page_url=>"/foo?page=3", :next_url=>"/foo?page=4", :last_url=>"/foo?page=50", :count=>1000, :page=>3, :items=>20, :vars=>{:page=>3, :items=>20, :outset=>0, :size=>[1, 4, 4, 1], :page_param=>:page, :params=>{}, :fragment=>"", :link_extra=>"", :i18n_key=>"pagy.item_name", :cycle=>false, :steps=>false, :metadata=>[:scaffold_url, :first_url, :prev_url, :page_url, :next_url, :last_url, :count, :page, :items, :vars, :pages, :last, :from, :to, :prev, :next, :series, :sequels], :count=>1000}, :pages=>50, :last=>50, :from=>41, :to=>60, :prev=>2, :next=>4, :series=>[1, 2, "3", 4, 5, 6, 7, :gap, 50], :sequels=>{"0"=>[1, 2, "3", 4, 5, 6, 7, :gap, 50]}}
)
end
it 'checks for unknown metadata' do
Expand Down
20 changes: 10 additions & 10 deletions test/pagy/frontend_test.rb
Expand Up @@ -139,23 +139,23 @@
_(view.pagy_url_for(pagy, 5)).must_equal "/foo?page=5&a=3&b=4"
_(view.pagy_url_for(pagy, 5, absolute: true)).must_equal "http://example.com:3000/foo?page=5&a=3&b=4"
end
it 'renders url with anchor' do
pagy = Pagy.new count: 1000, page: 3, anchor: '#anchor'
_(view.pagy_url_for(pagy, 6)).must_equal '/foo?page=6#anchor'
_(view.pagy_url_for(pagy, 6, absolute: true)).must_equal 'http://example.com:3000/foo?page=6#anchor'
it 'renders url with fragment' do
pagy = Pagy.new count: 1000, page: 3, fragment: '#fragment'
_(view.pagy_url_for(pagy, 6)).must_equal '/foo?page=6#fragment'
_(view.pagy_url_for(pagy, 6, absolute: true)).must_equal 'http://example.com:3000/foo?page=6#fragment'
end
it 'renders url with params and anchor' do
pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, anchor: '#anchor'
_(view.pagy_url_for(pagy, 5)).must_equal "/foo?page=5&a=3&b=4#anchor"
_(view.pagy_url_for(pagy, 5, absolute: true)).must_equal "http://example.com:3000/foo?page=5&a=3&b=4#anchor"
it 'renders url with params and fragment' do
pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, fragment: '#fragment'
_(view.pagy_url_for(pagy, 5)).must_equal "/foo?page=5&a=3&b=4#fragment"
_(view.pagy_url_for(pagy, 5, absolute: true)).must_equal "http://example.com:3000/foo?page=5&a=3&b=4#fragment"
end
end

describe '#pagy_get_params' do
it 'overrides params' do
overridden = MockView::Overridden.new
pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, anchor: '#anchor'
_(overridden.pagy_url_for(pagy, 5)).must_equal "/foo?page=5&b=4&k=k#anchor"
pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, fragment: '#fragment'
_(overridden.pagy_url_for(pagy, 5)).must_equal "/foo?page=5&b=4&k=k#fragment"
end
end

Expand Down

0 comments on commit d791a04

Please sign in to comment.