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: '«', previous_class: 'previous', next_text: '»', 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_link(text, params = {}, html_options = {})
click to toggle source
# File lib/smart_pagination/renderer.rb, line 160 def item_link(text, params = {}, html_options = {}) wrapper = @options[:item_wrapper] link_opt = html_options if wrapper.blank? item_tag = link_to text, params, link_opt item_tag = tag wrapper, item_tag, html_options if wrapper.present? item_tag end
item_sep()
click to toggle source
# File lib/smart_pagination/renderer.rb, line 169 def item_sep item_link '…', {}, class: "#{@options[:item_class]} #{@options[:disabled_class]}".strip end
link_options(page)
click to toggle source
# File lib/smart_pagination/renderer.rb, line 136 def link_options(page) if page.is_a? Integer active = @options[:active_class] if current_page? page { class: "#{@options[:item_class]} #{active}".strip } else disabled = @options[:disabled_class] unless @collection.send(:"#{page}_page").present? item_class = @options[:"#{page}_class"] { class: "#{@options[:item_class]} #{item_class} #{disabled}".strip } end end
link_params(page)
click to toggle source
# File lib/smart_pagination/renderer.rb, line 131 def link_params(page) page = @collection.send(:"#{page}_page") unless page.is_a? Integer { page: page } end
link_to(text, params = {}, html_options = {})
click to toggle source
# File lib/smart_pagination/renderer.rb, line 177 def link_to(text, params = {}, html_options = {}) prm = params.except(:page) if params[:page] == 1 url = @context.url_for url_params.merge(prm || params) opt = html_options.to_h opt = opt.merge(href: url) if params[:page].present? tag :a, "#{text}".html_safe, opt end
next_link()
click to toggle source
# File lib/smart_pagination/renderer.rb, line 156 def next_link item_link @options[:next_text], link_params(:next), link_options(:next) end
page_link(page)
click to toggle source
# File lib/smart_pagination/renderer.rb, line 148 def page_link(page) item_link page, link_params(page), link_options(page) 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
previous_link()
click to toggle source
# File lib/smart_pagination/renderer.rb, line 152 def previous_link item_link @options[:previous_text], link_params(:previous), link_options(:previous) 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