Tests are passing for has many pagination with Kaminari.

This commit is contained in:
Mario Visic 2011-11-05 18:42:04 +11:00
parent 45d4b7cb60
commit c3c5d8e754
4 changed files with 39 additions and 12 deletions

View File

@ -19,10 +19,14 @@ end
Then /^I should be able to view a paginated list of "([^"]*)" per "([^"]*)"$/ do |parent_model, child_model|
# Create contents
@parent_model.contents.create!(:slug => 'parent', :body => 'Parent')
@child_model.contents.create!(:slug => 'one', :body => 'One')
@child_model.contents.create!(:slug => 'two', :body => 'Two')
@child_model.contents.create!(:slug => 'three', :body => 'Three')
article = @parent_model.contents.create!(:slug => 'parent', :body => 'Parent')
@child_model.contents.create!(:slug => 'one', :body => 'One', :custom_field_2 => article.id.to_s)
@child_model.contents.create!(:slug => 'two', :body => 'Two', :custom_field_2 => article.id.to_s)
@child_model.contents.create!(:slug => 'three', :body => 'Three', :custom_field_2 => article.id.to_s)
@child_model.contents.each do |comment|
article.comments << comment
end
# Create a page
raw_template = %{
@ -41,7 +45,14 @@ Then /^I should be able to view a paginated list of "([^"]*)" per "([^"]*)"$/ do
# The page should have the first two comments
visit '/hello'
page.should have_content 'one'
page.should have_content 'two'
page.should_not have_content 'three'
page.should have_content 'One'
page.should have_content 'Two'
page.should_not have_content 'Three'
# The second page should have the last comment
click_link '2'
page.should_not have_content 'One'
page.should_not have_content 'Two'
page.should have_content 'Three'
end

View File

@ -5,7 +5,7 @@ require 'json/pure'
require 'devise'
require 'mongoid'
require 'mongoid_acts_as_tree'
require 'will_paginate'
require 'kaminari'
require 'haml'
require 'liquid'
require 'formtastic'

View File

@ -0,0 +1,13 @@
class Kaminari::PaginatableArray
def to_liquid(options = {})
{
:collection => self.to_a,
:current_page => current_page,
:previous_page => first_page? ? nil : current_page - 1,
:next_page => last_page? ? nil : current_page + 1,
:total_entries => total_count,
:total_pages => num_pages,
:per_page => limit_value
}
end
end

View File

@ -36,10 +36,13 @@ module Locomotive
raise ::Liquid::ArgumentError.new("Cannot paginate array '#{@collection_name}'. Not found.") if collection.nil?
pagination = collection.send(:paginate, {
:page => context['current_page'],
:per_page => @per_page }).to_liquid.stringify_keys
if collection.is_a? Array
pagination = Kaminari.paginate_array(collection).page(context['current_page']).per(@per_page).to_liquid.stringify_keys
else
pagination = collection.send(:paginate, {
:page => context['current_page'],
:per_page => @per_page }).to_liquid.stringify_keys
end
page_count, current_page = pagination['total_pages'], pagination['current_page']
path = context['path']