class Locomotive::Steam::Models::Pager

Constants

DEFAULT_PER_PAGE

Attributes

collection[R]
current_page[R]
per_page[R]
total_entries[R]
total_pages[R]

Public Class Methods

new(source, page, per_page) click to toggle source
# File lib/locomotive/steam/models/pager.rb, line 10
def initialize(source, page, per_page)
  @current_page, @per_page = page || 1, per_page || DEFAULT_PER_PAGE

  @current_page   = 1 if @current_page < 1
  @total_entries  = source.count
  @total_pages    = (@total_entries.to_f / @per_page).ceil

  index   = (@current_page - 1) * @per_page
  offset  = (index + @per_page - 1) >= @total_entries ? @total_entries : (index + @per_page - 1)

  @collection = paginate(source, index, offset)
end

Public Instance Methods

next_page() click to toggle source
# File lib/locomotive/steam/models/pager.rb, line 27
def next_page
  current_page >= total_pages ? nil : current_page + 1
end
previous_page() click to toggle source
# File lib/locomotive/steam/models/pager.rb, line 23
def previous_page
  current_page <= 1 ? nil : current_page - 1
end
to_liquid() click to toggle source
# File lib/locomotive/steam/models/pager.rb, line 31
def to_liquid
  {
    collection:       collection,
    current_page:     current_page,
    per_page:         per_page,
    previous_page:    previous_page,
    next_page:        next_page,
    total_entries:    total_entries,
    total_pages:      total_pages
  }
end

Private Instance Methods

paginate(source, index, offset) click to toggle source
# File lib/locomotive/steam/models/pager.rb, line 45
def paginate(source, index, offset)
  limit = offset - index + 1
  limit = 0 if limit < 1
  source.send(:slice, index, limit) || []
end