Class: Might::PaginationMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/might/pagination_middleware.rb

Overview

Pagination middleware

Instance Method Summary collapse

Constructor Details

#initialize(app, max_per_page: false, per_page: 50, paginator_class: Paginator) ⇒ PaginationMiddleware

Returns a new instance of PaginationMiddleware.

Parameters:

  • app (#call, Proc)
  • max_per_page (Integer) (defaults to: false)
  • per_page (Integer) (defaults to: 50)


10
11
12
13
14
15
# File 'lib/might/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

Instance Method Details

#call(env) ⇒ Object

First argument is a ActiveRecord relation which must be paginated Second argument is a request parameters provided by user

Parameters:

  • env (Array(ActiveRecord::Relation, Hash))


21
22
23
24
25
26
# File 'lib/might/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