public
Last active

code snippets used in the AuthApp screencast

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
# 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 | subscriber@example.com |
| 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 "changed@example.com"
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 %>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.