Skip to content

Commit

Permalink
Implement Users#create
Browse files Browse the repository at this point in the history
  • Loading branch information
rosylilly committed Jan 26, 2013
1 parent 5b21238 commit 04520ef
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
12 changes: 12 additions & 0 deletions examples/auction/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
class UsersController < ApplicationController
def create
@user = User.new(params[:user])
@user.password = params[:password]
@user.password_confirmation = params[:password_confirmation]

@user.save!
session[:user_id] = @user.id

redirect_to root_path
rescue ActiveRecord::RecordInvalid
render 'authentication/sign_up'
end
end
8 changes: 7 additions & 1 deletion examples/auction/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
class User < ActiveRecord::Base
attr_accessible :assets, :name
attr_accessible :assets, :name, :email

validates_presence_of :name
validates_presence_of :email
validates_uniqueness_of :email
validates_presence_of :password_confirmation, if: :password_changed?
validates_confirmation_of :password

def password
@password ||= BCrypt::Password.new(read_attribute(:password))
Expand Down
17 changes: 14 additions & 3 deletions examples/auction/app/views/authentication/sign_up.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@
h1 Sign In

= form_for @user, html: { class: 'form-horizontal' } do |f|
.control-group
- if @user.errors.present?
.alert.alert-error
h4.alert-heading
#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:
ul
- @user.errors.full_messages.each do |message|
li= message
.control-group class="#{@user.errors[:name].empty? ? '' : 'error'}"
label.control-label for='user_name' Name
.controls
= f.text_field :name, class: 'input-block-level'
.control-group
.control-group class="#{@user.errors[:email].empty? ? '' : 'error'}"
label.control-label for='user_email' E-Mail
.controls
= f.text_field :email, class: 'input-block-level'
.control-group
.control-group class="#{@user.errors[:password].empty? ? '' : 'error'}"
label.control-label for='user_password' Password
.controls
= password_field_tag :password, '', class: 'input-block-level'
.control-group class="#{@user.errors[:password_confirmation].empty? ? '' : 'error'}"
label.control-label for='user_password' Password(Confirm)
.controls
= password_field_tag :password_confirmation, '', class: 'input-block-level'
.form-actions
= submit_tag 'Sign In', class: 'btn btn-primary'
25 changes: 25 additions & 0 deletions examples/auction/spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
require 'spec_helper'

describe UsersController do
describe 'POST create' do
subject { post :create, params }

context 'when valid params' do
let(:password) { Forgery(:basic).password }
let(:params) do
{
user: {
name: Forgery(:name).full_name,
email: Forgery(:internet).email_address
},
password: password,
password_confirmation: password
}
end

it { should redirect_to(root_path) }
it { expect { subject }.to change { User.count }.from(0).to(1) }
end

context 'when invalid params' do
let(:params) { {} }

it { should render_template('authentication/sign_up') }
end
end
end
1 change: 1 addition & 0 deletions examples/auction/spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
email { Forgery::Internet.email_address }
assets 0
password { Forgery::Basic.password }
password_confirmation {|u| u.password }
end
end

0 comments on commit 04520ef

Please sign in to comment.