Commit f80e87b
Changed files (10)
app
controllers
models
views
registrations
sessions
config
db
spec
controllers
models
app/controllers/application_controller.rb
@@ -2,4 +2,10 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
+
+ protected
+
+ def log_in(user)
+ session[:user_id] = user.id
+ end
end
app/controllers/registrations_controller.rb
@@ -2,4 +2,15 @@ class RegistrationsController < ApplicationController
def new
@user = User.new
end
+
+ def create
+ log_in(User.create!(secure_params))
+ redirect_to dashboard_path
+ end
+
+ private
+
+ def secure_params
+ params.require(:user).permit(:username, :email, :password)
+ end
end
app/models/user.rb
@@ -1,6 +1,3 @@
-class User
- include ActiveModel::Model
-
- attr_accessor :username, :email
-
+class User < ActiveRecord::Base
+ attr_accessor :terms_and_conditions, :password
end
app/views/registrations/new.html.erb
@@ -15,6 +15,11 @@
<%= f.password_field :password, placeholder: "Password" %>
</label>
</div> <!-- /.small-12 -->
+ <div class="small-12 columns">
+ <label>I Agree
+ <%= f.check_box :terms_and_conditions %>
+ </label>
+ </div> <!-- /.small-12 -->
<div class="small-12 columns">
<%= f.submit "Register", class: "button" %>
</div> <!-- /.small-12 -->
app/views/sessions/new.html.erb
@@ -1,1 +1,1 @@
-<%= link_to "Register", new_registration_path, class: "button secondary" %>
\ No newline at end of file
+<%= link_to "Register", new_registration_path, class: "button secondary" %>
config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
root 'sessions#new'
resources :registrations, only: [:new, :create]
+ get '/' => 'sessions#new', as: :dashboard
end
db/migrate/20150103150805_create_users.rb
@@ -0,0 +1,10 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ enable_extension 'uuid-ossp'
+
+ create_table :users, id: :uuid do |t|
+ t.string :username, null: false
+ t.string :email, null: false
+ end
+ end
+end
db/schema.rb
@@ -11,9 +11,15 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 0) do
+ActiveRecord::Schema.define(version: 20150103150805) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
+ enable_extension "uuid-ossp"
+
+ create_table "users", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
+ t.string "username", null: false
+ t.string "email", null: false
+ end
end
spec/controllers/registrations_controller_spec.rb
@@ -7,4 +7,30 @@ describe RegistrationsController do
expect(assigns(:user)).to be_new_record
end
end
+
+ describe "#create" do
+ let(:username) { 'username' }
+ let(:password) { 'password' }
+ let(:email) { 'email@example.com' }
+
+ it 'creates a new user account' do
+ post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
+
+ expect(User.count).to eql(1)
+ first_user = User.first
+ expect(first_user.username).to eql(username)
+ expect(first_user.email).to eql(email)
+ end
+
+ it 'redirects them to the dashboard' do
+ post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
+ expect(response).to redirect_to(dashboard_path)
+ end
+
+ it 'logs them in' do
+ post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
+
+ expect(session[:user_id]).to eql(User.first.id)
+ end
+ end
end
spec/models/user_spec.rb
@@ -0,0 +1,14 @@
+require 'rails_helper'
+
+describe User do
+ describe "#create" do
+ it 'saves a new user to the database' do
+ user = User.create!(username: 'blah', email: 'blah@example.com', password: 'password')
+
+ saved_user = User.find(user.id)
+ expect(saved_user.username).to eql('blah')
+ expect(saved_user.email).to eql('blah@example.com')
+ expect(saved_user.password).to be_nil
+ end
+ end
+end