Skip to content
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

Don't raise KeyError when trying to navigate to nil next_page #203

Merged
merged 1 commit into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/json_api_client/linking/top_level_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def link_url_for(link_name)
end

def fetch_link(link_name)
return unless respond_to_missing?(link_name)
record_class.requestor.linked(link_url_for(link_name))
end
end
Expand Down
45 changes: 45 additions & 0 deletions test/unit/top_level_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ def test_can_parse_pagination_links
assert_pagination
end

def test_can_parse_pagination_links_when_no_next_page
stub_request(:get, "http://example.com/articles")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: [{
type: "articles",
id: "1",
attributes: {
title: "JSON API paints my bikeshed!"
}
}],
links: {
self: "http://example.com/articles",
prev: nil,
first: "http://example.com/articles",
last: "http://example.com/articles?page=1"
}
}.to_json)

assert_pagination_when_no_next_page
end

def test_can_parse_complex_pagination_links
stub_request(:get, "http://example.com/articles")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
Expand Down Expand Up @@ -179,4 +200,28 @@ def assert_pagination
assert_equal "JSON API paints my bikeshed!", article.title
end

def assert_pagination_when_no_next_page
articles = Article.all

# test kaminari pagination params
assert_equal 1, articles.current_page
assert_equal 1, articles.per_page
assert_equal 1, articles.total_pages
assert_equal 0, articles.offset
assert_equal 1, articles.total_entries
assert_equal 1, articles.limit_value
assert_equal nil, articles.next_page
assert_equal 1, articles.per_page
assert_equal false, articles.out_of_bounds?

# test browsing to next page
pages = articles.pages
assert pages.respond_to?(:next)
assert pages.respond_to?(:prev)
assert pages.respond_to?(:last)
assert pages.respond_to?(:first)

page2 = articles.pages.next
assert page2.nil?
end
end