Module: Praxis::Extensions::Pagination

Extended by:
ActiveSupport::Concern
Defined in:
lib/praxis.rb,
lib/praxis/extensions/pagination.rb,
lib/praxis/extensions/pagination/ordering_params.rb,
lib/praxis/extensions/pagination/header_generator.rb,
lib/praxis/extensions/pagination/pagination_params.rb,
lib/praxis/extensions/pagination/pagination_handler.rb,
lib/praxis/extensions/pagination/sequel_pagination_handler.rb,
lib/praxis/extensions/pagination/active_record_pagination_handler.rb

Defined Under Namespace

Classes: ActiveRecordPaginationHandler, HeaderGenerator, OrderingParams, PaginationHandler, PaginationParams, PaginationStruct, SequelPaginationHandler

Instance Method Summary collapse

Instance Method Details

#_paginationObject

Will set the typed paginator and order object into a controller ivar This is lazily evaluated and memoized, so there’s no need to only calculate things for actions that paginate/sort



61
62
63
64
65
66
67
68
69
70
# File 'lib/praxis/extensions/pagination.rb', line 61

def _pagination
  return @_pagination if @_pagination

  pagination = {}
  attrs = request.action&.params&.type&.attributes
  pagination[:paginator] = request.params.pagination if attrs&.key? :pagination
  pagination[:order] = request.params.order if attrs&.key? :order

  @_pagination = PaginationStruct.new(pagination[:paginator], pagination[:order])
end

#build_pagination_headers(pagination:, current_url:, current_query_params:) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/praxis/extensions/pagination.rb', line 72

def build_pagination_headers(pagination:, current_url:, current_query_params:)
  links = if pagination.paginator.by
            # We're assuming that the last element has a "symbol/string" field with the same name of the "by" pagination.
            last_element = response.body.last
            last_value = last_element[pagination.paginator.by.to_sym] || last_element[pagination.paginator.by] if last_element
            HeaderGenerator.build_cursor_headers(
              paginator: pagination.paginator,
              last_value: last_value,
              total_count: pagination.total_count
            )
          else
            HeaderGenerator.build_paging_headers(
              paginator: pagination.paginator,
              total_count: pagination.total_count
            )
          end

  HeaderGenerator.generate_headers(
    links: links,
    current_url: current_url,
    current_query_params: current_query_params,
    total_count: pagination.total_count
  )
end