@@ -15,7 +15,9 @@ def setup
15
15
@array = [
16
16
Profile . new ( { id : 1 , name : 'Name 1' , description : 'Description 1' , comments : 'Comments 1' } ) ,
17
17
Profile . new ( { id : 2 , name : 'Name 2' , description : 'Description 2' , comments : 'Comments 2' } ) ,
18
- Profile . new ( { id : 3 , name : 'Name 3' , description : 'Description 3' , comments : 'Comments 3' } )
18
+ Profile . new ( { id : 3 , name : 'Name 3' , description : 'Description 3' , comments : 'Comments 3' } ) ,
19
+ Profile . new ( { id : 4 , name : 'Name 4' , description : 'Description 4' , comments : 'Comments 4' } ) ,
20
+ Profile . new ( { id : 5 , name : 'Name 5' , description : 'Description 5' , comments : 'Comments 5' } )
19
21
]
20
22
end
21
23
@@ -32,31 +34,43 @@ def load_adapter(paginated_collection, options = {})
32
34
ActiveModel ::SerializableResource . new ( paginated_collection , options )
33
35
end
34
36
35
- def using_kaminari
36
- Kaminari . paginate_array ( @array ) . page ( 2 ) . per ( 1 )
37
+ def using_kaminari ( page = 2 )
38
+ Kaminari . paginate_array ( @array ) . page ( page ) . per ( 2 )
37
39
end
38
40
39
- def using_will_paginate
40
- @array . paginate ( page : 2 , per_page : 1 )
41
+ def using_will_paginate ( page = 2 )
42
+ @array . paginate ( page : page , per_page : 2 )
41
43
end
42
44
43
45
def data
44
46
{ data : [
45
47
{ id : '1' , type : 'profiles' , attributes : { name : 'Name 1' , description : 'Description 1' } } ,
46
48
{ id : '2' , type : 'profiles' , attributes : { name : 'Name 2' , description : 'Description 2' } } ,
47
- { id : '3' , type : 'profiles' , attributes : { name : 'Name 3' , description : 'Description 3' } }
49
+ { id : '3' , type : 'profiles' , attributes : { name : 'Name 3' , description : 'Description 3' } } ,
50
+ { id : '4' , type : 'profiles' , attributes : { name : 'Name 4' , description : 'Description 4' } } ,
51
+ { id : '5' , type : 'profiles' , attributes : { name : 'Name 5' , description : 'Description 5' } }
48
52
]
49
53
}
50
54
end
51
55
52
56
def links
53
57
{
54
58
links : {
55
- self : "#{ URI } ?page%5Bnumber%5D=2&page%5Bsize%5D=1" ,
56
- first : "#{ URI } ?page%5Bnumber%5D=1&page%5Bsize%5D=1" ,
57
- prev : "#{ URI } ?page%5Bnumber%5D=1&page%5Bsize%5D=1" ,
58
- next : "#{ URI } ?page%5Bnumber%5D=3&page%5Bsize%5D=1" ,
59
- last : "#{ URI } ?page%5Bnumber%5D=3&page%5Bsize%5D=1"
59
+ self : "#{ URI } ?page%5Bnumber%5D=2&page%5Bsize%5D=2" ,
60
+ first : "#{ URI } ?page%5Bnumber%5D=1&page%5Bsize%5D=2" ,
61
+ prev : "#{ URI } ?page%5Bnumber%5D=1&page%5Bsize%5D=2" ,
62
+ next : "#{ URI } ?page%5Bnumber%5D=3&page%5Bsize%5D=2" ,
63
+ last : "#{ URI } ?page%5Bnumber%5D=3&page%5Bsize%5D=2"
64
+ }
65
+ }
66
+ end
67
+
68
+ def last_page_links
69
+ {
70
+ links : {
71
+ self : "#{ URI } ?page%5Bnumber%5D=3&page%5Bsize%5D=2" ,
72
+ first : "#{ URI } ?page%5Bnumber%5D=1&page%5Bsize%5D=2" ,
73
+ prev : "#{ URI } ?page%5Bnumber%5D=2&page%5Bsize%5D=2"
60
74
}
61
75
}
62
76
end
@@ -67,19 +81,26 @@ def expected_response_without_pagination_links
67
81
68
82
def expected_response_with_pagination_links
69
83
{ } . tap do |hash |
70
- hash [ :data ] = [ data . values . flatten . second ]
84
+ hash [ :data ] = data . values . flatten [ 2 .. 3 ]
71
85
hash . merge! links
72
86
end
73
87
end
74
88
75
89
def expected_response_with_pagination_links_and_additional_params
76
90
new_links = links [ :links ] . each_with_object ( { } ) { |( key , value ) , hash | hash [ key ] = "#{ value } &test=test" }
77
91
{ } . tap do |hash |
78
- hash [ :data ] = [ data . values . flatten . second ]
92
+ hash [ :data ] = data . values . flatten [ 2 .. 3 ]
79
93
hash . merge! links : new_links
80
94
end
81
95
end
82
96
97
+ def expected_response_with_last_page_pagination_links
98
+ { } . tap do |hash |
99
+ hash [ :data ] = [ data . values . flatten . last ]
100
+ hash . merge! last_page_links
101
+ end
102
+ end
103
+
83
104
def test_pagination_links_using_kaminari
84
105
adapter = load_adapter ( using_kaminari )
85
106
@@ -102,6 +123,20 @@ def test_pagination_links_with_additional_params
102
123
adapter . serializable_hash ( @options )
103
124
end
104
125
126
+ def test_last_page_pagination_links_using_kaminari
127
+ adapter = load_adapter ( using_kaminari ( 3 ) )
128
+
129
+ mock_request
130
+ assert_equal expected_response_with_last_page_pagination_links , adapter . serializable_hash ( @options )
131
+ end
132
+
133
+ def test_last_page_pagination_links_using_will_paginate
134
+ adapter = load_adapter ( using_will_paginate ( 3 ) )
135
+
136
+ mock_request
137
+ assert_equal expected_response_with_last_page_pagination_links , adapter . serializable_hash ( @options )
138
+ end
139
+
105
140
def test_not_showing_pagination_links
106
141
adapter = load_adapter ( @array )
107
142
0 commit comments