From 1d905e615e927642a021701f82f0428ad009986d Mon Sep 17 00:00:00 2001 From: notten13 <15044786+notten13@users.noreply.github.com> Date: Sat, 11 Mar 2023 19:03:08 +0000 Subject: [PATCH] feat: sign up form (wip) --- Gemfile | 2 +- Gemfile.lock | 2 + app/controllers/users_controller.rb | 24 ++++++++++ app/models/user.rb | 13 ++++++ app/views/layouts/application.html.erb | 27 +++++++++++- app/views/users/new.html.erb | 54 +++++++++++++++++++++++ config/routes.rb | 4 +- db/migrate/20230311181749_create_users.rb | 16 +++++++ db/schema.rb | 27 ++++++++++++ test/fixtures/users.yml | 21 +++++++++ test/models/user_test.rb | 7 +++ 11 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 app/controllers/users_controller.rb create mode 100644 app/models/user.rb create mode 100644 app/views/users/new.html.erb create mode 100644 db/migrate/20230311181749_create_users.rb create mode 100644 db/schema.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/Gemfile b/Gemfile index 2437f82..31942bc 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", "~> 3.1.7" # 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 6d8e295..c716f24 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,7 @@ GEM tzinfo (~> 2.0) addressable (2.8.1) public_suffix (>= 2.0.2, < 6.0) + bcrypt (3.1.18) bindex (0.8.1) bootsnap (1.16.0) msgpack (~> 1.2) @@ -209,6 +210,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + bcrypt (~> 3.1.7) bootsnap capybara debug diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..be2e82f --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,24 @@ +class UsersController < ApplicationController + + def new + @user = User.new + end + + def create + user_params = params.require(:user).permit( + :username, + :email, + :password, + :password_confirmation + ) + + @user = User.new(user_params) + + if @user.valid? + render 'new' + else + render 'new', status: :unprocessable_entity + end + end + +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..17e885b --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,13 @@ +class User < ApplicationRecord + has_secure_password + + has_secure_token :email_confirmation_token + + validates :username, + format: { with: /\A[A-Za-z0-9_]{2,20}\z/ , message: 'should contain 2 to 20 alphanumeric characters' }, + uniqueness: { case_sensitive: false } + + validates :email, + format: { with: URI::MailTo::EMAIL_REGEXP }, + uniqueness: { case_sensitive: false } +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e93b05c..8ec7765 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,16 +1,39 @@ - App + + <% if content_for?(:page_title) %> + <%= yield :page_title %> | Petbook + <% else %> + Petbook + <% end %> + + <%= csrf_meta_tags %> <%= csp_meta_tag %> + + <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> - <%= javascript_importmap_tags %> + + <%= yield %> + + + <%= javascript_importmap_tags %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 0000000..17deff1 --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1,54 @@ +<% content_for :page_title, "Sign up" %> + +
+
+
+ +
+ +
+

Sign Up

+
+ +
+ <% if @user.errors.any? %> +
+

Your registration couldn't be processed:

+
    + <% @user.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + + <%= form_with model: @user do |f| %> +
+ <%= f.label :username, "User name" %> + <%= f.text_field :username, class: 'form-control' %> +
+ +
+ <%= f.label :email, "Email address" %> + <%= f.email_field :email, class: 'form-control' %> +
+ +
+ <%= f.label :password, "Password" %> + <%= f.password_field :password, class: 'form-control' %> +
+ +
+ <%= f.label :password_confirmation, "Confirm your password" %> + <%= f.password_field :password_confirmation, class: 'form-control' %> +
+ +
+ +
+ <% end %> +
+
+
+
+
diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..e658e1d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,5 +2,7 @@ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") - # root "articles#index" + root "users#new" + + resource :users, only: [:new, :create] end diff --git a/db/migrate/20230311181749_create_users.rb b/db/migrate/20230311181749_create_users.rb new file mode 100644 index 0000000..ed0c7f5 --- /dev/null +++ b/db/migrate/20230311181749_create_users.rb @@ -0,0 +1,16 @@ +class CreateUsers < ActiveRecord::Migration[7.0] + def change + create_table :users do |t| + t.string :username + t.string :email + t.string :email_confirmed, default: false + t.string :email_confirm_token + t.string :password_digest + t.string :first_name + t.string :last_name + t.boolean :avatar, default: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..9c02dc7 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,27 @@ +# 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_03_11_181749) do + create_table "users", charset: "utf8mb4", force: :cascade do |t| + t.string "username" + t.string "email" + t.string "email_confirmed", default: "0" + t.string "email_confirm_token" + t.string "password_digest" + t.string "first_name" + t.string "last_name" + t.boolean "avatar", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..6d181d7 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,21 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + username: MyString + email: MyString + email_confirmed: MyString + email_confirm_token: MyString + password_digest: MyString + first_name: MyString + last_name: MyString + avatar: false + +two: + username: MyString + email: MyString + email_confirmed: MyString + email_confirm_token: MyString + password_digest: MyString + first_name: MyString + last_name: MyString + avatar: false 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