class Dimples::Pager

A class for paginating a collection of posts.

Constants

PER_PAGE

Attributes

current_page[R]
next_page[R]
page_count[R]
previous_page[R]

Public Class Methods

new(site, url, posts) click to toggle source
# File lib/dimples/pager.rb, line 16
def initialize(site, url, posts)
  @site = site
  @url = url
  @posts = posts

  @per_page = @site.config.dig(:pagination, :per_page) || PER_PAGE
  @page_prefix = @site.config.dig(:pagination, :page_prefix) || 'page_'
  @page_count = (posts.length.to_f / @per_page.to_i).ceil

  step_to(1)
end
paginate(site, url, posts, metadata = {}) click to toggle source
# File lib/dimples/pager.rb, line 12
def self.paginate(site, url, posts, metadata = {})
  new(site, url, posts).paginate(metadata)
end

Public Instance Methods

current_page_url() click to toggle source
# File lib/dimples/pager.rb, line 59
def current_page_url
  @current_page == 1 ? @url : "#{@url}#{@page_prefix}#{@current_page}"
end
first_page_url() click to toggle source
# File lib/dimples/pager.rb, line 63
def first_page_url
  @url
end
last_page_url() click to toggle source
# File lib/dimples/pager.rb, line 67
def last_page_url
  @page_count == 1 ? @url : "#{@url}#{@page_prefix}#{@page_count}"
end
metadata() click to toggle source
# File lib/dimples/pager.rb, line 91
def metadata
  {
    posts: posts_at(current_page),
    current_page: @current_page,
    page_count: @page_count,
    post_count: @posts.count,
    previous_page: @previous_page,
    next_page: @next_page,
    urls: urls
  }
end
next_page?() click to toggle source
# File lib/dimples/pager.rb, line 55
def next_page?
  @current_page + 1 <= @page_count
end
next_page_url() click to toggle source
# File lib/dimples/pager.rb, line 77
def next_page_url
  "#{@url}#{@page_prefix}#{@next_page}" if @next_page
end
paginate(metadata) click to toggle source
# File lib/dimples/pager.rb, line 28
def paginate(metadata)
  (1..@page_count).each do |index|
    step_to(index)

    @site.layouts['posts']&.write(
      File.join(@site.config[:output][:root], current_page_url, 'index.html'),
      metadata.merge(pagination: self.metadata, url: current_page_url)
    )
  end
end
posts_at(page) click to toggle source
# File lib/dimples/pager.rb, line 47
def posts_at(page)
  @posts.slice((page - 1) * @per_page, @per_page)
end
previous_page?() click to toggle source
# File lib/dimples/pager.rb, line 51
def previous_page?
  (@current_page - 1).positive?
end
previous_page_url() click to toggle source
# File lib/dimples/pager.rb, line 71
def previous_page_url
  return unless @previous_page

  @previous_page == 1 ? @url : "#{@url}#{@page_prefix}#{@previous_page}"
end
step_to(page) click to toggle source
# File lib/dimples/pager.rb, line 39
def step_to(page)
  @current_page = page
  @previous_page = previous_page? ? @current_page - 1 : nil
  @next_page = next_page? ? @current_page + 1 : nil

  @current_page
end
urls() click to toggle source
# File lib/dimples/pager.rb, line 81
def urls
  {
    current_page: current_page_url,
    first_page: first_page_url,
    last_page: last_page_url,
    previous_page: previous_page_url,
    next_page: next_page_url
  }
end