Added user model

This commit is contained in:
Trevor Vallender 2023-08-08 20:25:35 +01:00
parent abf311ae5c
commit 0915c65dc8
5 changed files with 77 additions and 0 deletions

20
app/models/user.rb Normal file
View File

@ -0,0 +1,20 @@
# frozen_string_literal: true
class User < ApplicationRecord
has_secure_password
validates :username,
:first_name,
:last_names,
:email,
presence: true
validates :email,
:username,
uniqueness: { case_sensitive: false }
validates :email,
format: { with: /\A.*@.*\..*\z/ } # Only very basic regex
validates :password, confirmation: true
end

View File

@ -0,0 +1,13 @@
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :username, null: false, index: { unique: true }
t.string :password_digest, null: false
t.string :first_name, null: false
t.string :last_names, null: false
t.string :email, null: false, index: { unique: true }
t.timestamps
end
end
end

29
db/schema.rb generated Normal file
View File

@ -0,0 +1,29 @@
# 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.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_08_08_190158) 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 "username", null: false
t.string "password_digest", null: false
t.string "first_name", null: false
t.string "last_names", null: false
t.string "email", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
end

6
test/fixtures/users.yml vendored Normal file
View File

@ -0,0 +1,6 @@
user:
username: gimli
password_digest: <%= BCrypt::Password.create('tolkien-abercrombie-hobb-barker', cost: 5) %>
first_name: Gimli
last_names: son of Glóin
email: gimli@example.com

9
test/models/user_test.rb Normal file
View File

@ -0,0 +1,9 @@
require "test_helper"
class UserTest < ActiveSupport::TestCase
test "email must resemble an email" do
user = users(:user)
user.email = "foobar"
assert_not user.valid?
end
end