File tree 1 file changed +36
-0
lines changed
app/controllers/alchemy/json_api
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,42 @@ def base_page_scope
139
139
def jsonapi_serializer_class ( _resource , _is_collection )
140
140
::Alchemy ::JsonApi ::PageSerializer
141
141
end
142
+
143
+ # These overrides have to be in place until
144
+ # https://github.com/stas/jsonapi.rb/pull/91
145
+ # is merged and released
146
+ def jsonapi_paginate ( resources )
147
+ @_jsonapi_original_size = resources . size
148
+ super
149
+ end
150
+
151
+ def jsonapi_pagination_meta ( resources )
152
+ return { } unless JSONAPI ::Rails . is_collection? ( resources )
153
+
154
+ _ , limit , page = jsonapi_pagination_params
155
+
156
+ numbers = { current : page }
157
+
158
+ total = @_jsonapi_original_size
159
+
160
+ last_page = [ 1 , ( total . to_f / limit ) . ceil ] . max
161
+
162
+ if page > 1
163
+ numbers [ :first ] = 1
164
+ numbers [ :prev ] = page - 1
165
+ end
166
+
167
+ if page < last_page
168
+ numbers [ :next ] = page + 1
169
+ numbers [ :last ] = last_page
170
+ end
171
+
172
+ if total . present?
173
+ numbers [ :records ] = total
174
+ end
175
+
176
+ numbers
177
+ end
142
178
end
143
179
end
144
180
end
You can’t perform that action at this time.
0 commit comments