Added a cucumber feature to paginate through models.

This commit is contained in:
Mario Visic 2011-11-05 19:10:26 +11:00
parent c3c5d8e754
commit ad0adc7153
5 changed files with 49 additions and 11 deletions

View File

@ -0,0 +1,8 @@
Feature: Pagination
As a designer
In order to display a sensible amount of items per page
I want to be able to paginate through models
Scenario: Paginating through a model
Given I have a site setup
Then I should be able to display paginated models

View File

@ -0,0 +1,35 @@
Then /^I should be able to display paginated models$/ do
# Create our article model and three articles
@article_model = FactoryGirl.build(:content_type, :site => @site, :name => 'Articles').tap do |ct|
ct.content_custom_fields.build :label => 'Body', :kind => 'string', :required => false
ct.save!
end
%w(First Second Third).each do |body|
@article_model.contents.create!(:body => body)
end
# Create a page with template
raw_template = %{
{% paginate contents.articles by 2 %}
{% for article in paginate.collection %}
{{ article.body }}
{% endfor %}
{{ paginate | default_pagination }}
{% endpaginate %}
}
FactoryGirl.create(:page, :site => @site, :slug => 'hello', :raw_template => raw_template)
# The page should have the first two articles
visit '/hello'
page.should have_content 'First'
page.should have_content 'Second'
page.should_not have_content 'Third'
# The second page should have the last article
click_link '2'
page.should_not have_content 'First'
page.should_not have_content 'Second'
page.should have_content 'Third'
end

View File

@ -13,6 +13,10 @@ Given /^I have the site: "([^"]*)" set up(?: with #{capture_fields})?$/ do |site
@admin.should_not be_nil @admin.should_not be_nil
end end
Given /^I have a site setup$/ do
Given %{I have the site: "test site" set up}
end
Given /^I have a designer and an author$/ do Given /^I have a designer and an author$/ do
FactoryGirl.create(:designer, :site => Site.first) FactoryGirl.create(:designer, :site => Site.first)
FactoryGirl.create(:author, :site => Site.first) FactoryGirl.create(:author, :site => Site.first)

View File

@ -64,16 +64,7 @@ module Locomotive
protected protected
def paginate(options = {}) def paginate(options = {})
@collection = self.collection.paginate(options) @collection = Kaminari.paginate_array(self.collection).page(options[:page]).per(options[:per_page])
{
:collection => @collection,
:current_page => @collection.current_page,
:previous_page => @collection.previous_page,
:next_page => @collection.next_page,
:total_entries => @collection.total_entries,
:total_pages => @collection.total_pages,
:per_page => @collection.per_page
}
end end
def collection def collection

View File

@ -1,7 +1,7 @@
class Kaminari::PaginatableArray class Kaminari::PaginatableArray
def to_liquid(options = {}) def to_liquid(options = {})
{ {
:collection => self.to_a, :collection => to_a,
:current_page => current_page, :current_page => current_page,
:previous_page => first_page? ? nil : current_page - 1, :previous_page => first_page? ? nil : current_page - 1,
:next_page => last_page? ? nil : current_page + 1, :next_page => last_page? ? nil : current_page + 1,