Skip to content

Commit

Permalink
Users index with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartl committed Aug 29, 2010
1 parent 8e52fe8 commit 3212b97
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -88,16 +89,19 @@ GEM
nokogiri (>= 1.2.0)
rack (>= 1.0)
rack-test (>= 0.5.3)
will_paginate (3.0.pre)

PLATFORMS
ruby

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)
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
</li>
13 changes: 6 additions & 7 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
<% @users.each do |user| %>
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>
<%= render @users %>
</ul>

<%= will_paginate %>
22 changes: 22 additions & 0 deletions lib/tasks/sample_data.rake
Original file line number Diff line number Diff line change
@@ -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 => "[email protected]",
: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
16 changes: 15 additions & 1 deletion spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
@user = test_sign_in(Factory(:user))
Factory(:user, :email => "[email protected]")
Factory(:user, :email => "[email protected]")

30.times do
Factory(:user, :email => Factory.next(:email))
end
end

it "should be successful" do
Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
user.email "[email protected]"
user.password "foobar"
user.password_confirmation "foobar"
end

Factory.sequence :email do |n|
"person-#{n}@example.com"
end

0 comments on commit 3212b97

Please sign in to comment.