-
Notifications
You must be signed in to change notification settings - Fork 182
fix: make page[] param wrap consistent #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gaorlov
merged 4 commits into
JsonApiClient:master
from
dugsmith:feature/fix_pagination_issues
Jun 28, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d675eda
chore: update .gitignore to ignore IntelliJ and asdf files
cc4b697
feat: add NestedParamPaginator to make pagination params consistent
0cd860b
chore: add NestedParamPaginator to README and CHANGELOG
edc52d5
Merge branch 'master' into feature/fix_pagination_issues
gaorlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,10 @@ Gemfile.lock | |
*.gemfile.lock | ||
|
||
/coverage | ||
|
||
# IntellJ/RubyMine | ||
*.iml | ||
.idea/ | ||
|
||
# asdf config | ||
.tool-versions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module JsonApiClient | ||
module Paginating | ||
autoload :Paginator, 'json_api_client/paginating/paginator' | ||
autoload :NestedParamPaginator, 'json_api_client/paginating/nested_param_paginator' | ||
end | ||
end | ||
end |
140 changes: 140 additions & 0 deletions
140
lib/json_api_client/paginating/nested_param_paginator.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
module JsonApiClient | ||
module Paginating | ||
# An alternate, more consistent Paginator that always wraps | ||
# pagination query string params in a top-level wrapper_name, | ||
# e.g. page[offset]=2, page[limit]=10. | ||
class NestedParamPaginator | ||
DEFAULT_WRAPPER_NAME = "page".freeze | ||
DEFAULT_PAGE_PARAM = "page".freeze | ||
DEFAULT_PER_PAGE_PARAM = "per_page".freeze | ||
|
||
# Define class accessors as methods to enforce standard way | ||
# of defining pagination related query string params. | ||
class << self | ||
|
||
def wrapper_name | ||
@_wrapper_name ||= DEFAULT_WRAPPER_NAME | ||
end | ||
|
||
def wrapper_name=(param = DEFAULT_WRAPPER_NAME) | ||
raise ArgumentError, "don't wrap wrapper_name" unless valid_param?(param) | ||
|
||
@_wrapper_name = param.to_s | ||
end | ||
|
||
def page_param | ||
@_page_param ||= DEFAULT_PAGE_PARAM | ||
"#{wrapper_name}[#{@_page_param}]" | ||
end | ||
|
||
def page_param=(param = DEFAULT_PAGE_PARAM) | ||
raise ArgumentError, "don't wrap page_param" unless valid_param?(param) | ||
|
||
@_page_param = param.to_s | ||
end | ||
|
||
def per_page_param | ||
@_per_page_param ||= DEFAULT_PER_PAGE_PARAM | ||
"#{wrapper_name}[#{@_per_page_param}]" | ||
end | ||
|
||
def per_page_param=(param = DEFAULT_PER_PAGE_PARAM) | ||
raise ArgumentError, "don't wrap per_page_param" unless valid_param?(param) | ||
|
||
@_per_page_param = param | ||
end | ||
|
||
private | ||
|
||
def valid_param?(param) | ||
!(param.nil? || param.to_s.include?("[") || param.to_s.include?("]")) | ||
end | ||
|
||
end | ||
|
||
attr_reader :params, :result_set, :links | ||
|
||
def initialize(result_set, data) | ||
@params = params_for_uri(result_set.uri) | ||
@result_set = result_set | ||
@links = data["links"] | ||
end | ||
|
||
def next | ||
result_set.links.fetch_link("next") | ||
end | ||
|
||
def prev | ||
result_set.links.fetch_link("prev") | ||
end | ||
|
||
def first | ||
result_set.links.fetch_link("first") | ||
end | ||
|
||
def last | ||
result_set.links.fetch_link("last") | ||
end | ||
|
||
def total_pages | ||
if links["last"] | ||
uri = result_set.links.link_url_for("last") | ||
last_params = params_for_uri(uri) | ||
last_params.fetch(page_param, &method(:current_page)).to_i | ||
else | ||
current_page | ||
end | ||
end | ||
|
||
# this is an estimate, not necessarily an exact count | ||
def total_entries | ||
per_page * total_pages | ||
end | ||
def total_count; total_entries; end | ||
|
||
def offset | ||
per_page * (current_page - 1) | ||
end | ||
|
||
def per_page | ||
params.fetch(per_page_param) do | ||
result_set.length | ||
end.to_i | ||
end | ||
|
||
def current_page | ||
params.fetch(page_param, 1).to_i | ||
end | ||
|
||
def out_of_bounds? | ||
current_page > total_pages | ||
end | ||
|
||
def previous_page | ||
current_page > 1 ? (current_page - 1) : nil | ||
end | ||
|
||
def next_page | ||
current_page < total_pages ? (current_page + 1) : nil | ||
end | ||
|
||
def page_param | ||
self.class.page_param | ||
end | ||
|
||
def per_page_param | ||
self.class.per_page_param | ||
end | ||
|
||
alias limit_value per_page | ||
|
||
protected | ||
|
||
def params_for_uri(uri) | ||
return {} unless uri | ||
uri = Addressable::URI.parse(uri) | ||
( uri.query_values || {} ).with_indifferent_access | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'test_helper' | ||
|
||
class NestedParamPaginatorTest < MiniTest::Test | ||
|
||
class Book < JsonApiClient::Resource | ||
self.site = "http://example.com/" | ||
end | ||
|
||
def setup | ||
@nested_param_paginator = JsonApiClient::Paginating::NestedParamPaginator | ||
@default_paginator = JsonApiClient::Paginating::Paginator | ||
Article.paginator = @nested_param_paginator | ||
end | ||
|
||
def teardown | ||
@nested_param_paginator.page_param = @nested_param_paginator::DEFAULT_PAGE_PARAM | ||
@nested_param_paginator.per_page_param = @nested_param_paginator::DEFAULT_PER_PAGE_PARAM | ||
Article.paginator = @default_paginator | ||
end | ||
|
||
def test_default_page_params_wrapped_consistently | ||
assert_equal "page[page]", @nested_param_paginator.page_param | ||
assert_equal "page[per_page]", @nested_param_paginator.per_page_param | ||
end | ||
|
||
def test_custom_page_params_wrapped_consistently | ||
@nested_param_paginator.page_param = "offset" | ||
@nested_param_paginator.per_page_param = "limit" | ||
assert_equal "page[offset]", @nested_param_paginator.page_param | ||
assert_equal "page[limit]", @nested_param_paginator.per_page_param | ||
end | ||
|
||
def test_custom_page_param_does_not_allow_double_wrap | ||
assert_raises ArgumentError do | ||
@nested_param_paginator.page_param = "page[number]" | ||
end | ||
end | ||
|
||
def test_custom_per_page_param_does_not_allow_double_wrap | ||
assert_raises ArgumentError do | ||
@nested_param_paginator.per_page_param = "page[size]" | ||
end | ||
end | ||
|
||
def test_pagination_params_total_calculations | ||
@nested_param_paginator.page_param = "number" | ||
@nested_param_paginator.per_page_param = "size" | ||
stub_request(:get, "http://example.com/articles?page[number]=1&page[size]=2") | ||
.to_return(headers: {content_type: "application/vnd.api+json"}, body: { | ||
data: [{ | ||
type: "articles", | ||
id: "1", | ||
attributes: { | ||
title: "JSON API paints my bikeshed!" | ||
} | ||
}, | ||
{ | ||
type: "articles", | ||
id: "2", | ||
attributes: { | ||
title: "json_api_client counts pages correctly" | ||
} | ||
}], | ||
links: { | ||
self: "http://example.com/articles?page[number]=1&page[size]=2", | ||
next: "http://example.com/articles?page[number]=2&page[size]=2", | ||
prev: nil, | ||
first: "http://example.com/articles?page[number]=1&page[size]=2", | ||
last: "http://example.com/articles?page[number]=4&page[size]=2" | ||
} | ||
}.to_json) | ||
|
||
articles = Article.paginate(page: 1, per_page: 2).to_a | ||
assert_equal 1, articles.current_page | ||
assert_equal 4, articles.total_pages | ||
assert_equal 8, articles.total_entries | ||
ensure | ||
@nested_param_paginator.page_param = "page" | ||
@nested_param_paginator.per_page_param = "per_page" | ||
end | ||
|
||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.