-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing Registerable module, allowing users to sign up.
- Loading branch information
1 parent
4de1e43
commit 6b837cb
Showing
17 changed files
with
173 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class RegistrationsController < ApplicationController | ||
include Devise::Controllers::InternalHelpers | ||
include Devise::Controllers::Common | ||
|
||
# POST /resource/registration | ||
def create | ||
self.resource = resource_class.new(params[resource_name]) | ||
|
||
if resource.save | ||
# Attempt to sign the resource in. When there is no other thing blocking | ||
# the resource (ie confirmations), then the resource will be signed in, | ||
# otherwise the specific message is shown and the resource will be | ||
# redirected to the sign in page. | ||
sign_in(resource_name, resource) | ||
# At this time the resource has signed in and no hook has signed it out. | ||
set_flash_message :notice, :signed_up | ||
sign_in_and_redirect(resource_name, resource, true) | ||
else | ||
render_with_scope :new | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<h2>Sign up</h2> | ||
|
||
<%- if devise_mapping.registerable? %> | ||
<% form_for resource_name, resource, :url => registration_path(resource_name) do |f| -%> | ||
<%= f.error_messages %> | ||
<p><%= f.label :email %></p> | ||
<p><%= f.text_field :email %></p> | ||
|
||
<p><%= f.label :password %></p> | ||
<p><%= f.password_field :password %></p> | ||
|
||
<p><%= f.label :password_confirmation %></p> | ||
<p><%= f.password_field :password_confirmation %></p> | ||
|
||
<p><%= f.submit "Register" %></p> | ||
<% end -%> | ||
<% end%> | ||
<%= render :partial => "shared/devise_links" %> |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Devise | ||
module Models | ||
# Registerable is responsible for everything related to registering a new | ||
# resource (ie account or sign up). | ||
module Registerable | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'test/test_helper' | ||
|
||
class RegistrationTest < ActionController::IntegrationTest | ||
|
||
test 'a guest admin should be able to sign in successfully' do | ||
visit new_admin_session_path | ||
click_link 'Sign up' | ||
|
||
assert_template 'registrations/new' | ||
|
||
fill_in 'email', :with => 'new_user@test.com' | ||
fill_in 'password', :with => 'new_user123' | ||
fill_in 'password confirmation', :with => 'new_user123' | ||
click_button 'Register' | ||
|
||
assert_contain 'You have signed up successfully.' | ||
assert warden.authenticated?(:admin) | ||
|
||
admin = Admin.last | ||
assert_equal admin.email, 'new_user@test.com' | ||
end | ||
|
||
test 'a guest user should be able to sign up successfully and be blocked by confirmation' do | ||
visit new_user_session_path | ||
click_link 'Sign up' | ||
|
||
assert_template 'registrations/new' | ||
|
||
fill_in 'email', :with => 'new_user@test.com' | ||
fill_in 'password', :with => 'new_user123' | ||
fill_in 'password confirmation', :with => 'new_user123' | ||
click_button 'Register' | ||
|
||
follow_redirect! | ||
|
||
assert_contain 'You have to confirm your account before continuing.' | ||
assert_not warden.authenticated?(:user) | ||
|
||
user = User.last | ||
assert_equal user.email, 'new_user@test.com' | ||
end | ||
|
||
test 'a guest user cannot sign up with invalid information' do | ||
visit new_user_session_path | ||
click_link 'Sign up' | ||
|
||
fill_in 'email', :with => 'invalid_email' | ||
fill_in 'password', :with => 'new_user123' | ||
fill_in 'password confirmation', :with => 'new_user321' | ||
click_button 'Register' | ||
|
||
assert_template 'registrations/new' | ||
assert_have_selector '#errorExplanation' | ||
assert_contain "Email is invalid" | ||
assert_contain "Password doesn't match confirmation" | ||
assert_nil User.first | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
class User < ActiveRecord::Base | ||
devise :authenticatable, :confirmable, :recoverable, :rememberable, :trackable, | ||
:validatable, :timeoutable, :lockable, :token_authenticatable | ||
devise :authenticatable, :confirmable, :lockable, :recoverable, | ||
:registerable, :rememberable, :timeoutable, :token_authenticatable, | ||
:trackable, :validatable | ||
|
||
attr_accessible :username, :email, :password, :password_confirmation | ||
end |
This file contains 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
This file contains 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
6b837cb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YES! =D