Skip to content
Browse files

Blog posts now paginate with Kaminari

  • Loading branch information...
1 parent 9df8dbc commit 783f7291ae56b2c559dfa0dc239c2d930004ee21 @mchung committed Dec 5, 2011
View
13 Gemfile.lock
@@ -2,6 +2,7 @@ PATH
remote: .
specs:
active_blog (0.0.2)
+ kaminari (~> 0.12.4)
rails (~> 3.1.1)
redcarpet (~> 2.0.0b5)
@@ -41,16 +42,18 @@ GEM
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.0)
- jquery-rails (1.0.18)
+ jquery-rails (1.0.19)
railties (~> 3.0)
thor (~> 0.14)
- json (1.6.1)
+ json (1.6.3)
+ kaminari (0.12.4)
+ rails (>= 3.0.0)
mail (2.3.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.17.2)
- multi_json (1.0.3)
+ multi_json (1.0.4)
polyglot (0.3.3)
rack (1.3.5)
rack-cache (1.1)
@@ -79,12 +82,12 @@ GEM
rake (0.9.2.2)
rdoc (3.11)
json (~> 1.4)
- redcarpet (2.0.0b5)
+ redcarpet (2.0.0)
sprockets (2.0.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
- sqlite3 (1.3.4)
+ sqlite3 (1.3.5)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
View
1 active_blog.gemspec
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.add_dependency "rails", "~> 3.1.1"
s.add_dependency "redcarpet", "~> 2.0.0b5"
+ s.add_dependency "kaminari", "~> 0.12.4"
s.add_development_dependency "sqlite3"
end
View
2 app/controllers/active_blog/blog_posts_controller.rb
@@ -7,7 +7,7 @@ class BlogPostsController < ApplicationController
end
def index
- @blog_posts = BlogPost.live.order('published_at DESC')
+ @blog_posts = BlogPost.live.order('published_at DESC').page(params[:page])
end
def show
View
1 app/models/active_blog/blog_post.rb
@@ -1,6 +1,7 @@
module ActiveBlog
class BlogPost < ActiveRecord::Base
set_table_name 'active_blog_blog_posts'
+ paginates_per ActiveBlog.paginates_per
scope :live, where('published_at < ? AND draft = ?', Time.now, false)
scope :recent, limit(5)
View
2 app/views/active_blog/blog_posts/index.html.erb
@@ -15,6 +15,8 @@
<div itemprop="blogPosts">
<%= render :partial => "blog_post", :collection => @blog_posts, :locals => {:show => false} %>
</div>
+
+ <%= paginate @blog_posts %>
</div>
<%= render :partial => "sidebar" %>
View
4 lib/active_blog.rb
@@ -29,6 +29,10 @@ module ActiveBlog
mattr_accessor :author_email
@@author_email = nil
+ # Blog posts per page
+ mattr_accessor :paginates_per
+ @@paginates_per = 10
+
def self.setup
yield self
end
View
13 lib/active_blog/mapper.rb
@@ -5,10 +5,17 @@ def active_blog_at(mount_location)
#
#
scope mount_location do
- match '/' => 'active_blog/blog_posts#index', :as => :active_blog
+ resources :blog_posts, :module => 'active_blog', :path => '/', :only => [] do
+ get 'page/:page', :action => :index, :on => :collection
+ # match '/', :action => :index, :on => :collection, :as => :all
+ # match '/-/preview', :action => :preview, :on => :collection, :as => :preview, :via => [:post, :put]
+ # match '/-/feed', :action => :feed, :on => :collection, :as => :feed, :via => [:get]
+ # match '/:cached_slug', :action => :show, :on => :collection, :as => :post, :via => [:get]
+ end
+ match '/' => 'active_blog/blog_posts#index', :as => :active_blog, :via => [:get]
match '/-/preview' => 'active_blog/blog_posts#preview', :as => :active_blog_preview, :via => [:post, :put]
- match '/-/feed' => 'active_blog/blog_posts#feed', :as => :active_blog_feed, :via => :get
- match '/:cached_slug' => 'active_blog/blog_posts#show', :as => :active_blog_post
+ match '/-/feed' => 'active_blog/blog_posts#feed', :as => :active_blog_feed, :via => [:get]
+ match '/:cached_slug' => 'active_blog/blog_posts#show', :as => :active_blog_post, :via => [:get]
end
end
end
View
3 lib/generators/active_blog/install/templates/active_blog.rb
@@ -16,4 +16,7 @@
# Assuming your layout has a sidebar, active_blog will yield via content_for.
# config.content_for_sidebar = :sidebar
+
+ # Blog posts per page
+ # config.paginates_per = 10
end
View
1 test/dummy/config/application.rb
@@ -4,6 +4,7 @@
Bundler.require
require "active_blog"
+require "kaminari" # Load this manually.
module Dummy
class Application < Rails::Application
View
25 test/functional/active_blog/routing_test.rb
@@ -0,0 +1,25 @@
+require 'test_helper'
+
+module ActiveBlog
+ class RoutingTest < ActionController::TestCase
+ # Tests the following routes:
+ #
+ # active_blog /blog(.:format)
+ # active_blog_preview POST|PUT /blog/-/preview(.:format) {:controller=>"active_blog/blog_posts", :action=>"preview"}
+ # active_blog_feed GET /blog/-/feed(.:format) {:controller=>"active_blog/blog_posts", :action=>"feed"}
+ # active_blog_post /blog/:cached_slug(.:format) {:controller=>"active_blog/blog_posts", :action=>"show"}
+
+ test "should route /blog" do
+ assert_routing '/blog', {:controller=>"active_blog/blog_posts", :action=>"index"}
+ end
+ test "should route /blog/-/preview" do
+ assert_routing({:path => '/blog/-/preview', :method => :post}, {:controller=>"active_blog/blog_posts", :action=>"preview"})
+ end
+ test "should route /blog/-/feed" do
+ assert_routing '/blog/-/feed', {:controller=>"active_blog/blog_posts", :action=>"feed"}
+ end
+ test "should route /blog/:cached_slug" do
+ assert_routing '/blog/my-blog-post-title', {:controller=>"active_blog/blog_posts", :action=>"show", :cached_slug=>"my-blog-post-title"}
+ end
+ end
+end

0 comments on commit 783f729

Please sign in to comment.
Something went wrong with that request. Please try again.