Module: Paginatable

Extended by:
ActiveSupport::Concern
Included in:
Api::V1::JobsController, Api::V1::UsersController
Defined in:
app/controllers/concerns/paginatable.rb

Overview

Concern untuk menambahkan kemampuan pagination secara reusable di controller.

Constant Summary collapse

DEFAULT_PER_PAGE =

Jumlah default item per halaman

10
MAX_PER_PAGE =

Jumlah maksimum item per halaman untuk menghindari beban besar

100

Instance Method Summary collapse

Instance Method Details

#current_pageInteger

Mengambil nomor halaman dari query param ?page=

Returns:

  • (Integer)

    halaman saat ini (default: 1)



15
16
17
# File 'app/controllers/concerns/paginatable.rb', line 15

def current_page
  params[:page].presence || 1
end

#pagination_meta(collection) ⇒ Hash

Menghasilkan metadata pagination dari hasil query Kaminari

Parameters:

  • collection (Kaminari::PaginatableArray or ActiveRecord::Relation)

Returns:

  • (Hash)

    metadata pagination



30
31
32
33
34
35
36
37
# File 'app/controllers/concerns/paginatable.rb', line 30

def pagination_meta(collection)
  {
    current_page: collection.current_page,
    total_pages:  collection.total_pages,
    total_count:  collection.total_count,
    per_page:     collection.limit_value
  }
end

#per_pageInteger

Mengambil jumlah item per halaman dari query param ?per_page=

Returns:

  • (Integer)

    jumlah item yang akan ditampilkan per halaman (default: 10, maksimum: 100)



21
22
23
24
25
# File 'app/controllers/concerns/paginatable.rb', line 21

def per_page
  raw = params[:per_page].to_i
  raw = DEFAULT_PER_PAGE if raw <= 0
  [ raw, MAX_PER_PAGE ].min
end