Module: WCC::JsonAPI::PaginationHelper
- Defined in:
- lib/wcc/jsonapi/concerns/pagination_helper.rb
Instance Method Summary collapse
- #index_meta(total) ⇒ Object
- #index_serializer_options(total) ⇒ Object
- #limit ⇒ Object
-
#pagination_links(total) ⇒ Object
Gets the pagination links for the current skip and limit.
- #skip ⇒ Object
Instance Method Details
#index_meta(total) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 15 def (total) { total: total, limit: limit, skip: skip, } end |
#index_serializer_options(total) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 42 def (total) { is_collection: true, meta: (total), links: pagination_links(total), }.merge() end |
#limit ⇒ Object
4 5 6 7 8 9 |
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 4 def limit @limit ||= [ params[:limit]&.to_i || 100, 100, ].min end |
#pagination_links(total) ⇒ Object
Gets the pagination links for the current skip and limit. This method is inherently complex due to the math involved. Better to encapsulate the complexity here.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 26 def pagination_links(total) prev_skip = skip == 0 ? nil : [skip - limit, 0].max next_skip = (skip + limit) > total ? nil : skip + limit last_skip = (total / limit).floor * limit = permitted_params.merge(only_path: true) { self: url_for(.merge(limit: limit, skip: skip)), first: url_for(.merge(limit: limit, skip: 0)), prev: prev_skip && url_for(.merge(limit: limit, skip: prev_skip)), next: next_skip && url_for(.merge(limit: limit, skip: next_skip)), last: url_for(.merge(limit: limit, skip: last_skip)), } end |
#skip ⇒ Object
11 12 13 |
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 11 def skip @skip ||= params[:skip]&.to_i || 0 end |