Module: Undercarriage::Controllers::KaminariConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/undercarriage/controllers/kaminari_concern.rb

Overview

Kaminari pagination

Helpers for Kaminari style pagination. Note that the Kaminari gem is not loaded with dependency. It must be added to your own Gemfile

Examples:

Controller

class ExamplesController < ApplicationController
  include Undercarriage::Controllers::KaminariConcern

  def index
    @examples = Examples.page(page_num).per(per_page)
  end
end

Instance Method Summary collapse

Instance Method Details

#page_numInteger

Page number

Will look for the Kaminari config param_name (typically page) in the URL paramaters. The result is clamped to a minimum of 1 so a caller cannot force a negative or zero page number.

This is asseccible from the View as page_num

Examples:

Request

# GET /examples?page=5 # Return page 5 of items
# GET /examples?per=10&page=3 # Return page 3 of items with 10 items per page

Returns:

  • (Integer)

    the page number



59
60
61
# File 'lib/undercarriage/controllers/kaminari_concern.rb', line 59

def page_num
  params.fetch(page_num_key, page_num_default).to_i.clamp(1, nil)
end

#per_pageInteger

Items per page

The number of items to return in pagination. Will use the Kaminari config default_per_page (typically 25) for the count and will look for per in the URL paramaters to override. The result is clamped between 1 and #per_page_max so a caller cannot force an unbounded (or negative/zero) number of records per page.

This is asseccible from the View as per_page

Examples:

Request

# GET /examples?per=100 # Return 100 items per page
# GET /examples?per=10&page=3 # Return page 3 of items with 10 items per page
# GET /examples?per=999999999 # Clamped down to per_page_max

Returns:

  • (Integer)

    the number of items per page



42
43
44
# File 'lib/undercarriage/controllers/kaminari_concern.rb', line 42

def per_page
  params.fetch(per_page_key, per_page_default).to_i.clamp(1, per_page_max)
end