Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

User model with passwords, and user show page

  • Loading branch information...
commit e435042482df5027039a820b8f01aa78f59ad5b6 1 parent 12f36c1
@almakdad authored
View
2  Gemfile
@@ -1,6 +1,7 @@
source 'http://rubygems.org'
gem 'rails', '3.0.6.rc2'
+gem 'gravatar_image_tag', '1.0.0.pre2'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
@@ -16,6 +17,7 @@ group :test do
gem 'rspec', '2.5.0'
gem 'webrat', '0.7.1'
gem 'spork', '0.9.0.rc4'
+ gem 'factory_girl_rails', '1.0'
end
# Use unicorn as the web server
View
7 Gemfile.lock
@@ -34,6 +34,11 @@ GEM
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
+ factory_girl (1.3.3)
+ factory_girl_rails (1.0)
+ factory_girl (~> 1.3)
+ rails (>= 3.0.0.beta4)
+ gravatar_image_tag (1.0.0.pre2)
i18n (0.5.0)
mail (2.2.15)
activesupport (>= 2.3.6)
@@ -91,6 +96,8 @@ PLATFORMS
DEPENDENCIES
annotate-models (= 1.0.4)
+ factory_girl_rails (= 1.0)
+ gravatar_image_tag (= 1.0.0.pre2)
mysql2
rails (= 3.0.6.rc2)
rspec (= 2.5.0)
View
1  app/controllers/users_controller.rb
@@ -2,6 +2,7 @@ class UsersController < ApplicationController
def show
@user = User.find(params[:id])
+ @title = @user.name
end
def signup
View
6 app/helpers/users_helper.rb
@@ -1,2 +1,8 @@
module UsersHelper
+
+ def gravatar_for(user, options = { :size => 50 })
+ gravatar_image_tag(user.email.downcase, :alt => user.name,
+ :class => 'gravatar',
+ :gravatar => options)
+ end
end
View
6 app/models/user.rb
@@ -36,6 +36,12 @@ def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
+ def self.authenticate(email, submitted_password)
+ user = find_by_email(email)
+ return nil if user.nil?
+ return user if user.has_password?(submitted_password)
+ end
+
private
def encrypt_password
View
15 app/views/users/show.html.erb
@@ -1 +1,14 @@
-<%= @user.name %>, <%= @user.email %>
+<table class="profile" summary="Profile information">
+ <tr>
+ <td class="main">
+ <h1>
+ <%= gravatar_for @user %>
+ <%= @user.name %>
+ </h1>
+ </td>
+ <td class="sidebar round">
+ <strong>Name</strong> <%= @user.name %><br />
+ <strong>URL</strong> <%= link_to user_path(@user), @user %>
+ </td>
+ </tr>
+</table>
View
24 public/stylesheets/custom.css
@@ -114,4 +114,28 @@ footer {
footer nav {
float: none;
+}
+
+/* User show page */
+
+table.profile {
+ width: 100%;
+ margin-bottom: 0;
+}
+
+td.main {
+ width: 70%;
+ padding: 1em;
+}
+
+td.sidebar {
+ width: 30%;
+ padding: 1em;
+ vertical-align: top;
+ background: #ffc;
+}
+
+.profile img.gravatar {
+ border: 1px solid #999;
+ margin-bottom: -15px;
}
View
33 spec/controllers/users_controller_spec.rb
@@ -3,6 +3,39 @@
describe UsersController do
render_views
+ describe "GET 'show'" do
+
+ before(:each) do
+ @user = Factory(:user)
+ end
+
+ it "should be successful" do
+ get :show, :id => @user
+ response.should be_success
+ end
+
+ it "should find the right user" do
+ get :show, :id => @user
+ assigns(:user).should == @user
+ end
+
+ it "should have the right title" do
+ get :show, :id => @user
+ response.should have_selector("title", :content => @user.name)
+ end
+
+ it "should include the user's name" do
+ get :show, :id => @user
+ response.should have_selector("h1", :content => @user.name)
+ end
+
+ it "should have a profile image" do
+ get :show, :id => @user
+ response.should have_selector("h1>img", :class => "gravatar")
+ end
+
+ end
+
describe "GET 'signup'" do
it "should be successful" do
get 'signup'
View
7 spec/factories.rb
@@ -0,0 +1,7 @@
+# By using the symbol ':user', we get Factory Girl to simulate the User model.
+Factory.define :user do |user|
+ user.name "Michael Hartl"
+ user.email "mhartl@example.com"
+ user.password "foobar"
+ user.password_confirmation "foobar"
+end
View
18 spec/models/user_spec.rb
@@ -107,5 +107,23 @@
end
end
+ describe "authenticate method" do
+
+ it "should return nil on email/password mismatch" do
+ wrong_password_user = User.authenticate(@attr[:email], "wrongpass")
+ wrong_password_user.should be_nil
+ end
+
+ it "should return nil for an email address with no user" do
+ nonexistent_user = User.authenticate("bar@foo.com", @attr[:password])
+ nonexistent_user.should be_nil
+ end
+
+ it "should return the user on email/password match" do
+ matching_user = User.authenticate(@attr[:email], @attr[:password])
+ matching_user.should == @user
+ end
+ end
+
end
end
View
12 webrat.log
@@ -59,3 +59,15 @@ REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
+REQUESTING PAGE: GET / with {} and HTTP headers {}
+REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
+REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
+REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
+REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
+REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
+REQUESTING PAGE: GET / with {} and HTTP headers {}
+REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
+REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
+REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
+REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
+REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
Please sign in to comment.
Something went wrong with that request. Please try again.