class RKit::Pagination::Base

Attributes

page[RW]
per_page[RW]

Public Class Methods

new(collection, **options) click to toggle source

TODO: should raise (todo custom error) an error if has “limit” or “offset” values

Calls superclass method Object::new
# File lib/r_kit/pagination/base.rb, line 6
def initialize collection, **options
  raise if collection.values.keys.include_one? [:limit, :offset]

  super collection

  @page = options[:page] || 1
  @per_page = options[:per_page] || RKit::Pagination.config.per_page[collection.klass]
end

Public Instance Methods

each(&block) click to toggle source
# File lib/r_kit/pagination/base.rb, line 43
def each &block
  limited_collection.each(&block)
end
limit() click to toggle source

TODO: limit & offset should raise a custom made error

# File lib/r_kit/pagination/base.rb, line 26
def limit
  raise
end
limited_collection() click to toggle source
# File lib/r_kit/pagination/base.rb, line 35
def limited_collection
  @limited_collection ||= collection
    .limit(per_page)
    .offset((page-1) * per_page)
end
next_page() click to toggle source
# File lib/r_kit/pagination/base.rb, line 67
def next_page
  pages.find{ |page| page.page == (self.page + 1) } || pages.last
end
next_page_tag() click to toggle source
# File lib/r_kit/pagination/base.rb, line 98
def next_page_tag
  next_page.decorate.page_tag ">"
end
offset() click to toggle source
# File lib/r_kit/pagination/base.rb, line 30
def offset
  raise
end
pages() click to toggle source
# File lib/r_kit/pagination/base.rb, line 57
def pages
  (1..total_pages).map do |page|
    RKit::Pagination::Base::Page.new self, page: page
  end
end
pages_tag() click to toggle source
# File lib/r_kit/pagination/base.rb, line 94
def pages_tag
  pages.map(&:decorate).map(&:page_tag).reduce(:safe_concat)
end
paginated?() click to toggle source
# File lib/r_kit/pagination/base.rb, line 15
def paginated?() true end
pagination_tag() click to toggle source
# File lib/r_kit/pagination/base.rb, line 84
def pagination_tag
  view.content_tag :nav, class: :pagination do
    [previous_page_tag, pages_tag, next_page_tag].reduce(:safe_concat)
  end
end
previous_page() click to toggle source
# File lib/r_kit/pagination/base.rb, line 63
def previous_page
  pages.find{ |page| page.page == (self.page - 1) } || pages.first
end
previous_page_tag() click to toggle source
# File lib/r_kit/pagination/base.rb, line 90
def previous_page_tag
  previous_page.decorate.page_tag "<"
end
reverse() click to toggle source
# File lib/r_kit/pagination/base.rb, line 49
def reverse
  reversed = limited_collection.reverse
  @limited_collection.clear.concat(reversed)
  
  self
end
total_pages() click to toggle source
# File lib/r_kit/pagination/base.rb, line 20
def total_pages
  (collection.count / per_page.to_f).ceil
end