Class: Paginate

Inherits:
Liquid::Block show all
Defined in:
lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/paginate.rb

Constant Summary collapse

Syntax =
/(#{Liquid::QuotedFragment})\s*(by\s*(\d+))?/

Constants inherited from Liquid::Block

Liquid::Block::ContentOfVariable, Liquid::Block::FullToken, Liquid::Block::IsTag, Liquid::Block::IsVariable

Instance Attribute Summary

Attributes inherited from Liquid::Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Liquid::Block

#block_delimiter, #block_name, #create_variable, #end_tag, #parse, #unknown_tag

Methods inherited from Liquid::Tag

#name, #parse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Paginate



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/paginate.rb', line 4

def initialize(tag_name, markup, tokens)
  @nodelist = []
  
  if markup =~ Syntax
    @collection_name = $1
    @page_size = if $2
      $3.to_i
    else
      20
    end
    
    @attributes = { 'window_size' => 3 }
    markup.scan(Liquid::TagAttributes) do |key, value|
      @attributes[key] = value
    end        
  else
    raise SyntaxError.new("Syntax Error in tag 'paginate' - Valid syntax: paginate [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
# File 'lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/paginate.rb', line 26

def render(context)
  @context = context

  context.stack do                      
    current_page  = context['current_page'].to_i

    pagination = {
      'page_size'      => @page_size,
      'current_page'   => 5,
      'current_offset' => @page_size * 5
    }
    
    context['paginate'] = pagination      
    
    collection_size  = context[@collection_name].size      

    raise ArgumentError.new("Cannot paginate array '#{@collection_name}'. Not found.") if collection_size.nil?
  
    page_count = (collection_size.to_f / @page_size.to_f).to_f.ceil + 1 

    pagination['items']      = collection_size
    pagination['pages']      = page_count -1
    pagination['previous']   = link('« Previous', current_page-1 )  unless 1 >= current_page
    pagination['next']       = link('Next &raquo;', current_page+1 )      unless page_count <= current_page+1
    pagination['parts']      = []
    
    hellip_break = false
    
    if page_count > 2
      1.upto(page_count-1) do |page|          
      
        if current_page == page
          pagination['parts'] << no_link(page)        
        elsif page == 1 
          pagination['parts'] << link(page, page)
        elsif page == page_count -1
          pagination['parts'] << link(page, page)
        elsif page <= current_page - @attributes['window_size'] or page >= current_page + @attributes['window_size']
          next if hellip_break
          pagination['parts'] << no_link('&hellip;') 
          hellip_break = true
          next
        else
          pagination['parts'] << link(page, page)
        end
      
        hellip_break = false
      end 
    end
    
    render_all(@nodelist, context)      
  end
end