preserve GET parameters when paginating a collection

This commit is contained in:
Didier Lafforgue 2012-03-26 17:58:01 +02:00
parent ed15c0d6b5
commit 90e1c4f438
2 changed files with 32 additions and 9 deletions

View File

@ -46,7 +46,7 @@ module Locomotive
end end
page_count, current_page = pagination['total_pages'], pagination['current_page'] 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['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['next'] = link(I18n.t('pagination.next'), current_page + 1, path) if pagination['next_page']
@ -83,6 +83,12 @@ module Locomotive
private 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 def window_size
3 3
end end
@ -92,7 +98,8 @@ module Locomotive
end end
def link(title, page, path) 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
end end

View File

@ -2,14 +2,14 @@ require 'spec_helper'
describe Locomotive::Liquid::Tags::Paginate do describe Locomotive::Liquid::Tags::Paginate do
it 'should have a valid syntax' do it 'has a valid syntax' do
markup = "contents.projects by 5" markup = "contents.projects by 5"
lambda do lambda do
Locomotive::Liquid::Tags::Paginate.new('paginate', markup, ["{% endpaginate %}"], {}) Locomotive::Liquid::Tags::Paginate.new('paginate', markup, ["{% endpaginate %}"], {})
end.should_not raise_error end.should_not raise_error
end 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| ["contents.projects by a", "contents.projects", "contents.projects 5"].each do |markup|
lambda do lambda do
Locomotive::Liquid::Tags::Paginate.new('paginate', markup, ["{% endpaginate %}"], {}) Locomotive::Liquid::Tags::Paginate.new('paginate', markup, ["{% endpaginate %}"], {})
@ -17,9 +17,9 @@ describe Locomotive::Liquid::Tags::Paginate do
end end
end end
it 'should paginate the collection' do it 'paginates the collection' do
template = Liquid::Template.parse(default_template) template = Liquid::Template.parse(default_template)
text = template.render!(liquid_context) text = template.render!(liquid_context)
text.should match /!Ruby on Rails!/ text.should match /!Ruby on Rails!/
text.should match /!jQuery!/ text.should match /!jQuery!/
@ -33,7 +33,7 @@ describe Locomotive::Liquid::Tags::Paginate do
text.should_not match /!sqlite3!/ text.should_not match /!sqlite3!/
end 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) template = Liquid::Template.parse(default_template)
lambda do lambda do
@ -45,6 +45,20 @@ describe Locomotive::Liquid::Tags::Paginate do
end.should raise_error end.should raise_error
end 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 ___ # # ___ helpers methods ___ #
def liquid_context(options = {}) 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']), 'projects' => options.has_key?(:collection) ? options[:collection] : PaginatedCollection.new(['Ruby on Rails', 'jQuery', 'mongodb', 'Liquid', 'sqlite3']),
'current_page' => options[:page] || 1, 'current_page' => options[:page] || 1,
'path' => '/' 'path' => '/',
'fullpath' => options[:fullpath] || '/'
}, { }, {
:page => FactoryGirl.build(:page) :page => FactoryGirl.build(:page)
}, true) }, true)
@ -64,6 +79,7 @@ describe Locomotive::Liquid::Tags::Paginate do
{% for project in paginate.collection %} {% for project in paginate.collection %}
!{{ project }}! !{{ project }}!
{% endfor %} {% endfor %}
{{ paginate.next.url }}
{% endpaginate %}" {% endpaginate %}"
end end