Skip to content

Commit

Permalink
make a basic user model
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagonbotelho committed May 22, 2015
1 parent a617314 commit 465350d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source 'https://rubygems.org'


gem 'rails', '4.2.1'
gem 'bcrypt', '3.1.7'
gem 'bootstrap-sass', :git => 'https://github.com/twbs/bootstrap-sass.git', :branch => 'next'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ GEM
autoprefixer-rails (5.1.11)
execjs
json
bcrypt (3.1.7)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
Expand Down Expand Up @@ -208,6 +209,7 @@ PLATFORMS
ruby

DEPENDENCIES
bcrypt (= 3.1.7)
bootstrap-sass!
byebug
coffee-rails (~> 4.1.0)
Expand Down
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class User < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
before_save { self.email = self.email.downcase }
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end
10 changes: 10 additions & 0 deletions db/migrate/20150518215416_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email

t.timestamps null: false
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20150521114234_add_index_to_users_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20150521115706_add_password_digest_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
add_column :users, :password_digest, :string
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 0) do
ActiveRecord::Schema.define(version: 20150521115706) do

create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true

end
1 change: 1 addition & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#empty
54 changes: 54 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase

def setup
@user = User.new(name: "Example User", email: "[email protected]",
password: "foobar", password_confirmation: "foobar")
end

test "should be valid" do
assert @user.valid?
end

test "name should be present" do
@user.name = " "
assert_not @user.valid?
end

test "email should be present" do
@user.email = " "
assert_not @user.valid?
end

test "name should not be too long" do
@user.name = "a"*51
assert_not @user.valid?
end

test "email should not be too long" do
@user.email = "a"*255 + "@example.com"
assert_not @user.valid?
end

test "email validation should accept valid email adresses" do
valid_addresses = %w[[email protected] [email protected] [email protected] [email protected] [email protected]]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end

test "email addresses should be unique" do
duplicate_user = @user.dup
duplicate_user.email = @user.email.upcase
@user.save
assert_not duplicate_user.valid?
end

test "password should have minimum length" do
@user.password = @user.password_confirmation = "a"*5
assert_not @user.valid?
end

end

0 comments on commit 465350d

Please sign in to comment.