module Upfluence::Mixin::Pagination

Constants

DEFAULT_PAGE
DEFAULT_PER_PAGE
MAX_PER_PAGE

Public Instance Methods

guess_per_page() click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 37
def guess_per_page
  return params[:per_page].to_i if params[:per_page].present?

  return default_per_page if methods.include?(:default_per_page)

  DEFAULT_PER_PAGE
end
page() click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 26
def page
  [params[:page].to_i, DEFAULT_PAGE].max
end
paginated_entities() click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 12
def paginated_entities
  @paginated_entities ||= paginated_model.limit(per_page).offset(
    (page - 1) * per_page
  ).to_a
end
paginated_entities=(v) click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 18
def paginated_entities=(v)
  @paginated_entities = v
end
paginated_model() click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 8
def paginated_model
  raise NotImplementedError
end
paginated_total() click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 22
def paginated_total
  paginated_model.count
end
per_page() click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 30
def per_page
  [
    [0, guess_per_page].max,
    MAX_PER_PAGE
  ].min
end
respond_with_pagination(args = {}) click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 51
def respond_with_pagination(args = {})
  respond_with(
    args[:payload] || paginated_entities,
    args.merge(
      meta: {
        total: paginated_total,
        total_pages: total_pages,
        per_page: per_page
      }
    ) { |_, x, y| x.merge(y) }
  )
end
total_pages() click to toggle source
# File lib/upfluence/mixin/pagination.rb, line 45
def total_pages
  return paginated_total if per_page <= 1

  (paginated_total.to_f / per_page.to_f).ceil
end