Class: Dugway::Tags::Paginate
- Inherits:
-
Liquid::Block
- Object
- Liquid::Block
- Dugway::Tags::Paginate
- Defined in:
- lib/dugway/liquid/tags/paginate.rb
Constant Summary collapse
- Syntax =
/(\w+)\s+from\s+(#{ Liquid::QuotedFragment })\s*(by\s*(#{ Liquid::QuotedFragment }))?/
Instance Method Summary collapse
-
#initialize(tag_name, markup, tokens) ⇒ Paginate
constructor
A new instance of Paginate.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ Paginate
Returns a new instance of Paginate.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dugway/liquid/tags/paginate.rb', line 6 def initialize(tag_name, markup, tokens) if markup =~ Syntax @variable_name = $1 @collection_name = $2 @per_page = $3.present? ? $4 : nil @attributes = { 'inner_window' => 3, 'outer_window' => 1 } markup.scan(Liquid::TagAttributes) { |key, value| @attributes[key] = value } @limit = @attributes['limit'] else raise SyntaxError.new("Syntax Error in tag 'paginate' - Valid syntax: paginate [variable] from [collection] by [number]") end super end |
Instance Method Details
#render(context) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dugway/liquid/tags/paginate.rb', line 26 def render(context) @context = context @limit = context[@limit].present? ? context[@limit] : (@limit.present? ? @limit.to_i : nil) @per_page = context[@per_page].present? ? context[@per_page] : (@per_page.present? ? @per_page.to_i : nil) @order = context[@attributes['order']].present? ? context[@attributes['order']] : @attributes['order'] context.stack do context['internal'] = { 'per_page' => @per_page, 'order' => @order, 'page' => @context.registers[:params][:page] && @context.registers[:params][:page].to_i, 'limit' => @limit } collection = context[@collection_name] context[@variable_name] = collection current_page = collection.current_page pagination = { 'page_size' => collection.per_page, 'current_page' => current_page, 'current_offset' => collection.offset } context['paginate'] = pagination collection_size = collection.total_entries raise ArgumentError.new("Cannot paginate array '#{ @collection_name }'. Not found.") if collection_size.nil? page_count = collection.total_pages pagination['items'] = collection_size pagination['pages'] = page_count pagination['previous'] = collection.previous_page.blank? ? no_link('« Previous') : link('« Previous', collection.previous_page) pagination['next'] = collection.next_page.blank? ? no_link('Next »') : link('Next »', collection.next_page) pagination['parts'] = [] if page_count > 1 inner_window = @attributes['inner_window'].to_i outer_window = @attributes['outer_window'].to_i min = current_page - inner_window max = current_page + inner_window # Adjust lower or upper limit if other is out of bounds if max > page_count min -= max - page_count elsif min < 1 max += 1 - min end current = min..max beginning = 1..(1 + outer_window) tail = (page_count - outer_window)..page_count visible = [current, beginning, tail].map(&:to_a).flatten visible &= (1..page_count).to_a hellip_break = false 1.upto(page_count) { |page| if page == current_page pagination['parts'] << no_link(page) elsif visible.include?(page) pagination['parts'] << link(page, page) elsif page == beginning.last + 1 || page == tail.first - 1 next if hellip_break pagination['parts'] << no_link('…') hellip_break = true next end hellip_break = false } end render_all @nodelist, context end end |