diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..54dcff8 --- /dev/null +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20230808190158_create_users.rb b/db/migrate/20230808190158_create_users.rb new file mode 100644 index 0000000..8e7df9a --- /dev/null +++ b/db/migrate/20230808190158_create_users.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..671fc65 --- /dev/null +++ b/db/schema.rb @@ -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 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..3916d86 --- /dev/null +++ b/test/fixtures/users.yml @@ -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 diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..a99f01b --- /dev/null +++ b/test/models/user_test.rb @@ -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