Class: Praxis::Extensions::Pagination::HeaderGenerator
- Inherits:
-
Object
- Object
- Praxis::Extensions::Pagination::HeaderGenerator
- Defined in:
- lib/praxis/extensions/pagination/header_generator.rb
Class Method Summary collapse
- .build_cursor_headers(paginator:, last_value:, total_count: nil) ⇒ Object
-
.build_paging_headers(paginator:, total_count: nil) ⇒ Object
This is only for plain paging.
- .generate_headers(links:, current_url:, current_query_params:, total_count:) ⇒ Object
Class Method Details
.build_cursor_headers(paginator:, last_value:, total_count: nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/praxis/extensions/pagination/header_generator.rb', line 7 def self.build_cursor_headers(paginator:, last_value:, total_count: nil) %i[next prev first last].each_with_object({}) do |rel_name, info| case rel_name when :next # If we don't know the total, we'll try to go to the next page # but assume we're done if there isn't a last value... if last_value info[:next] = { by: paginator.by, from: last_value, items: paginator.items } info[:next][:total_count] = true if total_count.present? end when :prev # Not possible to go back when :first info[:first] = { by: paginator.by, items: paginator.items } info[:first][:total_count] = true if total_count.present? when :last # Not possible to scroll to last end end end |
.build_paging_headers(paginator:, total_count: nil) ⇒ Object
This is only for plain paging
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/praxis/extensions/pagination/header_generator.rb', line 29 def self.build_paging_headers(paginator:, total_count: nil) last_page = total_count.nil? ? nil : (total_count / (paginator.items * 1.0)).ceil %i[next prev first last].each_with_object({}) do |rel_name, info| num = case rel_name when :first 1 when :prev next if paginator.page < 2 paginator.page - 1 when :next # don't include this link if we know the total and we see there are no more pages next if last_page.present? && (paginator.page >= last_page) # if we don't know the total, we'll specify to the next page even if it ends up being blank paginator.page + 1 when :last next if last_page.blank? last_page end info[rel_name] = { page: num, items: paginator.items, total_count: total_count.present? } end end |
.generate_headers(links:, current_url:, current_query_params:, total_count:) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/praxis/extensions/pagination/header_generator.rb', line 58 def self.generate_headers(links:, current_url:, current_query_params:, total_count:) mapped = links.map do |(rel, info)| # Make sure to encode it our way (with comma-separated args, as it is our own syntax, and not a query string one) pagination_param = info.map { |(k, v)| "#{k}=#{v}" }.join(',') new_url = "#{current_url}?#{current_query_params.dup.merge(pagination: pagination_param).to_query}" LinkHeader::Link.new(new_url, [['rel', rel.to_s]]) end link_header = LinkHeader.new(mapped) headers = { 'Link' => link_header.to_s } headers['Total-Count'] = total_count if total_count headers end |