Module: Pragma::Decorator::Pagination

Defined in:
lib/pragma/decorator/pagination.rb,
lib/pragma/decorator/pagination/adapter.rb,
lib/pragma/decorator/pagination/adapter/base.rb,
lib/pragma/decorator/pagination/adapter/kaminari.rb,
lib/pragma/decorator/pagination/adapter/will_paginate.rb

Overview

Pagination provides support for including pagination metadata in your collection.

It is particularly useful when used in conjunction with Collection.

It supports both Kaminari and will_paginate.

Examples:

Including pagination metadata

class ArticlesDecorator < Pragma::Decorator::Base
  include Pragma::Decorator::Collection
  include Pragma::Decorator::Pagination
end

# {
#   "data": [
#     { "...": "..." },
#     { "...": "..." },
#     { "...": "..." }
#   ],
#   "total_entries": 150,
#   "per_page": 30,
#   "total_pages": 5,
#   "previous_page": 2,
#   "current_page": 3,
#   "next_page": 4
# }
ArticlesDecorator.new(Article.all).to_hash

Defined Under Namespace

Modules: Adapter, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pragma/decorator/pagination.rb', line 95

def self.included(klass)
  klass.include InstanceMethods

  klass.class_eval do
    property :total_entries, exec_context: :decorator
    property :per_page, exec_context: :decorator
    property :total_pages, exec_context: :decorator
    property :previous_page, exec_context: :decorator
    property :current_page, exec_context: :decorator
    property :next_page, exec_context: :decorator
  end
end