class Locomotive::Steam::Liquid::Tags::Paginate

Paginate a collection (array or from a DB).

Usage:

{% paginate contents.projects by 5 %}

 {% for project in paginate.collection %}
   {{ project.name }}
 {% endfor %}
{% endpaginate %}

Constants

Syntax

Attributes

collection_name[R]
per_page[R]

Public Class Methods

new(tag_name, markup, options) click to toggle source
Calls superclass method
# File lib/locomotive/steam/liquid/tags/paginate.rb, line 24
def initialize(tag_name, markup, options)
  if markup =~ Syntax
    @collection_name  = $1
    @per_page         = $2

    parse_attributes(markup)
  else
    raise ::Liquid::SyntaxError.new('Valid syntax: paginate <collection> by <number>')
  end

  super
end

Public Instance Methods

render(context) click to toggle source
Calls superclass method
# File lib/locomotive/steam/liquid/tags/paginate.rb, line 37
def render(context)
  evaluate_attributes(context)

  context.stack do
    pagination = context['paginate'] = paginate_collection(context)

    path = sanitize_path(context['fullpath'])

    build_next_previous_links(pagination, path)

    if pagination['total_pages'] > 1
      build_parts(pagination, path)
    end

    super
  end
end

Private Instance Methods

_build_parts(pagination, path, page, hellip_break) click to toggle source
# File lib/locomotive/steam/liquid/tags/paginate.rb, line 96
def _build_parts(pagination, path, page, hellip_break)
  if pagination['current_page'] == page
    pagination['parts'] << no_link(page)
  elsif is_page_a_bound?(pagination, page)
    pagination['parts'] << link(page, page, path)
  elsif is_page_inside_window?(pagination, page)
    pagination['parts'] << no_link('&hellip;') unless hellip_break
    return true
  else
    pagination['parts'] << link(page, page, path)
  end

  false
end
build_parts(pagination, path) click to toggle source
# File lib/locomotive/steam/liquid/tags/paginate.rb, line 88
def build_parts(pagination, path)
  hellip_break  = false

  1.upto(pagination['total_pages']) do |page|
    hellip_break = _build_parts(pagination, path, page, hellip_break)
  end
end
is_page_a_bound?(pagination, page) click to toggle source
# File lib/locomotive/steam/liquid/tags/paginate.rb, line 121
def is_page_a_bound?(pagination, page)
  page == 1 || page == pagination['total_pages']
end
is_page_inside_window?(pagination, page) click to toggle source
# File lib/locomotive/steam/liquid/tags/paginate.rb, line 117
def is_page_inside_window?(pagination, page)
  page <= pagination['current_page'] - window_size or page >= pagination['current_page'] + window_size
end
paginate_collection(context) click to toggle source

Paginate the collection and returns a pagination object storing all the information about the paginated collection.

# File lib/locomotive/steam/liquid/tags/paginate.rb, line 61
def paginate_collection(context)
  collection    = context[collection_name]
  per_page      = context[self.per_page].to_i
  current_page  = context['current_page'].try(:to_i)

  raise ::Liquid::ArgumentError.new("Cannot paginate '#{collection_name}'. Not found.") if collection.nil?

  pager = Locomotive::Steam::Models::Pager.new(collection, current_page, per_page)

  # make sure the pagination object is a hash with strings as keys (and not symbol)
  HashConverter.to_string(pager.to_liquid).tap do |_pagination|
    _pagination['parts'] = []
  end
end
sanitize_path(path) click to toggle source
# File lib/locomotive/steam/liquid/tags/paginate.rb, line 111
def sanitize_path(path)
  _path = path.gsub(/page=[0-9]+&?/, '').gsub(/_pjax=true&?/, '')
  _path = _path.slice(0..-2) if _path.last == '?' || _path.last == '&'
  _path
end
window_size() click to toggle source
# File lib/locomotive/steam/liquid/tags/paginate.rb, line 125
def window_size
  @window_size ||= attributes[:window_size] ? attributes[:window_size].to_i : 3
end