From ee42ebbf1bbb5e1760170a678fc2dd5d710c7653 Mon Sep 17 00:00:00 2001 From: gnattery Date: Wed, 20 Mar 2013 15:27:54 +1100 Subject: [PATCH] paginate posts --- app/controllers/posts_controller.rb | 2 +- app/views/posts/index.html.haml | 6 ++++-- spec/views/posts/index.html.haml_spec.rb | 18 ++++++++++-------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index f78a476c6..ff70d5aae 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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| diff --git a/app/views/posts/index.html.haml b/app/views/posts/index.html.haml index b16bd5d26..846aa00e2 100644 --- a/app/views/posts/index.html.haml +++ b/app/views/posts/index.html.haml @@ -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 } diff --git a/spec/views/posts/index.html.haml_spec.rb b/spec/views/posts/index.html.haml_spec.rb index 43b24b006..e95b1285a 100644 --- a/spec/views/posts/index.html.haml_spec.rb +++ b/spec/views/posts/index.html.haml_spec.rb @@ -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