lib/jsonapi/paginator.rb in jsonapi-resources-0.3.3 vs lib/jsonapi/paginator.rb in jsonapi-resources-0.4.0

- old
+ new

@@ -1,11 +1,11 @@ module JSONAPI class Paginator def initialize(params) end - def apply(relation) + def apply(relation, order_options) # relation end class << self def paginator_for(paginator) @@ -17,13 +17,14 @@ end class OffsetPaginator < JSONAPI::Paginator def initialize(params) parse_pagination_params(params) + verify_pagination_params end - def apply(relation) + def apply(relation, order_options) relation.offset(@offset).limit(@limit) end private def parse_pagination_params(params) @@ -33,31 +34,38 @@ elsif params.is_a?(ActionController::Parameters) validparams = params.permit(:offset, :limit) @offset = validparams[:offset] ? validparams[:offset].to_i : 0 @limit = validparams[:limit] ? validparams[:limit].to_i : JSONAPI.configuration.default_page_size - - if @limit < 1 - raise JSONAPI::Exceptions::InvalidPageValue.new(:limit, validparams[:limit]) - elsif @limit > JSONAPI.configuration.maximum_page_size - raise JSONAPI::Exceptions::InvalidPageValue.new(:limit, validparams[:limit], - "Limit exceeds maximum page size of #{JSONAPI.configuration.maximum_page_size}.") - end else raise JSONAPI::Exceptions::InvalidPageObject.new end rescue ActionController::UnpermittedParameters => e raise JSONAPI::Exceptions::PageParametersNotAllowed.new(e.params) end + + def verify_pagination_params + if @limit < 1 + raise JSONAPI::Exceptions::InvalidPageValue.new(:limit, @limit) + elsif @limit > JSONAPI.configuration.maximum_page_size + raise JSONAPI::Exceptions::InvalidPageValue.new(:limit, @limit, + "Limit exceeds maximum page size of #{JSONAPI.configuration.maximum_page_size}.") + end + + if @offset < 0 + raise JSONAPI::Exceptions::InvalidPageValue.new(:offset, @offset) + end + end end class PagedPaginator < JSONAPI::Paginator def initialize(params) parse_pagination_params(params) + verify_pagination_params end - def apply(relation) + def apply(relation, order_options) offset = (@number - 1) * @size relation.offset(offset).limit(@size) end private @@ -68,20 +76,26 @@ elsif params.is_a?(ActionController::Parameters) validparams = params.permit(:number, :size) @size = validparams[:size] ? validparams[:size].to_i : JSONAPI.configuration.default_page_size @number = validparams[:number] ? validparams[:number].to_i : 1 - - if @size < 1 - raise JSONAPI::Exceptions::InvalidPageValue.new(:size, validparams[:size]) - elsif @size > JSONAPI.configuration.maximum_page_size - raise JSONAPI::Exceptions::InvalidPageValue.new(:size, validparams[:size], - "size exceeds maximum page size of #{JSONAPI.configuration.maximum_page_size}.") - end else @size = JSONAPI.configuration.default_page_size @number = params.to_i end rescue ActionController::UnpermittedParameters => e raise JSONAPI::Exceptions::PageParametersNotAllowed.new(e.params) + end + + def verify_pagination_params + if @size < 1 + raise JSONAPI::Exceptions::InvalidPageValue.new(:size, @size) + elsif @size > JSONAPI.configuration.maximum_page_size + raise JSONAPI::Exceptions::InvalidPageValue.new(:size, @size, + "size exceeds maximum page size of #{JSONAPI.configuration.maximum_page_size}.") + end + + if @number < 1 + raise JSONAPI::Exceptions::InvalidPageValue.new(:number, @number) + end end end