Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ from the configuration settings is used.

###### Custom Paginators

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

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

Expand All @@ -440,7 +440,7 @@ class SingleRecordPaginator < JSONAPI::Paginator
@page = params.to_i
end

def apply(relation)
def apply(relation, order_options)
relation.offset(@page).limit(1)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/jsonapi/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Paginator
def initialize(params)
end

def apply(relation)
def apply(relation, order_options)
# relation
end

Expand All @@ -22,7 +22,7 @@ def initialize(params)
verify_pagination_params
end

def apply(relation)
def apply(relation, order_options)
relation.offset(@offset).limit(@limit)
end

Expand Down Expand Up @@ -63,7 +63,7 @@ def initialize(params)
verify_pagination_params
end

def apply(relation)
def apply(relation, order_options)
offset = (@number - 1) * @size
relation.offset(offset).limit(@size)
end
Expand Down
25 changes: 17 additions & 8 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ def apply_includes(records, directives)
records
end

def apply_pagination(records, paginator)
def apply_pagination(records, paginator, order_options)
if paginator
records = paginator.apply(records)
records = paginator.apply(records, order_options)
end
records
end
Expand Down Expand Up @@ -354,13 +354,15 @@ def apply_filters(records, filters)
end

def filter_records(filters, options)
sort_criteria = options.fetch(:sort_criteria) { [] }
include_directives = options[:include_directives]

records = records(options)
records = apply_includes(records, include_directives)
records = apply_filters(records, filters)
apply_sort(records, construct_order_options(sort_criteria))
apply_filters(records, filters)
end

def sort_records(records, order_options)
apply_sort(records, order_options)
end

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

records = filter_records(filters, options)
records = apply_pagination(records, options[:paginator])

sort_criteria = options.fetch(:sort_criteria) { [] }
order_options = construct_order_options(sort_criteria)
records = sort_records(records, order_options)

records = apply_pagination(records, options[:paginator], order_options)

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

resources = []

if resource_class
records = public_send(associated_records_method_name)
records = resource_class.apply_filters(records, filters)
records = resource_class.apply_sort(records, self.class.construct_order_options(sort_criteria))
records = resource_class.apply_pagination(records, paginator)
order_options = self.class.construct_order_options(sort_criteria)
records = resource_class.apply_sort(records, order_options)
records = resource_class.apply_pagination(records, paginator, order_options)
records.each do |record|
resources.push resource_class.new(record, @context)
end
Expand Down
6 changes: 3 additions & 3 deletions test/unit/resource/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_has_many_association_pagination

# define apply_filters method on post resource to not respect filters
PostResource.instance_eval do
def apply_pagination(records, criteria)
def apply_pagination(records, criteria, order_options)
records
end
end
Expand All @@ -200,7 +200,7 @@ def initialize(params)
@page = params.to_i
end

def apply(relation)
def apply(relation, order_options)
relation.offset(@page).limit(1)
end
end
Expand All @@ -210,7 +210,7 @@ def apply(relation)

# reset method to original implementation
PostResource.instance_eval do
def apply_pagination(records, criteria)
def apply_pagination(records, criteria, order_options)
super
end
end
Expand Down