Add users controller
Some checks are pending
CI / scan_ruby (push) Waiting to run
CI / scan_js (push) Waiting to run
CI / lint (push) Waiting to run
CI / test (push) Waiting to run

This commit is contained in:
sto 2025-03-14 16:37:21 +01:00
parent 50280ce389
commit 026bda2a99
14 changed files with 110 additions and 6 deletions

View File

@ -3,6 +3,7 @@ class ContestsController < ApplicationController
def index
@contests = current_user.contests
@user = current_user
end
def show

View File

@ -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

View File

@ -0,0 +1,2 @@
module UsersHelper
end

View File

@ -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

View File

@ -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!

View File

@ -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

View File

@ -0,0 +1,4 @@
.container
h1 Edit settings
= render "form", user: @user

View File

@ -0,0 +1 @@
.container

View File

@ -0,0 +1,4 @@
.container
h1 Create a new user
= render "form", user: @user

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,5 @@
class AddUsernameToUser < ActiveRecord::Migration[8.0]
def change
add_column :users, :username, :string
end
end

3
db/schema.rb generated
View File

@ -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

View File

@ -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