Skip to content

Commit 42770b8

Browse files
committed
Use all next/previous link params
1 parent f2c05be commit 42770b8

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

lib/shopify_api/collection_pagination.rb

+13-14
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,44 @@ module ShopifyAPI
22
module CollectionPagination
33

44
def next_page?
5-
next_page_info.present?
5+
next_url_params.present?
66
end
77

88
def previous_page?
9-
previous_page_info.present?
9+
previous_url_params.present?
1010
end
1111

1212
def fetch_next_page
13-
fetch_page(next_page_info)
13+
fetch_page(next_url_params)
1414
end
1515

1616
def fetch_previous_page
17-
fetch_page(previous_page_info)
17+
fetch_page(previous_url_params)
1818
end
1919

2020
private
2121

2222
AVAILABLE_IN_VERSION = ShopifyAPI::ApiVersion::Unstable.new
2323

24-
def fetch_page(page_info)
25-
return [] unless page_info
24+
def fetch_page(url_params)
25+
return [] unless url_params.present?
2626

27-
resource_class.where(original_params.merge(page_info: page_info))
27+
resource_class.where(url_params)
2828
end
2929

30-
def previous_page_info
31-
@previous_page_info ||= extract_page_info(pagination_link_headers.previous_link)
30+
def previous_url_params
31+
@previous_url_params ||= extract_url_params(pagination_link_headers.previous_link)
3232
end
3333

34-
def next_page_info
35-
@next_page_info ||= extract_page_info(pagination_link_headers.next_link)
34+
def next_url_params
35+
@next_url_params ||= extract_url_params(pagination_link_headers.next_link)
3636
end
3737

38-
def extract_page_info(link_header)
38+
def extract_url_params(link_header)
3939
raise NotImplementedError unless ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION
4040

4141
return nil unless link_header.present?
42-
43-
CGI.parse(link_header.url.query).dig("page_info", 0)
42+
Rack::Utils.parse_nested_query(link_header.url.query)
4443
end
4544

4645
def pagination_link_headers

test/pagination_test.rb

+8-9
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ def setup
1313
@previous_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@previous_page_info}>; rel=\"previous\""
1414
end
1515

16-
test "navigates using next and previous link headers" do
17-
link_header =
18-
"<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@previous_page_info}>; rel=\"previous\",\
19-
<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>; rel=\"next\""
16+
test "navigates using next and previous link headers with no original params" do
17+
link_header ="#{@previous_link_header}, #{@next_link_header}"
2018

2119
fake 'orders', :method => :get, :status => 200, api_version: @version, :body => load_fixture('orders'), :link => link_header
2220
orders = ShopifyAPI::Order.all
@@ -28,7 +26,6 @@ def setup
2826
status: 200,
2927
body: load_fixture('orders')
3028
)
31-
3229
next_page = orders.fetch_next_page
3330
assert_equal 450789469, next_page.first.id
3431

@@ -44,24 +41,26 @@ def setup
4441
assert_equal 1122334455, previous_page.first.id
4542
end
4643

47-
test "retains previous querystring parameters" do
44+
test "uses all passed in querystring parameters" do
45+
params = "page_info=#{@next_page_info}&limit=50&fields=#{CGI.escape('id,created_at')}"
46+
@next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{params}>; rel=\"next\""
4847
fake(
4948
'orders',
5049
method: :get,
5150
status: 200,
5251
api_version: @version,
53-
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Cupdated_at",
52+
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Cupdated_at&limit=100",
5453
body: load_fixture('orders'),
5554
link: @next_link_header
5655
)
57-
orders = ShopifyAPI::Order.where(fields: 'id,updated_at')
56+
orders = ShopifyAPI::Order.where(fields: 'id,updated_at', limit: 100)
5857

5958
fake(
6059
'orders',
6160
method: :get,
6261
status: 200,
6362
api_version: @version,
64-
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Cupdated_at&page_info=#{@next_page_info}",
63+
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Ccreated_at&limit=50&page_info=#{@next_page_info}",
6564
body: load_fixture('orders')
6665
)
6766
next_page = orders.fetch_next_page

0 commit comments

Comments
 (0)