Permalink
Browse files

User model, done till 6.3.4

  • Loading branch information...
Hanif, Hakimuddin
Hanif, Hakimuddin committed May 17, 2014
1 parent 33e9540 commit 29628e1ce9c59e64fcf87c6e68ec0c3a0b820b6a
View
@@ -6,6 +6,8 @@ gem 'rails', '4.0.5'
gem 'bootstrap-sass', '3.1.1'
+gem 'bcrypt-ruby', '3.1.2'
+
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
View
@@ -26,6 +26,7 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
arel (4.0.2)
+ bcrypt-ruby (3.1.2)
bootstrap-sass (3.1.1.0)
sass (~> 3.2)
builder (3.1.4)
@@ -188,6 +189,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ bcrypt-ruby (= 3.1.2)
bootstrap-sass (= 3.1.1)
capybara (= 2.1.0)
coffee-rails (= 4.0.1)
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
@@ -0,0 +1,3 @@
+// Place all the styles related to the Users controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,4 @@
+class UsersController < ApplicationController
+ def new
+ end
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+end
View
@@ -0,0 +1,11 @@
+class User < ActiveRecord::Base
+ before_save { self.email = email.downcase }
+
+ validates :name, presence:true, length: { maximum: 50 }
+
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
+ validates :email, presence:true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive:false }
+
+ has_secure_password
+ validates :password, length: { maximum: 10 }
+end
@@ -0,0 +1,3 @@
+<% provide(:title, 'Sign up') %>
+<h1>Sign up</h1>
+<p>Find me in app/views/users/new.html.erb</p>
@@ -0,0 +1,11 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+
+ create_table :users do |t|
+ t.string :name
+ t.string :email
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,5 @@
+class AddIndexToUsersEmail < ActiveRecord::Migration
+ def change
+ add_index :users, :email, unique: true
+ end
+end
@@ -0,0 +1,5 @@
+class AddPasswordDigetsToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :password_digest, :string
+ end
+end
View
@@ -0,0 +1,27 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema.define(version: 20140516033923) do
+
+ create_table "users", force: true do |t|
+ t.string "name"
+ t.string "email"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "passord_digest"
+ t.string "password_digest"
+ end
+
+ add_index "users", ["email"], name: "index_users_on_email", unique: true
+
+end
View
@@ -0,0 +1,83 @@
+require 'spec_helper'
+
+describe User do
+ before { @user = User.new(name: "Hakim", email:"h@haha.com", password: "password", password_confirmation: "password") }
+
+ subject { @user }
+
+ it { should respond_to(:name) }
+ it { should respond_to(:email) }
+ it { should respond_to(:password) }
+ it { should respond_to(:password_confirmation) }
+ it { should respond_to(:password_digest) }
+
+ it { should be_valid }
+
+ describe "when password and confirmation is blank" do
+ before do
+ @user.password = " "
+ @user.password_confirmation = " "
+ end
+ it { should_not be_valid }
+ end
+
+ describe "when password confirmation does not match" do
+ before { @user.password_confirmation = "mismatch" }
+ it { should_not be_valid }
+ end
+
+ describe "when password is too long" do
+ before do
+ @user.password = "p" * 11
+ end
+ it { should_not be_valid }
+ end
+
+ describe "when name is not present" do
+ before { @user.name = " " }
+ it { should_not be_valid }
+ end
+
+ describe "when name is too long" do
+ before { @user.name = "a" * 51}
+ it { should_not be_valid }
+ end
+
+ describe "when email is not present" do
+ before {@user.email=" "}
+ it { should_not be_valid}
+ end
+
+ describe "when email is invalid" do
+
+ it "should be invalid" do
+ invalid_addresses = %w[ sds@sd without @sdss fadsa@ddsa,cds dsd$ds@ssd.com ]
+ invalid_addresses.each do | invalid_address |
+ @user.email = invalid_address
+ expect( @user ).not_to be_valid
+ end
+ end
+
+ end
+
+ describe "when email is valid" do
+ it "should be valid" do
+ valid_addresses = %w[ h@haha.com hh@h.in A+B@dd.COm ffd@in.in.in ]
+ valid_addresses.each do | valid_address |
+ @user.email = valid_address
+ expect( @user ).to be_valid
+ end
+ end
+ end
+
+ describe "when email is already taken" do
+ before do
+ dup_user = @user.dup
+ dup_user.email = @user.email.upcase
+ dup_user.save
+ end
+ it { should_not be_valid}
+ end
+
+
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe "UserPages" do
+
+ subject {page}
+
+ describe "user sign up" do
+
+ before { visit signup_path }
+
+ it { should have_content('Sign up') }
+ it { should have_title('Sign up') }
+
+ end
+end

0 comments on commit 29628e1

Please sign in to comment.