Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add pagination using Kaminari.

  • Loading branch information...
commit 43c8fdea7f3a585ec28c1029c8578d00b89f2f86 1 parent d40786a
Jason Weathered and Odin Dutton authored twe4ked committed
View
12 app/controllers/posts_controller.rb
@@ -21,7 +21,17 @@ def resource
helper_method :resource
def collection
- @collection ||= Post.where(params.slice(:year, :month, :day))
+ @collection ||= begin
+ posts = Post.where(params.slice(:year, :month, :day))
+ posts = Kaminari.paginate_array(posts).page(params[:page]).per(posts_per_page)
+ posts
+ end
end
helper_method :collection
+
+ private
+
+ def posts_per_page
+ params[:count] || Postmarkdown::Config.options[:posts_per_page]
+ end
end
View
1  app/views/posts/index.html.haml
@@ -3,5 +3,6 @@
%section#posts
- if collection.present?
= render collection, :summary => true
+ = paginate collection
- else
No posts found.
View
2  lib/postmarkdown/config.rb
@@ -11,6 +11,8 @@ module Config
Postmarkdown::Config.options[:permalink_format] = :day
+Postmarkdown::Config.options[:posts_per_page] = 5
+
Postmarkdown::Config.options[:use_theme] = false
Postmarkdown::Config.options[:permalink_regex] = {}
View
1  lib/postmarkdown/engine.rb
@@ -5,6 +5,7 @@ module Postmarkdown
require 'rdiscount'
require 'nokogiri'
require 'haml'
+ require 'kaminari'
class Engine < Rails::Engine
end
View
1  postmarkdown.gemspec
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
s.add_dependency 'gravtastic'
s.add_dependency 'nokogiri'
s.add_dependency 'rdiscount'
+ s.add_dependency 'kaminari'
s.add_development_dependency 'appraisal'
s.add_development_dependency 'rspec-rails', '~> 2.5'
View
1  readme.md
@@ -137,7 +137,6 @@ What about mapping Postmarkdown to root? We got you covered:
* Syntax highlighting for code blocks
* Generated routes should show example usage
* Support more file formats, eg. textile
-* Pagination
* Built-in theme should have a link to the RSS Feed
* Generator tests
View
25 spec/integrations/posts_spec.rb
@@ -45,6 +45,31 @@
all('section#posts article.post').size.should == 4
end
end
+
+ describe 'pagination' do
+ def article_titles
+ all('article header h1').map(&:text)
+ end
+
+ before do
+ visit posts_path(:count => 2)
+ end
+
+ it 'returns the latest posts on the first page' do
+ article_titles.should eq [
+ 'Post with full metadata',
+ 'A Test Post',
+ ]
+ end
+
+ it 'returns earlier posts on the second page' do
+ click_link 'Next'
+ article_titles.should eq [
+ 'Image',
+ 'First Post',
+ ]
+ end
+ end
end
context 'Posts#index with no posts' do
View
2  vendor/assets/stylesheets/postmarkdown/postmarkdown.css
@@ -63,5 +63,7 @@ section#content article header h1{}
section#content article header img{display:none;}
section#content article header .meta{color:#999;font-size:12px;}
+nav.pagination{margin-bottom:20px;}
+
/* -- Footer ---------------------------------------------------------------- */
footer{margin-bottom:20px;}
Please sign in to comment.
Something went wrong with that request. Please try again.