Skip to content

Commit

Permalink
added countless support to the items extra
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Nov 19, 2018
1 parent f272faf commit 04fa7a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/extras/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ title: Items

Allow the client to request a custom number of items per page with an optional selector UI. It is useful with APIs or higly user-customizable apps.

It works also with the [countless extra](countless.md).

## Synopsys

See [extras](../extras.md) for general usage info.
Expand Down Expand Up @@ -67,6 +69,10 @@ The `items` extra overrides a couple of builtin methods and adds a helper to the

This extra overrides the `pagy_get_vars` method of the `Pagy::Backend` module in order to set the `:items` variable, pulled from the request-params.

### pagy_countless_get_vars(collection, vars)

This extra overrides the `pagy_countless_get_vars` method of the `Pagy::Backend` module (added by the `countless` extra) in order to set the `:items` variable, pulled from the request-params.

### pagy_url_for(page, pagy)

This extra overrides also the `pagy_url_for` method of the `Pagy::Frontend` module in order to add the `:items_param` param to the url of the links.
Expand Down
18 changes: 16 additions & 2 deletions lib/pagy/extras/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,28 @@ class Pagy
# Handle a custom number of :items from params
module Backend ; private

alias_method :pagy_get_vars_without_items, :pagy_get_vars
def pagy_get_vars_with_items(collection, vars)
def pagy_with_items(vars)
vars[:items] ||= (items = params[vars[:items_param] || VARS[:items_param]]) && # :items from :items_param
[items&.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
end

alias_method :pagy_get_vars_without_items, :pagy_get_vars
def pagy_get_vars_with_items(collection, vars)
pagy_with_items(vars)
pagy_get_vars_without_items(collection, vars)
end
alias_method :pagy_get_vars, :pagy_get_vars_with_items

# support for countless extra
if defined?(Pagy::COUNTLESS) # defined in the countless extra
alias_method :pagy_countless_get_vars_without_items, :pagy_countless_get_vars
def pagy_countless_get_vars_with_items(collection, vars)
pagy_with_items(vars)
pagy_countless_get_vars_without_items(collection, vars)
end
alias_method :pagy_countless_get_vars, :pagy_countless_get_vars_with_items
end

end

module Frontend
Expand Down
24 changes: 22 additions & 2 deletions test/pagy/extras/items_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require_relative '../../test_helper'
require 'pagy/extras/countless'
require 'pagy/extras/items'

SingleCov.covered!
SingleCov.covered!(uncovered: 1)

describe Pagy::Backend do

describe "#pagy_get_vars" do
describe "#pagy_get_vars and #pagy_countless_get_vars" do

before do
@collection = TestCollection.new((1..1000).to_a)
Expand All @@ -17,6 +18,9 @@
merged = backend.send :pagy_get_vars, @collection, vars
merged.keys.must_include :items
merged[:items].must_be_nil
merged_countless = backend.send :pagy_countless_get_vars, @collection, vars
merged_countless.keys.must_include :items
merged_countless[:items].must_be_nil
end

it 'uses the vars' do
Expand All @@ -26,6 +30,8 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 15
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless.items.must_equal 15
end

it 'uses the params' do
Expand All @@ -35,6 +41,8 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 12
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless.items.must_equal 12
end

it 'overrides the params' do
Expand All @@ -44,6 +52,8 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 21
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless.items.must_equal 21
end

it 'uses the max_items default' do
Expand All @@ -53,6 +63,8 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 100
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless.items.must_equal 100
end

it 'doesn\'t limit the items from vars' do
Expand All @@ -62,6 +74,8 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 1000
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless.items.must_equal 1000
end

it 'doesn\'t limit the items from default' do
Expand All @@ -73,6 +87,8 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 1000
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless.items.must_equal 1000
}
end

Expand All @@ -83,6 +99,8 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 14
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless.items.must_equal 14
end

it 'uses items_param from default' do
Expand All @@ -94,6 +112,8 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 15
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless.items.must_equal 15
}
end

Expand Down

0 comments on commit 04fa7a2

Please sign in to comment.