class Right::PaginationMiddleware

Pagination middleware

Attributes

app[R]

Public Class Methods

new(app, max_per_page: false, per_page: 50, paginator_class: Paginator) click to toggle source

@param app [#call, Proc] @param max_per_page [Integer] @param per_page [Integer]

# File lib/right/pagination_middleware.rb, line 10
def initialize(app, max_per_page: false, per_page: 50, paginator_class: Paginator)
  @app = app
  @max_per_page = max_per_page
  @per_page = per_page
  @paginator_class = paginator_class
end

Public Instance Methods

call(env) click to toggle source

@param [Array(ActiveRecord::Relation, Hash)] env First argument is a ActiveRecord relation which must be paginated Second argument is a request parameters provided by user

# File lib/right/pagination_middleware.rb, line 21
def call(env)
  scope, params = env

  paginated_scope = @paginator_class.new(pagination_options(params)).paginate(scope)
  app.call([paginated_scope, params])
end

Private Instance Methods

default_pagination_options() click to toggle source
# File lib/right/pagination_middleware.rb, line 47
def default_pagination_options
  {
    limit: @per_page,
    offset: 0,
  }.with_indifferent_access
end
pagination_options(params) click to toggle source

@param [Hash] params @option params [Hash] (nil) :limit @option params [Hash] (nil) :offset

# File lib/right/pagination_middleware.rb, line 36
def pagination_options(params)
  options = default_pagination_options.merge(Hash(params[:page]))
  max_per_page = @max_per_page

  if max_per_page && options[:limit] > max_per_page
    options.merge(limit: max_per_page)
  else
    options
  end
end