From 026bda2a99a3e2d78a55495a33184bb7ddc9c613 Mon Sep 17 00:00:00 2001 From: sto Date: Fri, 14 Mar 2025 16:37:21 +0100 Subject: [PATCH] Add users controller --- app/controllers/contests_controller.rb | 1 + app/controllers/users_controller.rb | 36 +++++++++++++++++++ app/helpers/users_helper.rb | 2 ++ app/models/user.rb | 2 ++ app/views/contests/index.html.slim | 6 ++-- app/views/users/_form.html.slim | 9 +++++ app/views/users/edit.html.slim | 4 +++ app/views/users/index.html.slim | 1 + app/views/users/new.html.slim | 4 +++ app/views/users/show.html.slim | 9 +++++ config/routes.rb | 6 ++-- .../20250314145912_add_username_to_user.rb | 5 +++ db/schema.rb | 3 +- test/controllers/users_controller_test.rb | 28 +++++++++++++++ 14 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/views/users/_form.html.slim create mode 100644 app/views/users/edit.html.slim create mode 100644 app/views/users/index.html.slim create mode 100644 app/views/users/new.html.slim create mode 100644 app/views/users/show.html.slim create mode 100644 db/migrate/20250314145912_add_username_to_user.rb create mode 100644 test/controllers/users_controller_test.rb diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index 601369f..8ff3af8 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -3,6 +3,7 @@ class ContestsController < ApplicationController def index @contests = current_user.contests + @user = current_user end def show diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..bcfe9e4 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,36 @@ +class UsersController < ApplicationController + before_action :set_user, only: %i[ destroy edit update show ] + + def index + end + + def edit + end + + def update + if @user.update(user_params) + redirect_to @user + else + render :edit, status: :unprocessable_entity + end + end + + def show + end + + def new + end + + def destroy + end + + private + + def set_user + @user = User.find(params[:id]) + end + + def user_params + params.expect(user: [ :username, :email_address ]) + end +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 index 1a080e8..d9c98c8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,4 +4,6 @@ class User < ApplicationRecord has_secure_password normalizes :email_address, with: ->(e) { e.strip.downcase } + + validates :username, presence: true, uniqueness: true end diff --git a/app/views/contests/index.html.slim b/app/views/contests/index.html.slim index 378c8ea..3e370d5 100644 --- a/app/views/contests/index.html.slim +++ b/app/views/contests/index.html.slim @@ -1,7 +1,9 @@ .container.mt-5 - if authenticated? - .float-end - = button_to "Log out", session_path, method: :delete + .float-end.ms-3 + = button_to "Log out", session_path, method: :delete + .float-end.mt-1 + = link_to "Settings", user_path(@user) h1 Welcome! diff --git a/app/views/users/_form.html.slim b/app/views/users/_form.html.slim new file mode 100644 index 0000000..cff2b58 --- /dev/null +++ b/app/views/users/_form.html.slim @@ -0,0 +1,9 @@ += form_with model: user do |form| + div + = form.label :username + = form.text_field :username + div + = form.label :email_address + = form.text_field :email_address + div + = form.submit \ No newline at end of file diff --git a/app/views/users/edit.html.slim b/app/views/users/edit.html.slim new file mode 100644 index 0000000..b3e1bdc --- /dev/null +++ b/app/views/users/edit.html.slim @@ -0,0 +1,4 @@ +.container + h1 Edit settings + + = render "form", user: @user \ No newline at end of file diff --git a/app/views/users/index.html.slim b/app/views/users/index.html.slim new file mode 100644 index 0000000..9e552a7 --- /dev/null +++ b/app/views/users/index.html.slim @@ -0,0 +1 @@ +.container \ No newline at end of file diff --git a/app/views/users/new.html.slim b/app/views/users/new.html.slim new file mode 100644 index 0000000..c90fdb0 --- /dev/null +++ b/app/views/users/new.html.slim @@ -0,0 +1,4 @@ +.container + h1 Create a new user + + = render "form", user: @user \ No newline at end of file diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim new file mode 100644 index 0000000..24da1e2 --- /dev/null +++ b/app/views/users/show.html.slim @@ -0,0 +1,9 @@ +.container + h1 = @user.username + + p + |> Email: + = @user.email_address + + a.btn.btn-primary.mt-4 href=edit_user_path(@user) + | Edit \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 41ea461..1347a77 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,4 @@ Rails.application.routes.draw do - get "contests/create" - resource :session - resources :passwords, param: :token # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. @@ -12,5 +9,8 @@ Rails.application.routes.draw do root "contests#index" resources :contests + resources :passwords, param: :token resources :puzzles + resource :session + resources :users end diff --git a/db/migrate/20250314145912_add_username_to_user.rb b/db/migrate/20250314145912_add_username_to_user.rb new file mode 100644 index 0000000..ecbd903 --- /dev/null +++ b/db/migrate/20250314145912_add_username_to_user.rb @@ -0,0 +1,5 @@ +class AddUsernameToUser < ActiveRecord::Migration[8.0] + def change + add_column :users, :username, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index aba61de..22263c3 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[8.0].define(version: 2025_03_13_142853) do +ActiveRecord::Schema[8.0].define(version: 2025_03_14_145912) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -67,6 +67,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_13_142853) do t.string "password_digest", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "username" t.index ["email_address"], name: "index_users_on_email_address", unique: true end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..6ed33da --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,28 @@ +require "test_helper" + +class UsersControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get users_index_url + assert_response :success + end + + test "should get edit" do + get users_edit_url + assert_response :success + end + + test "should get show" do + get users_show_url + assert_response :success + end + + test "should get new" do + get users_new_url + assert_response :success + end + + test "should get destroy" do + get users_destroy_url + assert_response :success + end +end