Skip to content

Commit

Permalink
Merge 078d85e into 5ba0aad
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Dec 5, 2015
2 parents 5ba0aad + 078d85e commit aafb65a
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class User < ActiveRecord::Base
has_secure_password

validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :city, presence: true
validates :state, presence: true
validates :country, presence: true
validates :password, length: { minimum: 8 }
validates :email, format: {
with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create
}
end
15 changes: 15 additions & 0 deletions db/migrate/20151203004651_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password_digest
t.boolean :email_confirmed
t.string :confirm_digest
t.string :reset_digest
t.datetime :reset_sent_at

t.timestamps null: false
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20151203025432_rename_user_name_to_first_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameUserNameToFirstName < ActiveRecord::Migration
def change
rename_column :users, :name, :first_name
end
end
5 changes: 5 additions & 0 deletions db/migrate/20151203030029_add_last_name_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLastNameToUser < ActiveRecord::Migration
def change
add_column :users, :last_name, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20151203030116_add_city_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCityToUser < ActiveRecord::Migration
def change
add_column :users, :city, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20151203030132_add_state_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStateToUser < ActiveRecord::Migration
def change
add_column :users, :state, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20151203030146_add_country_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCountryToUser < ActiveRecord::Migration
def change
add_column :users, :country, :string
end
end
35 changes: 35 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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: 20151203030146) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "email"
t.string "password_digest"
t.boolean "email_confirmed"
t.string "confirm_digest"
t.string "reset_digest"
t.datetime "reset_sent_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "last_name"
t.string "city"
t.string "state"
t.string "country"
end

end
22 changes: 22 additions & 0 deletions spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FactoryGirl.define do
factory :confirmed_user, class: User do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
email Faker::Internet.email
city Faker::Address.city
state Faker::Address.state
country Faker::Address.country
email_confirmed true
end

factory :unconfirmed_user, class: User do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
email Faker::Internet.email
city Faker::Address.city
state Faker::Address.state
country Faker::Address.country
password "helloworld"
email_confirmed false
end
end
49 changes: 49 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "rails_helper"

RSpec.describe User do
required_props = %i(
first_name
last_name
email
city
state
country
password
)

properties = required_props + %i(
password_digest
email_confirmed
confirm_digest
reset_digest
reset_sent_at
)

describe "properties" do
properties.each do |prop|
it "has an accessor for #{prop}" do
expect { subject.send(prop) }.to_not raise_error
end
end
end

describe "validations" do
required_props.each do |prop|
it "rejects a new user without a `#{prop}`" do
user = FactoryGirl.build(:unconfirmed_user)
user.send("#{prop}=", nil)
expect(user.save).to be_falsy
end
end

it "validates password length" do
user = FactoryGirl.build(:unconfirmed_user, password: "xxx")
expect(user.save).to be_falsy
end

it "valudates email format" do
user = FactoryGirl.build(:unconfirmed_user, email: "xxx.xxx")
expect(user.save).to be_falsy
end
end
end

0 comments on commit aafb65a

Please sign in to comment.