Class: OffsetPaginator
- Inherits:
-
JSONAPI::Paginator
- Object
- JSONAPI::Paginator
- OffsetPaginator
- Defined in:
- lib/jsonapi/paginator.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Class Method Summary collapse
- .requires_record_count ⇒ Object deprecated Deprecated.
Instance Method Summary collapse
- #apply(relation, _order_options) ⇒ Object
-
#initialize(params) ⇒ OffsetPaginator
constructor
A new instance of OffsetPaginator.
- #links_page_params(options = {}) ⇒ Object
- #requires_record_count ⇒ Object
Methods inherited from JSONAPI::Paginator
Constructor Details
#initialize(params) ⇒ OffsetPaginator
Returns a new instance of OffsetPaginator.
41 42 43 44 |
# File 'lib/jsonapi/paginator.rb', line 41 def initialize(params) parse_pagination_params(params) verify_pagination_params end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
39 40 41 |
# File 'lib/jsonapi/paginator.rb', line 39 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
39 40 41 |
# File 'lib/jsonapi/paginator.rb', line 39 def offset @offset end |
Class Method Details
.requires_record_count ⇒ Object
Deprecated.
47 48 49 |
# File 'lib/jsonapi/paginator.rb', line 47 def self.requires_record_count true end |
Instance Method Details
#apply(relation, _order_options) ⇒ Object
55 56 57 |
# File 'lib/jsonapi/paginator.rb', line 55 def apply(relation, ) relation.offset(@offset).limit(@limit) end |
#links_page_params(options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/jsonapi/paginator.rb', line 59 def links_page_params( = {}) record_count = [:record_count] links_page_params = {} links_page_params['first'] = { 'offset' => 0, 'limit' => @limit } if @offset > 0 previous_offset = @offset - @limit previous_offset = 0 if previous_offset < 0 links_page_params['prev'] = { 'offset' => previous_offset, 'limit' => @limit } end next_offset = @offset + @limit unless next_offset >= record_count links_page_params['next'] = { 'offset' => next_offset, 'limit' => @limit } end if record_count last_offset = record_count - @limit last_offset = 0 if last_offset < 0 links_page_params['last'] = { 'offset' => last_offset, 'limit' => @limit } end links_page_params end |
#requires_record_count ⇒ Object
51 52 53 |
# File 'lib/jsonapi/paginator.rb', line 51 def requires_record_count true end |