preserve GET parameters when paginating a collection
This commit is contained in:
parent
ed15c0d6b5
commit
90e1c4f438
@ -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
|
||||
|
||||
|
@ -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,7 +17,7 @@ describe Locomotive::Liquid::Tags::Paginate do
|
||||
end
|
||||
end
|
||||
|
||||
it 'should paginate the collection' do
|
||||
it 'paginates the collection' do
|
||||
template = Liquid::Template.parse(default_template)
|
||||
text = template.render!(liquid_context)
|
||||
|
||||
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user