Skip to content

Commit 7335d83

Browse files
committed
Merge pull request #225 from cerebris/214
Paginator Order Context
2 parents a4795a5 + 5ad7b32 commit 7335d83

4 files changed

Lines changed: 27 additions & 18 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,9 @@ from the configuration settings is used.
427427

428428
###### Custom Paginators
429429

430-
Custom `paginators` can be used. These should derive from `Paginator`. The `apply` method takes a `relation` and is
431-
expected to return a `relation`. The `initialize` method receives the parameters from the `page` request parameters. It
432-
is up to the paginator author to parse and validate these parameters.
430+
Custom `paginators` can be used. These should derive from `Paginator`. The `apply` method takes a `relation` and
431+
`order_options` and is expected to return a `relation`. The `initialize` method receives the parameters from the `page`
432+
request parameters. It is up to the paginator author to parse and validate these parameters.
433433

434434
For example, here is a very simple single record at a time paginator:
435435

@@ -440,7 +440,7 @@ class SingleRecordPaginator < JSONAPI::Paginator
440440
@page = params.to_i
441441
end
442442

443-
def apply(relation)
443+
def apply(relation, order_options)
444444
relation.offset(@page).limit(1)
445445
end
446446
end

lib/jsonapi/paginator.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Paginator
33
def initialize(params)
44
end
55

6-
def apply(relation)
6+
def apply(relation, order_options)
77
# relation
88
end
99

@@ -22,7 +22,7 @@ def initialize(params)
2222
verify_pagination_params
2323
end
2424

25-
def apply(relation)
25+
def apply(relation, order_options)
2626
relation.offset(@offset).limit(@limit)
2727
end
2828

@@ -63,7 +63,7 @@ def initialize(params)
6363
verify_pagination_params
6464
end
6565

66-
def apply(relation)
66+
def apply(relation, order_options)
6767
offset = (@number - 1) * @size
6868
relation.offset(offset).limit(@size)
6969
end

lib/jsonapi/resource.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ def apply_includes(records, directives)
311311
records
312312
end
313313

314-
def apply_pagination(records, paginator)
314+
def apply_pagination(records, paginator, order_options)
315315
if paginator
316-
records = paginator.apply(records)
316+
records = paginator.apply(records, order_options)
317317
end
318318
records
319319
end
@@ -354,13 +354,15 @@ def apply_filters(records, filters)
354354
end
355355

356356
def filter_records(filters, options)
357-
sort_criteria = options.fetch(:sort_criteria) { [] }
358357
include_directives = options[:include_directives]
359358

360359
records = records(options)
361360
records = apply_includes(records, include_directives)
362-
records = apply_filters(records, filters)
363-
apply_sort(records, construct_order_options(sort_criteria))
361+
apply_filters(records, filters)
362+
end
363+
364+
def sort_records(records, order_options)
365+
apply_sort(records, order_options)
364366
end
365367

366368
def find_count(filters, options = {})
@@ -372,7 +374,12 @@ def find(filters, options = {})
372374
context = options[:context]
373375

374376
records = filter_records(filters, options)
375-
records = apply_pagination(records, options[:paginator])
377+
378+
sort_criteria = options.fetch(:sort_criteria) { [] }
379+
order_options = construct_order_options(sort_criteria)
380+
records = sort_records(records, order_options)
381+
382+
records = apply_pagination(records, options[:paginator], order_options)
376383

377384
resources = []
378385
records.each do |model|
@@ -587,11 +594,13 @@ def _associate(klass, *attrs)
587594
paginator = options[:paginator]
588595

589596
resources = []
597+
590598
if resource_class
591599
records = public_send(associated_records_method_name)
592600
records = resource_class.apply_filters(records, filters)
593-
records = resource_class.apply_sort(records, self.class.construct_order_options(sort_criteria))
594-
records = resource_class.apply_pagination(records, paginator)
601+
order_options = self.class.construct_order_options(sort_criteria)
602+
records = resource_class.apply_sort(records, order_options)
603+
records = resource_class.apply_pagination(records, paginator, order_options)
595604
records.each do |record|
596605
resources.push resource_class.new(record, @context)
597606
end

test/unit/resource/resource_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def test_has_many_association_pagination
189189

190190
# define apply_filters method on post resource to not respect filters
191191
PostResource.instance_eval do
192-
def apply_pagination(records, criteria)
192+
def apply_pagination(records, criteria, order_options)
193193
records
194194
end
195195
end
@@ -200,7 +200,7 @@ def initialize(params)
200200
@page = params.to_i
201201
end
202202

203-
def apply(relation)
203+
def apply(relation, order_options)
204204
relation.offset(@page).limit(1)
205205
end
206206
end
@@ -210,7 +210,7 @@ def apply(relation)
210210

211211
# reset method to original implementation
212212
PostResource.instance_eval do
213-
def apply_pagination(records, criteria)
213+
def apply_pagination(records, criteria, order_options)
214214
super
215215
end
216216
end

0 commit comments

Comments
 (0)