Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9bf38c0

Browse files
author
Brandon Sislow
committedApr 17, 2014
Added will_paginate and kaminari pagination methods and tests
1 parent 4820a94 commit 9bf38c0

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed
 

‎lib/json_api_client/result_set.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@ module JsonApiClient
22
class ResultSet < Array
33

44
attr_accessor :total_pages, :total_entries, :offset, :per_page, :current_page, :errors
5+
alias_attribute :limit_value, :per_page
56

67
def self.build(klass, data)
78
result_data = data.fetch(klass.table_name, [])
89
new(result_data.map {|attributes| klass.new(attributes) }).tap do |result_set|
910
yield(result_set) if block_given?
1011
end
1112
end
13+
14+
def out_of_bounds?
15+
current_page > total_pages
16+
end
17+
18+
def previous_page
19+
current_page > 1 ? (current_page - 1) : nil
20+
end
21+
22+
def next_page
23+
current_page < total_pages ? (current_page + 1) : nil
24+
end
1225
end
1326
end

‎test/unit/pagination_test.rb

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ def test_meta_data
4949

5050
def test_custom_meta_data
5151
stub_request(:get, "http://localhost:3000/api/1/custom_paginations.json")
52-
.to_return(headers: {content_type: "application/json"}, body: {
53-
custom_paginations: [
54-
{id: 1, name: "Jeff Ching", email_address: "ching.jeff@gmail.com"},
55-
{id: 2, name: "Barry Bonds", email_address: "barry@bonds.com"},
56-
{id: 3, name: "Hank Aaron", email_address: "hank@aaron.com"}
57-
],
58-
meta: {
59-
per: 3,
60-
page: 2,
61-
total: 10
62-
}
63-
}.to_json)
52+
.to_return(headers: {content_type: "application/json"}, body: {
53+
custom_paginations: [
54+
{id: 1, name: "Jeff Ching", email_address: "ching.jeff@gmail.com"},
55+
{id: 2, name: "Barry Bonds", email_address: "barry@bonds.com"},
56+
{id: 3, name: "Hank Aaron", email_address: "hank@aaron.com"}
57+
],
58+
meta: {
59+
per: 3,
60+
page: 2,
61+
total: 10
62+
}
63+
}.to_json)
6464

6565
users = CustomPagination.all
6666
assert_equal 3, users.length
@@ -71,4 +71,58 @@ def test_custom_meta_data
7171
assert_equal 4, users.total_pages
7272
end
7373

74+
# will_paginate gem specific parameters check
75+
# https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/collection.rb
76+
def test_will_paginate_params
77+
stub_request(:get, "http://localhost:3000/api/1/custom_paginations.json")
78+
.to_return(headers: {content_type: "application/json"}, body: {
79+
custom_paginations: [
80+
{id: 1, name: "Jeff Ching", email_address: "ching.jeff@gmail.com"},
81+
{id: 2, name: "Barry Bonds", email_address: "barry@bonds.com"},
82+
{id: 3, name: "Hank Aaron", email_address: "hank@aaron.com"}
83+
],
84+
meta: {
85+
per: 3,
86+
page: 2,
87+
total: 10
88+
}
89+
}.to_json)
90+
91+
users = CustomPagination.all
92+
assert_equal 4, users.total_pages
93+
assert_equal 3, users.offset
94+
assert_equal 1, users.previous_page
95+
assert_equal 3, users.next_page
96+
assert_equal false, users.out_of_bounds?
97+
assert_equal 2, users.current_page
98+
assert_equal 10, users.total_entries
99+
assert_equal 3, users.per_page
100+
end
101+
102+
# kaminari gem specific parameters check
103+
# https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/models/array_extension.rb
104+
def test_kaminari_params
105+
stub_request(:get, "http://localhost:3000/api/1/custom_paginations.json")
106+
.to_return(headers: {content_type: "application/json"}, body: {
107+
custom_paginations: [
108+
{id: 1, name: "Jeff Ching", email_address: "ching.jeff@gmail.com"},
109+
{id: 2, name: "Barry Bonds", email_address: "barry@bonds.com"},
110+
{id: 3, name: "Hank Aaron", email_address: "hank@aaron.com"}
111+
],
112+
meta: {
113+
per: 3,
114+
page: 2,
115+
total: 10
116+
}
117+
}.to_json)
118+
119+
users = CustomPagination.all
120+
assert_equal 3, users.limit_value
121+
assert_equal 1, users.previous_page
122+
assert_equal 3, users.next_page
123+
assert_equal 2, users.current_page
124+
assert_equal 10, users.total_entries
125+
assert_equal 3, users.per_page
126+
end
127+
74128
end

0 commit comments

Comments
 (0)
Failed to load comments.