Skip to content

Commit

Permalink
setup sample data, users index with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
paws committed Dec 13, 2011
1 parent 149c828 commit 4ae206f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -10,11 +10,15 @@ gem 'json'
#for the user profile pic
gem 'gravatar_image_tag'

#for paginating through long lists
gem 'will_paginate'

group :development do
gem 'rspec-rails'
#gem 'annotate'
gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'
gem 'sqlite3' #doesn't work on Heroku+cedar stack
gem 'faker'
end

group :test do
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -55,6 +55,8 @@ GEM
factory_girl_rails (1.3.0)
factory_girl (~> 2.2.0)
railties (>= 3.0.0)
faker (1.0.1)
i18n (~> 0.4)
gravatar_image_tag (1.0.0)
hike (1.2.1)
i18n (0.6.0)
Expand Down Expand Up @@ -137,6 +139,7 @@ GEM
nokogiri (>= 1.2.0)
rack (>= 1.0)
rack-test (>= 0.5.3)
will_paginate (3.0.2)

PLATFORMS
ruby
Expand All @@ -145,6 +148,7 @@ DEPENDENCIES
annotate!
coffee-rails (~> 3.1.0)
factory_girl_rails
faker
gravatar_image_tag
jquery-rails
json
Expand All @@ -157,3 +161,4 @@ DEPENDENCIES
sqlite3
uglifier
webrat
will_paginate
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Expand Up @@ -4,7 +4,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
@@ -0,0 +1,4 @@
<li>
<%= gravatar_for user, :size => 25 %>
<%= link_to user.name, user %>
</li>
20 changes: 14 additions & 6 deletions app/views/users/index.html.erb
@@ -1,10 +1,18 @@
<h1>All users</h1>

<%= will_paginate %>
<%# will_paginate in a given controller automatically looks for an instance variable of the same name %>
<ul class="users">
<% @users.each do |user| %>
<li>
<%= gravatar_for user, :size => 25 %>
<%= link_to user.name, user %>
</li>
<% end %>

<%# METHOD 3 - works as long as its enumerable (can run .each) %>
<%= render @users %>
<%# @users.each do |user| %>
<%# METHOD 1 %>
<%#= render 'user', :user => user #manually coding it %>
<%# METHOD 2 %>
<%#= render user %>
<%# yes, somehow rails knows to render the _user partial AND pass the user variable %>
<%# end %>
</ul>
<%= will_paginate %>
21 changes: 21 additions & 0 deletions lib/tasks/sample_data.rake
@@ -0,0 +1,21 @@
require 'faker'

namespace :db do
desc 'Fill database with sample data'
task :populate => :environment do
Rake::Task['db:reset'].invoke #runs rake db:reset to clear database
User.create!(:name => "Example User",
:email => "example@e.org",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "boring"
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
Expand Up @@ -17,6 +17,11 @@
@user = test_sign_in(Factory(:user))
Factory(:user, :email => "user@example2.com")
Factory(:user, :email => "darth@vader.com")

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

end

it "should allow access" do
Expand All @@ -31,10 +36,19 @@

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
Expand Up @@ -4,3 +4,7 @@
user.password "foobar"
user.password_confirmation "foobar"
end

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

0 comments on commit 4ae206f

Please sign in to comment.