From 014c15fe4e85df95f69b45dad13b6f3b9ee5e9e2 Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Tue, 26 Sep 2023 13:29:04 -0500 Subject: [PATCH] Complete Lesson 26 --- Gemfile | 2 +- Gemfile.lock | 2 ++ app/controllers/users_controller.rb | 2 ++ app/helpers/users_helper.rb | 2 ++ app/models/user.rb | 6 ++++++ config/routes.rb | 1 + db/migrate/20230926182250_create_users.rb | 11 +++++++++++ db/schema.rb | 10 +++++++++- test/controllers/users_controller_test.rb | 7 +++++++ test/fixtures/users.yml | 11 +++++++++++ test/models/user_test.rb | 7 +++++++ 11 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/models/user.rb create mode 100644 db/migrate/20230926182250_create_users.rb create mode 100644 test/controllers/users_controller_test.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/Gemfile b/Gemfile index 7b00de9..1f2c4f2 100644 --- a/Gemfile +++ b/Gemfile @@ -34,7 +34,7 @@ gem "jbuilder" # gem "kredis" # Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] -# gem "bcrypt", "~> 3.1.7" +gem "bcrypt" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] diff --git a/Gemfile.lock b/Gemfile.lock index cbec558..9c3c5bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -70,6 +70,7 @@ GEM public_suffix (>= 2.0.2, < 6.0) autoprefixer-rails (10.4.15.0) execjs (~> 2) + bcrypt (3.1.19) bindex (0.8.1) bootsnap (1.16.0) msgpack (~> 1.2) @@ -225,6 +226,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + bcrypt bootsnap bootstrap capybara diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..3e74dea --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,2 @@ +class UsersController < ApplicationController +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..eac3154 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ApplicationRecord + has_secure_password + + validates :name, presence: true + validates :email, presence: true, format: { with: /\S+@\S+/ }, uniqueness: { case_sensitive: false } +end diff --git a/config/routes.rb b/config/routes.rb index 97916af..bd48338 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :users resources :reviews root "movies#index" diff --git a/db/migrate/20230926182250_create_users.rb b/db/migrate/20230926182250_create_users.rb new file mode 100644 index 0000000..33b1925 --- /dev/null +++ b/db/migrate/20230926182250_create_users.rb @@ -0,0 +1,11 @@ +class CreateUsers < ActiveRecord::Migration[7.0] + def change + create_table :users do |t| + t.string :name + t.string :email + t.string :password_digest + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b208283..6938b67 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_09_26_165701) do +ActiveRecord::Schema[7.0].define(version: 2023_09_26_182250) do create_table "movies", force: :cascade do |t| t.string "title" t.string "rating" @@ -34,5 +34,13 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_26_165701) do t.index ["movie_id"], name: "index_reviews_on_movie_id" end + create_table "users", force: :cascade do |t| + t.string "name" + t.string "email" + t.string "password_digest" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + add_foreign_key "reviews", "movies" end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..61c1532 --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UsersControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..536ab84 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + email: MyString + password_digest: <%= BCrypt::Password.create("secret") %> + +two: + name: MyString + email: MyString + password_digest: <%= BCrypt::Password.create("secret") %> diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..5c07f49 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end