Class: Liquid::Rails::PaginateTag
- Inherits:
-
Block
- Object
- Block
- Liquid::Rails::PaginateTag
- Defined in:
- lib/liquid4-rails/tags/paginate_tag.rb
Constant Summary collapse
- Syntax =
/(#{::Liquid::QuotedFragment})\s*(by\s*(\d+))?/
Instance Method Summary collapse
-
#initialize(tag_name, markup, context) ⇒ PaginateTag
constructor
A new instance of PaginateTag.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, context) ⇒ PaginateTag
Returns a new instance of PaginateTag.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/liquid4-rails/tags/paginate_tag.rb', line 16 def initialize(tag_name, markup, context) super if markup =~ Syntax @collection_name = $1 @page_size = if $2 $3.to_i else 25 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 end |
Instance Method Details
#render(context) ⇒ Object
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 |
# File 'lib/liquid4-rails/tags/paginate_tag.rb', line 36 def render(context) @context = context context.stack do collection = @context[@collection_name].presence || @context.environments[0][@collection_name] raise ::Liquid::ArgumentError.new("Cannot paginate collection '#{@collection_name}'. Not found.") if collection.nil? if collection.is_a? Array paginated_collection = Kaminari.paginate_array(collection.to_a).page(current_page).per(@page_size) elsif collection.respond_to?(:page) paginated_collection = collection.page(current_page).per(@page_size) end page_count = paginated_collection.total_pages pagination = {} pagination['collection'] = paginated_collection pagination['current_offset'] = (current_page-1) * @page_size pagination['current_page'] = current_page pagination['page_size'] = @page_size pagination['pages'] = page_count pagination['items'] = paginated_collection.total_count pagination['previous'] = link('« Previous'.html_safe, current_page-1 ) unless 1 >= current_page pagination['next'] = link('Next »'.html_safe, current_page+1 ) unless page_count < current_page+1 pagination['parts'] = [] hellip_break = false if page_count > 1 1.upto(page_count) 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 - window_size or page >= current_page + window_size next if hellip_break pagination['parts'] << no_link('…') hellip_break = true next else pagination['parts'] << link(page, page) end hellip_break = false end end context['paginate'] = pagination super end end |