paginate posts

This commit is contained in:
gnattery
2013-03-20 15:27:54 +11:00
parent 793b25e3dd
commit ee42ebbf1b
3 changed files with 15 additions and 11 deletions

View File

@@ -4,7 +4,7 @@ class PostsController < ApplicationController
# GET /posts.json
def index
@posts = Post.all
@posts = Post.paginate(:page => params[:page])
@recent_posts = Post.limit(100).order('created_at desc').all
respond_to do |format|

View File

@@ -1,8 +1,10 @@
= content_for :title, "Recent #{Growstuff::Application.config.site_name} member posts"
%p= link_to 'Post something', new_post_path, :class => 'btn'
%p= link_to 'Post something', new_post_path, :class => 'btn btn-primary'
%p= "Displaying #{@posts.length} posts"
%div.pagination
= page_entries_info @posts, :model => "posts"
= will_paginate @posts
- @posts.each do |post|
= render :partial => "single", :locals => { :post => post, :subject => true }

View File

@@ -4,10 +4,16 @@ describe "posts/index" do
before(:each) do
controller.stub(:current_user) { nil }
@author = FactoryGirl.create(:member)
# We use create (= build+save) to generate slugs and hence paths for posts
@post1 = FactoryGirl.create(:post, :author => @author)
@post2 = FactoryGirl.create(:post, :author => @author)
assign(:posts, [@post1, @post2])
page = 1
per_page = 2
total_entries = 2
posts = WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
pager.replace([
FactoryGirl.create(:post, :author => @author),
FactoryGirl.create(:post, :author => @author)
])
end
assign(:posts, posts)
render
end
@@ -18,10 +24,6 @@ describe "posts/index" do
:text => "This is some text.".to_s, :count => 2
end
it "counts the number of posts" do
rendered.should contain "Displaying 2 posts"
end
it "contains two gravatar icons" do
assert_select "img", :src => /gravatar\.com\/avatar/, :count => 2
end