Sha256: e60d5a8ce6457b1d053cec7f7fc727ea5afe0b390864e0d35295d15498d9e12c

Contents?: true

Size: 1013 Bytes

Versions: 8

Compression:

Stored size: 1013 Bytes

Contents

module BazaModels::Query::Pagination
  def current_page
    if @page
      @page
    else
      raise "Page has not been set"
    end
  end

  def out_of_bounds?
    current_page > total_pages
  end

  def page(some_page = :non_given)
    if some_page == :non_given
      @page ||= 1
    else
      some_page ||= 1
      some_page = some_page.to_i
      offset = (some_page - 1) * per

      clone(page: some_page, offset: offset, limit: per)
    end
  end

  def paginated?
    @page != nil
  end

  def per(value = :non_given)
    if value == :non_given
      @per ||= 30
    else
      value = value.to_i
      offset = (page - 1) * value
      clone(limit: value, offset: offset, per: value)
    end
  end

  alias per_page per

  def total_entries
    @model.count
  end

  def total_pages
    pages_count = (count.to_f / per.to_f)

    pages_count = 1 if pages_count.nan? || pages_count == Float::INFINITY
    pages_count = pages_count.ceil
    pages_count = 1 if pages_count == 0
    pages_count
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
baza_models-0.0.13 lib/baza_models/query/pagination.rb
baza_models-0.0.12 lib/baza_models/query/pagination.rb
baza_models-0.0.11 lib/baza_models/query/pagination.rb
baza_models-0.0.10 lib/baza_models/query/pagination.rb
baza_models-0.0.9 lib/baza_models/query/pagination.rb
baza_models-0.0.8 lib/baza_models/query/pagination.rb
baza_models-0.0.7 lib/baza_models/query/pagination.rb
baza_models-0.0.6 lib/baza_models/query/pagination.rb