Class: Sigmund::Liquid::Tags::CustomPaginate
- Inherits:
-
Liquid::Block
- Object
- Liquid::Block
- Sigmund::Liquid::Tags::CustomPaginate
- Defined in:
- lib/sigmund/liquid/tags/custom_paginate.rb
Constant Summary collapse
- Syntax =
/(#{::Liquid::Expression}+)?/
Instance Method Summary collapse
-
#initialize(tag_name, markup, tokens, context) ⇒ CustomPaginate
constructor
A new instance of CustomPaginate.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens, context) ⇒ CustomPaginate
Returns a new instance of CustomPaginate.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sigmund/liquid/tags/custom_paginate.rb', line 11 def initialize(tag_name, markup, tokens, context) if markup =~ Syntax @collection_name = $1 @options = { } markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') } @per_page = @options[:per_page] ? @options[:per_page] : 10 @window_size = @options[:window_size] ? @options[:window_size].to_i : 3 else raise ::Liquid::SyntaxError.new("Syntax Error in 'paginate' - Valid syntax: paginate <collection> by <number>") end super end |
Instance Method Details
#render(context) ⇒ Object
25 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/sigmund/liquid/tags/custom_paginate.rb', line 25 def render(context) context.stack do collection = context[@collection_name] @per_page = context[@per_page] if @per_page == 'all' @per_page = collection.size + 1 end raise ::Liquid::ArgumentError.new("Cannot paginate array '#{@collection_name}'. Not found.") if collection.nil? if collection.is_a? Array pagination = Kaminari.paginate_array(collection).page(context['current_page']).per(@per_page).to_liquid.stringify_keys else pagination = collection.send(:paginate, { page: context['current_page'], per_page: @per_page }).to_liquid.stringify_keys end page_count, current_page = pagination['total_pages'], pagination['current_page'] path = sanitize_path(context['fullpath']) pagination['previous'] = link(I18n.t('pagination.previous'), current_page - 1, path) if pagination['previous_page'] pagination['next'] = link(I18n.t('pagination.next'), current_page + 1, path) if pagination['next_page'] 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, path) elsif page == page_count pagination['parts'] << link(page, page, path) 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, path) end hellip_break = false end end context['custom_paginate'] = pagination render_all(@nodelist, context) end end |