Module: ROM::SQL::Plugin::Pagination

Defined in:
lib/rom/sql/plugin/pagination.rb

Defined Under Namespace

Classes: Pager

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rom/sql/plugin/pagination.rb', line 50

def self.included(klass)
  super

  klass.class_eval do
    defines :per_page

    option :pager, reader: true, default: proc { |relation|
      Pager.new(relation.dataset, per_page: relation.class.per_page)
    }
  end
end

Instance Method Details

#page(num) ⇒ Relation

Paginate a relation

Examples:

rom.relation(:users).class.per_page(10)
rom.relation(:users).page(1)
rom.relation(:users).pager # => info about pagination

Returns:



72
73
74
75
# File 'lib/rom/sql/plugin/pagination.rb', line 72

def page(num)
  next_pager = pager.at(dataset, num)
  __new__(next_pager.dataset, pager: next_pager)
end

#per_page(num) ⇒ Object

Set limit for pagination

Examples:

rom.relation(:users).page(2).per_page(10)


83
84
85
86
# File 'lib/rom/sql/plugin/pagination.rb', line 83

def per_page(num)
  next_pager = pager.at(dataset, pager.current_page, num)
  __new__(next_pager.dataset, pager: next_pager)
end