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



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rom/sql/plugin/pagination.rb', line 47

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:



69
70
71
72
# File 'lib/rom/sql/plugin/pagination.rb', line 69

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)


80
81
82
83
# File 'lib/rom/sql/plugin/pagination.rb', line 80

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