RegistrationsController#create deals with validation errors. Username, email, password now required on sign-up.

This commit is contained in:
Sarah Mei 2010-10-16 22:02:51 -07:00
parent 1b6726354f
commit d3a62c7a45
4 changed files with 27 additions and 2 deletions

View file

@ -7,13 +7,14 @@ class RegistrationsController < Devise::RegistrationsController
begin
user = User.instantiate!(params[:user])
rescue MongoMapper::DocumentNotValid => e
user = nil
flash[:error] = e.message
redirect_to new_user_registration_path
end
if user
if user.save
flash[:notice] = I18n.t 'registrations.create.success'
sign_in_and_redirect(:user, user)
else
flash[:error] = user.errors.full_messages.join(', ')
redirect_to new_user_registration_path
end
end

View file

@ -24,6 +24,7 @@ class User
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
key :username, :unique => true
key :serialized_private_key, String
@ -36,6 +37,8 @@ class User
key :visible_post_ids, Array
key :visible_person_ids, Array
validates_presence_of :username
one :person, :class_name => 'Person', :foreign_key => :owner_id
many :inviters, :in => :inviter_ids, :class_name => 'User'

View file

@ -35,5 +35,22 @@ describe RegistrationsController do
response.should redirect_to root_path
end
end
context "with invalid parameters" do
before do
@valid_params["user"].delete("username")
@invalid_params = @valid_params
end
it "does not create a user" do
lambda { get :create, @invalid_params }.should_not change(User, :count)
end
it "sets the flash error" do
get :create, @invalid_params
flash[:error].should_not be_blank
end
it "goes back to the form" do
get :create, @invalid_params
response.should redirect_to new_user_registration_path
end
end
end
end

View file

@ -13,6 +13,10 @@ describe User do
let(:aspect3) { user3.aspect(:name => 'stuff') }
describe "validations" do
it "requires a username" do
user = Factory.build(:user, :username => nil)
user.should_not be_valid
end
it "downcases the username" do
user = Factory.build(:user, :username => "ALLUPPERCASE")
user.valid?