diff --git a/lib/locomotive/liquid/tags/paginate.rb b/lib/locomotive/liquid/tags/paginate.rb index 22e8211c..0e65c479 100644 --- a/lib/locomotive/liquid/tags/paginate.rb +++ b/lib/locomotive/liquid/tags/paginate.rb @@ -46,7 +46,7 @@ module Locomotive end page_count, current_page = pagination['total_pages'], pagination['current_page'] - path = context['path'] + 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'] @@ -83,6 +83,12 @@ module Locomotive private + def sanitize_path(path) + _path = path.gsub(/page=[0-9]+&?/, '').gsub(/_pjax=true&?/, '') + _path = _path.slice(0..-2) if _path.last == '?' || _path.last == '&' + _path + end + def window_size 3 end @@ -92,7 +98,8 @@ module Locomotive end def link(title, page, path) - { 'title' => title, 'url' => path + "?page=#{page}", 'is_link' => true} + _path = %(#{path}#{path.include?('?') ? '&' : '?'}page=#{page}) + { 'title' => title, 'url' => _path, 'is_link' => true } end end diff --git a/spec/lib/locomotive/liquid/tags/paginate_spec.rb b/spec/lib/locomotive/liquid/tags/paginate_spec.rb index 2058320a..e3b8e0a7 100644 --- a/spec/lib/locomotive/liquid/tags/paginate_spec.rb +++ b/spec/lib/locomotive/liquid/tags/paginate_spec.rb @@ -2,14 +2,14 @@ require 'spec_helper' describe Locomotive::Liquid::Tags::Paginate do - it 'should have a valid syntax' do + it 'has a valid syntax' do markup = "contents.projects by 5" lambda do Locomotive::Liquid::Tags::Paginate.new('paginate', markup, ["{% endpaginate %}"], {}) end.should_not raise_error end - it 'should raise an error if the syntax is incorrect' do + it 'raises an error if the syntax is incorrect' do ["contents.projects by a", "contents.projects", "contents.projects 5"].each do |markup| lambda do Locomotive::Liquid::Tags::Paginate.new('paginate', markup, ["{% endpaginate %}"], {}) @@ -17,9 +17,9 @@ describe Locomotive::Liquid::Tags::Paginate do end end - it 'should paginate the collection' do - template = Liquid::Template.parse(default_template) - text = template.render!(liquid_context) + it 'paginates the collection' do + template = Liquid::Template.parse(default_template) + text = template.render!(liquid_context) text.should match /!Ruby on Rails!/ text.should match /!jQuery!/ @@ -33,7 +33,7 @@ describe Locomotive::Liquid::Tags::Paginate do text.should_not match /!sqlite3!/ end - it 'should not paginate if collection is nil or empty' do + it 'does not paginate if collection is nil or empty' do template = Liquid::Template.parse(default_template) lambda do @@ -45,6 +45,20 @@ describe Locomotive::Liquid::Tags::Paginate do end.should raise_error end + it 'keeps the original GET parameters' do + context = liquid_context(:fullpath => '/products?foo=1&bar=1&baz=1') + template = Liquid::Template.parse(default_template) + text = template.render!(context) + text.should match /\/products\?foo=1&bar=1&baz=1&page=2/ + end + + it 'does not include twice the page parameter' do + context = liquid_context(:fullpath => '/products?page=1') + template = Liquid::Template.parse(default_template) + text = template.render!(context) + text.should match /\/products\?page=2/ + end + # ___ helpers methods ___ # def liquid_context(options = {}) @@ -53,7 +67,8 @@ describe Locomotive::Liquid::Tags::Paginate do { 'projects' => options.has_key?(:collection) ? options[:collection] : PaginatedCollection.new(['Ruby on Rails', 'jQuery', 'mongodb', 'Liquid', 'sqlite3']), 'current_page' => options[:page] || 1, - 'path' => '/' + 'path' => '/', + 'fullpath' => options[:fullpath] || '/' }, { :page => FactoryGirl.build(:page) }, true) @@ -64,6 +79,7 @@ describe Locomotive::Liquid::Tags::Paginate do {% for project in paginate.collection %} !{{ project }}! {% endfor %} + {{ paginate.next.url }} {% endpaginate %}" end