Class: RESTFramework::PageNumberPaginator
- Inherits:
-
Object
- Object
- RESTFramework::PageNumberPaginator
- Defined in:
- lib/rest_framework/paginators.rb
Overview
A simple paginator based on page numbers.
Instance Method Summary collapse
- #_page_query_param ⇒ Object
- #_page_size ⇒ Object
- #get_page(page_number = nil) ⇒ Object
-
#get_paginated_response(serialized_page) ⇒ Object
Wrap the serialized page with appripriate metadata.
-
#initialize(data:, controller:, **kwargs) ⇒ PageNumberPaginator
constructor
A new instance of PageNumberPaginator.
Constructor Details
#initialize(data:, controller:, **kwargs) ⇒ PageNumberPaginator
Returns a new instance of PageNumberPaginator.
7 8 9 10 11 12 13 14 15 |
# File 'lib/rest_framework/paginators.rb', line 7 def initialize(data:, controller:, **kwargs) @data = data @controller = controller @count = data.count @page_size = self._page_size @total_pages = @count / @page_size @total_pages += 1 if (@count % @page_size != 0) end |
Instance Method Details
#_page_query_param ⇒ Object
45 46 47 |
# File 'lib/rest_framework/paginators.rb', line 45 def _page_query_param return @controller.class.page_query_param&.to_sym end |
#_page_size ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rest_framework/paginators.rb', line 17 def _page_size page_size = nil # Get from context, if allowed. if @controller.class.page_size_query_param page_size = @controller.params[@controller.class.page_size_query_param].presence if page_size page_size = page_size.to_i end end # Otherwise, get from config. if !page_size && @controller.class.page_size page_size = @controller.class.page_size end # Fallback to a page size of 15. page_size = 15 unless page_size # Ensure we don't exceed the max page size. if @controller.class.max_page_size && page_size > @controller.class.max_page_size page_size = @controller.class.max_page_size end # Ensure we return at least 1. return page_size.zero? ? 1 : page_size end |
#get_page(page_number = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rest_framework/paginators.rb', line 49 def get_page(page_number=nil) # If page number isn't provided, infer from the params or use 1 as a fallback value. if !page_number page_number = @controller&.params&.[](self._page_query_param) if page_number.blank? page_number = 1 else page_number = page_number.to_i if page_number.zero? page_number = 1 end end end @page_number = page_number # Get the data page and return it so the caller can serialize the data in the proper format. page_index = @page_number - 1 return @data.limit(@page_size).offset(page_index * @page_size) end |
#get_paginated_response(serialized_page) ⇒ Object
Wrap the serialized page with appripriate metadata.
70 71 72 73 74 75 76 77 |
# File 'lib/rest_framework/paginators.rb', line 70 def get_paginated_response(serialized_page) return { count: @count, page: @page_number, total_pages: @total_pages, results: serialized_page, } end |