diff --git a/Gemfile b/Gemfile index b0492ed..18ff84b 100644 --- a/Gemfile +++ b/Gemfile @@ -3,10 +3,12 @@ source 'http://rubygems.org' gem 'rails', '3.0.0.rc' gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3' gem 'gravatar_image_tag', '0.1.0' +gem 'will_paginate', '3.0.pre' group :development do gem 'rspec-rails', '2.0.0.beta.18' gem 'annotate-models', '1.0.4' + gem 'faker', '0.3.1' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 1517a26..77470ba 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,6 +39,7 @@ GEM factory_girl_rails (1.0) factory_girl (~> 1.3) rails (>= 3.0.0.beta4) + faker (0.3.1) gravatar_image_tag (0.1.0) i18n (0.4.1) mail (2.2.5) @@ -88,6 +89,7 @@ GEM nokogiri (>= 1.2.0) rack (>= 1.0) rack-test (>= 0.5.3) + will_paginate (3.0.pre) PLATFORMS ruby @@ -95,9 +97,11 @@ PLATFORMS DEPENDENCIES annotate-models (= 1.0.4) factory_girl_rails (= 1.0) + faker (= 0.3.1) gravatar_image_tag (= 0.1.0) rails (= 3.0.0.rc) rspec (= 2.0.0.beta.18) rspec-rails (= 2.0.0.beta.18) spork (= 0.8.4) sqlite3-ruby (= 1.2.5) + will_paginate (= 3.0.pre) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7e99f8e..4cb847b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,7 +3,7 @@ class UsersController < ApplicationController before_filter :correct_user, :only => [:edit, :update] def index - @users = User.all + @users = User.paginate(:page => params[:page]) @title = "All users" end diff --git a/app/views/users/_user.html.erb b/app/views/users/_user.html.erb new file mode 100644 index 0000000..fe808e7 --- /dev/null +++ b/app/views/users/_user.html.erb @@ -0,0 +1,4 @@ +
  • + <%= gravatar_for user, :size => 30 %> + <%= link_to user.name, user %> +
  • \ No newline at end of file diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 7346a87..d96cda3 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -1,10 +1,9 @@

    All users

    +<%= will_paginate %> + \ No newline at end of file + <%= render @users %> + + +<%= will_paginate %> diff --git a/lib/tasks/sample_data.rake b/lib/tasks/sample_data.rake new file mode 100644 index 0000000..73f0d80 --- /dev/null +++ b/lib/tasks/sample_data.rake @@ -0,0 +1,22 @@ +require 'faker' + +namespace :db do + desc "Fill database with sample data" + task :populate => :environment do + Rake::Task['db:reset'].invoke + User.create!(:name => "Example User", + :email => "example@railstutorial.org", + :password => "foobar", + :password_confirmation => "foobar") + 99.times do |n| + name = Faker::Name.name + email = "example-#{n+1}@railstutorial.org" + password = "password" + User.create!(:name => name, + :email => email, + :password => password, + :password_confirmation => password) + end + end + +end \ No newline at end of file diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 1171014..292d833 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -18,6 +18,10 @@ @user = test_sign_in(Factory(:user)) Factory(:user, :email => "another@example.com") Factory(:user, :email => "another@example.net") + + 30.times do + Factory(:user, :email => Factory.next(:email)) + end end it "should be successful" do @@ -32,10 +36,20 @@ it "should have an element for each user" do get :index - User.all.each do |user| + User.paginate(:page => 1).each do |user| response.should have_selector('li', :content => user.name) end end + + it "should paginate users" do + get :index + response.should have_selector('div.pagination') + response.should have_selector('span.disabled', :content => "Previous") + response.should have_selector('a', :href => "/users?page=2", + :content => "2") + response.should have_selector('a', :href => "/users?page=2", + :content => "Next") + end end end diff --git a/spec/factories.rb b/spec/factories.rb index 752f04b..b920a9e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -3,4 +3,8 @@ user.email "mhartl@example.com" user.password "foobar" user.password_confirmation "foobar" +end + +Factory.sequence :email do |n| + "person-#{n}@example.com" end \ No newline at end of file