Skip to content

Commit

Permalink
Create user model
Browse files Browse the repository at this point in the history
  • Loading branch information
btk5h committed Aug 26, 2020
1 parent 302ad2c commit e73b49f
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/api/api_routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class APIRoutes < Grape::API
use ActionDispatch::Session::CookieStore, key: '_sbhacks_session'
format :json

helpers APIHelpers

mount API::Auth
end
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 < ApplicationRecord
has_secure_password

validates :name, presence: true
validates :email, format: {
with: URI::MailTo::EMAIL_REGEXP,
message: 'is not a valid email address'
}
validates :password, length: { in: 8..72 }
end
11 changes: 11 additions & 0 deletions db/migrate/20200824035326_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name, nil: false
t.string :email, index: { unique: true }, nil: false
t.string :password_digest, nil: false

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 0) do
ActiveRecord::Schema.define(version: 2020_08_24_035326) 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 "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
end

end
12 changes: 5 additions & 7 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
User.create!(
name: 'SBHacks Admin',
email: '[email protected]',
password: 'password'
)
9 changes: 9 additions & 0 deletions test/factories/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :user do
name { 'Akshay Heda' }
sequence :email do |n|
"akshay.#{n}@example.com"
end
password { 'ilovesbhacks' }
end
end
31 changes: 31 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
test 'factory is valid' do
assert_predicate build(:user), :valid?
end

test 'requires a name' do
user = build(:user)
user.name = nil
assert_not_predicate user, :valid?

user.name = ''
assert_not_predicate user, :valid?
end

test 'requires a valid email address' do
user = build(:user)
user.email = 'not an email'
assert_not_predicate user, :valid?
end

test 'requires a password' do
user = build(:user)
user.password = nil
assert_not_predicate user, :valid?

user.password = 'short'
assert_not_predicate user, :valid?
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module ActiveSupport
class TestCase
include GrapeRouteHelpers::NamedRouteMatcher
include FactoryBot::Syntax::Methods
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)

Expand Down

0 comments on commit e73b49f

Please sign in to comment.