class SmartPagination::Renderer

Public Class Methods

new(context, collection, options = {}) click to toggle source
# File lib/smart_pagination/renderer.rb, line 3
def initialize(context, collection, options = {})
  @context    = context
  @collection = collection
  @options    = default_options.merge(options)
end

Public Instance Methods

render() click to toggle source
# File lib/smart_pagination/renderer.rb, line 9
def render
  if auto_hide?
    nil
  elsif info_mode?
    pagination_info
  elsif pager_mode?
    pager
  else
    pagination
  end
end

Private Instance Methods

auto_hide?() click to toggle source
# File lib/smart_pagination/renderer.rb, line 76
def auto_hide?
  @options[:auto_hide].present? && total_pages < 2
end
collection_name() click to toggle source
# File lib/smart_pagination/renderer.rb, line 43
def collection_name
  if @collection.is_a? ActiveRecord::Relation
    @collection.model_name.human(count: total_entries)
  else
    total_entries == 1 ? 'Item' : 'Items'
  end
end
current_entries() click to toggle source
# File lib/smart_pagination/renderer.rb, line 67
def current_entries
  entries = current_page * per_page
  entries < total_entries ? entries : total_entries
end
current_page() click to toggle source
# File lib/smart_pagination/renderer.rb, line 51
def current_page
  @collection.current_page.to_i
end
current_page?(page) click to toggle source
# File lib/smart_pagination/renderer.rb, line 59
def current_page?(page)
  page.to_i == current_page
end
default_options() click to toggle source
# File lib/smart_pagination/renderer.rb, line 23
def default_options
  {
    info_mode:      false,
    pager_mode:     false,
    auto_hide:      false,
    item_class:     '',
    previous_text:  '&laquo;',
    previous_class: 'previous',
    next_text:      '&raquo;',
    next_class:     'next',
    active_class:   'active',
    disabled_class: 'disabled',
    wrapper:        'ul',
    wrapper_class:  'pagination',
    item_wrapper:   'li',
    inner_window:    2,
    outer_window:    0
  }
end
info_mode?() click to toggle source
# File lib/smart_pagination/renderer.rb, line 84
def info_mode?
  @options[:info_mode].present?
end
item_sep() click to toggle source
# File lib/smart_pagination/renderer.rb, line 169
def item_sep
  item_link '&hellip;', {}, class: "#{@options[:item_class]} #{@options[:disabled_class]}".strip
end
page_numbers() click to toggle source
# File lib/smart_pagination/renderer.rb, line 95
def page_numbers
  inner = @options[:inner_window].to_i
  outer = @options[:outer_window].to_i
  from  = current_page - inner
  to    = current_page + inner

  if to > total_pages
    from -= to - total_pages
    to    = total_pages
  end

  if from < 1
    to   += 1 - from
    from  = 1
    to    = total_pages if to > total_pages
  end

  middle = from..to

  if outer + 3 < middle.first
    left = (1..(outer + 1)).to_a
    left << :sep
  else
    left = 1...middle.first
  end

  if total_pages - outer - 2 > middle.last
    right = ((total_pages - outer)..total_pages).to_a
    right.unshift :sep
  else
    right = (middle.last + 1)..total_pages
  end

  left.to_a + middle.to_a + right.to_a
end
pager() click to toggle source
# File lib/smart_pagination/renderer.rb, line 202
def pager
  items = [previous_link, next_link]
  wrapper items.map(&:to_s).join
end
pager_mode?() click to toggle source
# File lib/smart_pagination/renderer.rb, line 80
def pager_mode?
  @options[:pager_mode].present?
end
pagination() click to toggle source
# File lib/smart_pagination/renderer.rb, line 207
def pagination
  items = page_numbers.map { |page| page == :sep ? item_sep : page_link(page) }
  items = [previous_link, *items, next_link]

  wrapper items.map(&:to_s).join
end
pagination_info() click to toggle source
# File lib/smart_pagination/renderer.rb, line 195
def pagination_info
  current = tag :strong, current_entries, class: 'current'
  total   = tag :strong, total_entries, class: 'total'

  "Displaying #{current} of #{total} #{collection_name}".html_safe
end
per_page() click to toggle source
# File lib/smart_pagination/renderer.rb, line 63
def per_page
  @collection.per_page.to_i
end
tag(*args, &block) click to toggle source
# File lib/smart_pagination/renderer.rb, line 173
def tag(*args, &block)
  @context.content_tag(*args, &block)
end
total_entries() click to toggle source
# File lib/smart_pagination/renderer.rb, line 72
def total_entries
  @total_entries ||= @collection.total_entries.to_i
end
total_pages() click to toggle source
# File lib/smart_pagination/renderer.rb, line 55
def total_pages
  @total_pages ||= @collection.total_pages.to_i
end
url_params() click to toggle source
# File lib/smart_pagination/renderer.rb, line 88
def url_params
  url    = URI.parse(@context.request.url)
  params = Rack::Utils.parse_nested_query(url.query).symbolize_keys

  params.except(:page)
end
wrapper(links) click to toggle source
# File lib/smart_pagination/renderer.rb, line 186
def wrapper(links)
  wrapper  = @options[:wrapper]
  wrap_opt = { class: @options[:wrapper_class] }
  wrap_tag = links.html_safe
  wrap_tag = tag wrapper, wrap_tag, wrap_opt if wrapper.present?

  wrap_tag
end