Module: ApiCursorPagination::Concern
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/api_cursor_pagination/concern.rb
Instance Attribute Summary collapse
- #next_page_cursor_id ⇒ Object
- #page_after ⇒ Object
- #page_before ⇒ Object
- #page_size ⇒ Object
- #prev_page_cursor_id ⇒ Object
- #total_pages ⇒ Object
- #total_size ⇒ Object
Instance Method Summary collapse
- #page_links_and_meta_data(base_url, query_params) ⇒ Object
- #paginate(scope, scope_id_str, row_id_str = scope_id_str) ⇒ Object
- #validate_and_setup_page_params(page_params) ⇒ Object
Instance Attribute Details
#next_page_cursor_id ⇒ Object
43 44 45 |
# File 'lib/api_cursor_pagination/concern.rb', line 43 def next_page_cursor_id @next_page_cursor_id end |
#page_after ⇒ Object
43 44 45 |
# File 'lib/api_cursor_pagination/concern.rb', line 43 def page_after @page_after end |
#page_before ⇒ Object
43 44 45 |
# File 'lib/api_cursor_pagination/concern.rb', line 43 def page_before @page_before end |
#page_size ⇒ Object
43 44 45 |
# File 'lib/api_cursor_pagination/concern.rb', line 43 def page_size @page_size end |
#prev_page_cursor_id ⇒ Object
43 44 45 |
# File 'lib/api_cursor_pagination/concern.rb', line 43 def prev_page_cursor_id @prev_page_cursor_id end |
#total_pages ⇒ Object
43 44 45 |
# File 'lib/api_cursor_pagination/concern.rb', line 43 def total_pages @total_pages end |
#total_size ⇒ Object
43 44 45 |
# File 'lib/api_cursor_pagination/concern.rb', line 43 def total_size @total_size end |
Instance Method Details
#page_links_and_meta_data(base_url, query_params) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/api_cursor_pagination/concern.rb', line 118 def (base_url, query_params) if page_size > 0 cursor = {} cursor[:before] = prev_page_cursor_id if prev_page_cursor_id.present? cursor[:after] = next_page_cursor_id if next_page_cursor_id.present? = { meta: { page: { cursor: cursor, total: total_size, pages: total_pages } } } [:links] = {} if prev_page_cursor_id.present? query_string = query_params.merge(page: { before: prev_page_cursor_id, size: page_size }).to_query [:links].merge!(prev: "#{base_url}?#{query_string}") end if next_page_cursor_id.present? query_string = query_params.merge(page: { after: next_page_cursor_id, size: page_size }).to_query [:links].merge!(next: "#{base_url}?#{query_string}") end else {} end end |
#paginate(scope, scope_id_str, row_id_str = scope_id_str) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/api_cursor_pagination/concern.rb', line 94 def paginate(scope, scope_id_str, row_id_str = scope_id_str) if page_size > 0 self.total_size = scope.size self.total_pages = total_size / page_size self.total_pages += 1 if total_size % page_size > 0 scope = scope.having("#{scope_id_str} > ?", page_after) if page_after.present? scope = scope.having("#{scope_id_str} < ?", page_before) if page_before.present? scope = scope.limit(page_size) if page_before.present? && page_after.blank? scope = scope.order("#{scope_id_str} desc") rows = scope.to_a.sort_by{|r| r.send(row_id_str)} else rows = scope.order(scope_id_str).to_a end self.next_page_cursor_id = rows.last&.send(row_id_str) self.prev_page_cursor_id = rows.first&.send(row_id_str) rows else scope.to_a end end |
#validate_and_setup_page_params(page_params) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 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 |
# File 'lib/api_cursor_pagination/concern.rb', line 47 def validate_and_setup_page_params(page_params) @errors ||= [] self.page_size = 0 return if page_params.blank? self.page_size = page_params[:size].to_i if page_size < 1 @errors << { title: 'Invalid Parameter.', detail: "page[size] is required and must be a positive integer; got #{page_params[:size]}", source: { parameter: "page[size]" } } elsif page_params.has_key?(:sort) @errors << { title: 'Unsupported Sort.', detail: "page[sort] is not supported; got page[sort]=#{page_params[:sort]}", source: { parameter: "page[sort]" }, links: { type: ["https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort"] } } elsif page_params.has_key?(:before) && page_params.has_key?(:after) @errors << { title: "Range Pagination Not Supported.", detail: "Range pagination not supported; got page[before]=#{page_params[:before]} and page[after]=#{page_params[:after]}", links: { type: ["https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported"] } } elsif page_params.has_key?(:before) self.page_before = page_params[:before] if self.page_before.blank? @errors << { title: 'Invalid Parameter.', detail: 'page[before] is invalid', source: { parameter: 'page[before]' } } end elsif page_params.has_key?(:after) self.page_after = page_params[:after] if self.page_after.blank? @errors << { title: 'Invalid Parameter.', detail: 'page[after] is invalid', source: { parameter: 'page[after]' } } end end end |