Created
September 20, 2009 02:03
-
-
Save linojon/189671 to your computer and use it in GitHub Desktop.
code snippets used in the AuthApp screencast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# File: features/support/path.rb | |
when /login/ | |
login_path | |
when /signup/ | |
signup_path | |
# File: features/support/user_helpers.rb | |
module UserHelpers | |
def create_user( options = {} ) | |
args = { | |
:username => 'subscriber', | |
:password => 'secret', | |
:password_confirmation => 'secret', | |
}.merge( options ) | |
args[:email] ||= "#{args[:username]}@example.com" | |
user = User.create!(args) | |
# :create syntax for restful_authentication w/ aasm. Tweak as needed. | |
# user.activate! | |
user | |
end | |
def log_in_as( username ) | |
visit "/login" | |
fill_in("user_session_username", :with => username) | |
fill_in("password", :with => 'secret') | |
click_button("Log in") | |
end | |
end | |
World(UserHelpers) | |
# File: features/user.feature | |
Feature: User authentication | |
In order to access the site | |
As a user | |
I must register and log in | |
Scenario: Register as new user | |
Given I am on the homepage | |
When I follow "Sign up!" | |
Then I should be on the signup page | |
When I fill in the following: | |
| username | subscriber | | |
| email | [email protected] | | |
| password | secret | | |
| confirm password | secret | | |
And I press "Sign up" | |
Then I should be on the homepage | |
And I should see "Thank you for signing up! You are now logged in." | |
Scenario: Edit profile | |
Given a user is logged in as "subscriber" | |
And I am on the homepage | |
When I follow "My profile" | |
Then I should see "Show User Profile" | |
When I follow "Edit Profile" | |
Then I should see "Edit User Profile" | |
When I fill in "email" with "[email protected]" | |
And I press "Submit" | |
Then I should be on the homepage | |
And I should see "Successfully updated user profile." | |
# File: app/layouts/_user_nav.html.erb | |
<div id="user_nav"> | |
<%= link_to "Home", root_path %> | | |
<% if current_user %> | |
<%= link_to "My profile", user_path(:current) %> | | |
<%= link_to "Logout", logout_path %> | |
<% else %> | |
<%= link_to "Sign up", signup_path %> | | |
<%= link_to "Login", login_path %> | |
<% end %> | |
</div> | |
# File: app/controllers/users_controller.rb | |
class UsersController < ApplicationController | |
before_filter :login_required, :except => [:new, :create] | |
before_filter :find_user, :except => [:new, :create] | |
def new | |
@user = User.new | |
end | |
def create | |
@user = User.new(params[:user]) | |
if @user.save | |
flash[:notice] = "Thank you for signing up! You are now logged in." | |
redirect_to root_url | |
else | |
render :action => 'new' | |
end | |
end | |
def show | |
end | |
def edit | |
end | |
def update | |
if @user.update_attributes(params[:user]) | |
flash[:notice] = "Successfully updated user profile." | |
redirect_to root_url | |
else | |
render :action => 'edit' | |
end | |
end | |
private | |
def find_user | |
@user = current_user | |
end | |
end | |
# File: app/views/users/show.html.erb | |
<% title "Show User Profile" %> | |
<p> | |
<strong>Username:</strong> | |
<%=h @user.username %> | |
</p> | |
<p> | |
<strong>Email:</strong> | |
<%=h @user.email %> | |
</p> | |
<p> | |
<%= link_to "Edit Profile", edit_user_path(@user) %> | |
</p> | |
# File: app/views/users/edit.html.erb | |
<% title "Edit User Profile" %> | |
<% form_for @user do |f| %> | |
<%= f.error_messages %> | |
<p> | |
<%= f.label :email %><br /> | |
<%= f.text_field :email %> | |
</p> | |
<p> | |
<%= f.label :password %><br /> | |
<%= f.password_field :password, :value => nil, :autocomplete => 'off' %> | |
</p> | |
<p> | |
<%= f.label :password_confirmation %><br /> | |
<%= f.password_field :password_confirmation %> | |
</p> | |
<p><%= f.submit "Submit" %></p> | |
<p><%= link_to "Cancel", root_url %> | |
<% end %> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment