2010-05-30 23:57:33 +00:00
|
|
|
require 'spec_helper'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
describe Locomotive::Liquid::Tags::Paginate do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
it 'should have a valid syntax' do
|
|
|
|
markup = "contents.projects by 5"
|
|
|
|
lambda do
|
2010-08-20 15:56:15 +00:00
|
|
|
Locomotive::Liquid::Tags::Paginate.new('paginate', markup, ["{% endpaginate %}"], {})
|
2010-05-30 23:57:33 +00:00
|
|
|
end.should_not raise_error
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
it 'should raise an error if the syntax is incorrect' do
|
|
|
|
["contents.projects by a", "contents.projects", "contents.projects 5"].each do |markup|
|
|
|
|
lambda do
|
2010-08-20 15:56:15 +00:00
|
|
|
Locomotive::Liquid::Tags::Paginate.new('paginate', markup, ["{% endpaginate %}"], {})
|
2010-05-30 23:57:33 +00:00
|
|
|
end.should raise_error
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
it 'should paginate the collection' do
|
|
|
|
template = Liquid::Template.parse(default_template)
|
|
|
|
text = template.render!(liquid_context)
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
text.should match /!Ruby on Rails!/
|
|
|
|
text.should match /!jQuery!/
|
|
|
|
text.should_not match /!mongodb!/
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
text = template.render!(liquid_context(:page => 2))
|
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
text.should_not match /!jQuery!/
|
2010-05-30 23:57:33 +00:00
|
|
|
text.should match /!mongodb!/
|
|
|
|
text.should match /!Liquid!/
|
|
|
|
text.should_not match /!sqlite3!/
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
it 'should not paginate if collection is nil or empty' do
|
|
|
|
template = Liquid::Template.parse(default_template)
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
lambda do
|
|
|
|
template.render!(liquid_context(:collection => nil))
|
|
|
|
end.should raise_error
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
lambda do
|
|
|
|
template.render!(liquid_context(:collection => PaginatedCollection.new))
|
|
|
|
end.should raise_error
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
# ___ helpers methods ___ #
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def liquid_context(options = {})
|
2010-08-04 13:12:14 +00:00
|
|
|
::Liquid::Context.new(
|
|
|
|
{},
|
|
|
|
{
|
2010-05-30 23:57:33 +00:00
|
|
|
'projects' => options.has_key?(:collection) ? options[:collection] : PaginatedCollection.new(['Ruby on Rails', 'jQuery', 'mongodb', 'Liquid', 'sqlite3']),
|
|
|
|
'current_page' => options[:page] || 1
|
2010-10-29 15:40:58 +00:00
|
|
|
}, {
|
|
|
|
:page => Factory.build(:page)
|
|
|
|
}, true)
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def default_template
|
|
|
|
"{% paginate projects by 2 %}
|
|
|
|
{% for project in paginate.collection %}
|
|
|
|
!{{ project }}!
|
|
|
|
{% endfor %}
|
|
|
|
{% endpaginate %}"
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
class PaginatedCollection
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def initialize(collection)
|
|
|
|
@collection = collection || []
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def paginate(options = {})
|
|
|
|
total_pages = (@collection.size.to_f / options[:per_page].to_f).to_f.ceil + 1
|
|
|
|
offset = (options[:page] - 1) * options[:per_page]
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
{
|
|
|
|
:collection => @collection[offset..(offset + options[:per_page]) - 1],
|
|
|
|
:current_page => options[:page],
|
|
|
|
:previous_page => options[:page] == 1 ? 1 : options[:page] - 1,
|
|
|
|
:next_page => options[:page] == total_pages ? total_pages : options[:page] + 1,
|
|
|
|
:total_entries => @collection.size,
|
|
|
|
:total_pages => total_pages,
|
|
|
|
:per_page => options[:per_page]
|
2010-07-23 20:09:54 +00:00
|
|
|
}
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def each(&block)
|
|
|
|
@collection.each(&block)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def method_missing(method, *args)
|
|
|
|
@collection.send(method, *args)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def to_liquid
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
end
|