Add separate page for user management

This commit is contained in:
Eugene Burmakin 2024-11-08 17:56:14 +01:00
parent ed0b28f553
commit b97b30c88a
8 changed files with 114 additions and 21 deletions

File diff suppressed because one or more lines are too long

View file

@ -14,7 +14,7 @@ class ApplicationController < ActionController::Base
end
def authenticate_admin!
return if current_user.admin?
return if current_user&.admin?
redirect_to root_path, notice: 'You are not authorized to perform this action.', status: :see_other
end

View file

@ -1,9 +1,26 @@
# frozen_string_literal: true
class Settings::UsersController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_admin!
def index
@users = User.order(created_at: :desc)
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to settings_users_url, notice: 'User was successfully updated.'
else
redirect_to settings_users_url, notice: 'User could not be updated.', status: :unprocessable_entity
end
end
def create
@user = User.new(
email: user_params[:email],
@ -12,10 +29,20 @@ class Settings::UsersController < ApplicationController
)
if @user.save
redirect_to settings_url,
redirect_to settings_users_url,
notice: "User was successfully created, email is #{@user.email}, password is \"password\"."
else
redirect_to settings_url, notice: 'User could not be created.', status: :unprocessable_entity
redirect_to settings_users_url, notice: 'User could not be created.', status: :unprocessable_entity
end
end
def destroy
@user = User.find(params[:id])
if @user.destroy
redirect_to settings_url, notice: 'User was successfully deleted.'
else
redirect_to settings_url, notice: 'User could not be deleted.', status: :unprocessable_entity
end
end

View file

@ -1,4 +1,5 @@
<div role="tablist" class="tabs tabs-lifted tabs-lg">
<%= link_to 'Main', settings_path, role: 'tab', class: "tab #{active_tab?(settings_path)}" %>
<%= link_to 'Users', settings_users_path, role: 'tab', class: "tab #{active_tab?(settings_users_path)}" %>
<%= link_to 'Background Jobs', settings_background_jobs_path, role: 'tab', class: "tab #{active_tab?(settings_background_jobs_path)}" %>
</div>

View file

@ -20,19 +20,5 @@
</div>
<% end %>
</div>
<div class="card flex-shrink-0 w-full max-w-sm shadow-2xl bg-base-100 px-5 py-5">
<h2 class="text-2xl font-bold">Create a new user!</h1>
<%= form_for :user, url: settings_users_path, method: :post, data: { turbo_method: :post, turbo: false } do |f| %>
<div class="form-control">
<%= f.label :email do %>
Email
<% end %>
<%= f.email_field :email, value: '', class: "input input-bordered" %>
</div>
<div class="form-control mt-5">
<%= f.submit "Create", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>

View file

@ -0,0 +1,22 @@
<% content_for :title, 'Editing user' %>
<div class="min-h-content w-full">
<%= render 'settings/navigation' %>
<div class="flex w-full my-10 space-x-4">
<div class="overflow-x-auto w-4/12 mx-auto">
<h1 class="text-2xl font-bold">Editing user</h1>
<%= form_for @user, url: settings_user_path(@user), method: :put, data: { turbo_method: :put, turbo: false } do |f| %>
<div class="form-control">
<%= f.label :email do %>
Email
<% end %>
<%= f.email_field :email, value: @user.email, class: "input input-bordered" %>
</div>
<div class="form-control mt-5">
<%= f.submit "Update", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>

View file

@ -0,0 +1,57 @@
<% content_for :title, 'Users' %>
<div class="min-h-content w-full">
<%= render 'settings/navigation' %>
<div class="flex flex-col lg:flex-row w-full my-10 space-x-4">
<div class="overflow-x-auto w-10/12 mx-auto">
<button class="btn" onclick="create_user.showModal()">Add new user</button>
<table class="table w-full">
<thead>
<tr>
<th>Email</th>
<th>Points</th>
<th>Created at</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td>
<div>
<%= link_to user.email, edit_settings_user_path(user), class: 'font-bold underline hover:no-underline' %>
</div>
</td>
<td>
<%= number_with_delimiter user.tracked_points.count %>
</td>
<td>
<%= user.created_at.strftime('%Y-%m-%d %H:%M:%S') %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
<dialog id="create_user" class="modal">
<div class="modal-box">
<h2 class="text-2xl font-bold">Create a new user!</h1>
<%= form_for :user, url: settings_users_path, method: :post, data: { turbo_method: :post, turbo: false } do |f| %>
<div class="form-control">
<%= f.label :email do %>
Email
<% end %>
<%= f.email_field :email, value: '', class: "input input-bordered" %>
</div>
<div class="form-control mt-5">
<%= f.submit "Create", class: "btn btn-primary" %>
</div>
<% end %>
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>

View file

@ -19,7 +19,7 @@ Rails.application.routes.draw do
resources :settings, only: :index
namespace :settings do
resources :background_jobs, only: %i[index create destroy]
resources :users, only: :create
resources :users, only: %i[index create destroy edit update]
end
patch 'settings', to: 'settings#update'