Added an Admin tab to the main header. Added Forms and Users as additional pages. Set it so Users can only be accessed by an Admin. Still need to finish styling. Forms is set to default until worked on.

This commit is contained in:
Ben 2024-02-15 17:23:54 -06:00
parent 5d024f430a
commit 12a9641ca7
29 changed files with 310 additions and 2 deletions

View File

@ -0,0 +1,50 @@
# app/controllers/forms_controller.rb
class FormsController < ApplicationController
before_action :set_form, only: [:show, :edit, :update, :destroy]
def index
@forms = Form.all
end
def show
end
def new
@form = Form.new
end
def create
@form = Form.new(form_params)
if @form.save
redirect_to @form, notice: 'Form was successfully created.'
else
render :new
end
end
def edit
end
def update
if @form.update(form_params)
redirect_to @form, notice: 'Form was successfully updated.'
else
render :edit
end
end
def destroy
@form.destroy
redirect_to forms_url, notice: 'Form was successfully destroyed.'
end
private
def set_form
@form = Form.find(params[:id])
end
def form_params
params.require(:form).permit(:name) # Adjust attributes as needed
end
end

View File

@ -0,0 +1,41 @@
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :set_user, only: [:edit, :update, :destroy]
before_action :require_admin
def index
@users = User.all
end
def edit
end
def update
if @user.update(user_params)
redirect_to users_path, notice: 'User was successfully updated.'
else
render :edit
end
end
def destroy
@user.destroy
redirect_to users_path, notice: 'User was successfully deleted.'
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:email, :admin)
end
def require_admin
unless current_user.admin?
redirect_to root_path, alert: 'Only admins are allowed to access this section.'
end
end
end

View File

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

View File

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

2
app/models/form.rb Normal file
View File

@ -0,0 +1,2 @@
class Form < ApplicationRecord
end

View File

@ -0,0 +1,2 @@
<h1>Forms#create</h1>
<p>Find me in app/views/forms/create.html.erb</p>

View File

@ -0,0 +1,2 @@
<h1>Forms#destroy</h1>
<p>Find me in app/views/forms/destroy.html.erb</p>

View File

@ -0,0 +1,2 @@
<h1>Forms#edit</h1>
<p>Find me in app/views/forms/edit.html.erb</p>

View File

@ -0,0 +1,2 @@
<h1>Forms Index</h1>
<p>Placeholder for listing forms.</p>

View File

@ -0,0 +1,2 @@
<h1>Forms#new</h1>
<p>Find me in app/views/forms/new.html.erb</p>

View File

@ -0,0 +1,2 @@
<h1>Forms#show</h1>
<p>Find me in app/views/forms/show.html.erb</p>

View File

@ -0,0 +1,2 @@
<h1>Forms#update</h1>
<p>Find me in app/views/forms/update.html.erb</p>

View File

@ -6,7 +6,7 @@
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<!-- Add links to model index pages here -->
<!-- Existing links -->
<li class="nav-item">
<%= link_to 'Participants', participants_path, class: 'nav-link' %>
</li>
@ -20,6 +20,17 @@
<%= link_to 'Vendors', vendors_path, class: 'nav-link' %>
</li>
<!-- Admin Dropdown Menu -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Admin
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><%= link_to 'Forms', forms_path, class: 'dropdown-item' %></li>
<li><%= link_to 'Users', users_path, class: 'dropdown-item' %></li>
</ul>
</li>
<!-- Authentication links -->
<% if user_signed_in? %>
<li class="nav-item">

View File

@ -0,0 +1,27 @@
<%= form_with(model: user, local: true) do |form| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :email %><br>
<%= form.email_field :email, id: :user_email %>
</div>
<div class="field">
<%= form.label :admin %><br>
<%= form.check_box :admin %>
</div>
<div class="actions">
<%= form.submit 'Save', class: 'btn btn-primary' %>
</div>
<% end %>

View File

@ -0,0 +1,2 @@
<h1>Users#create</h1>
<p>Find me in app/views/users/create.html.erb</p>

View File

@ -0,0 +1,2 @@
<h1>Users#destroy</h1>
<p>Find me in app/views/users/destroy.html.erb</p>

View File

@ -0,0 +1,5 @@
<h1>Edit User</h1>
<%= render 'form', user: @user %>
<%= link_to 'Back to Users', users_path, class: 'btn btn-secondary' %>

View File

@ -0,0 +1,23 @@
<h1>Users</h1>
<table class="table">
<thead>
<tr>
<th>Email</th>
<th>Admin</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td><%= user.admin? ? 'Yes' : 'No' %></td>
<td>
<%= link_to 'Edit', edit_user_path(user), class: 'btn btn-info' %>
<%= link_to 'Delete', user_path(user), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>

View File

@ -0,0 +1,2 @@
<h1>Users#new</h1>
<p>Find me in app/views/users/new.html.erb</p>

View File

@ -0,0 +1,2 @@
<h1>Users#show</h1>
<p>Find me in app/views/users/show.html.erb</p>

View File

@ -0,0 +1,2 @@
<h1>Users#update</h1>
<p>Find me in app/views/users/update.html.erb</p>

View File

@ -5,6 +5,11 @@ Rails.application.routes.draw do
root 'home#index', as: :authenticated_root
end
authenticate :user, lambda { |u| u.admin? } do
resources :users, only: [:index, :edit, :update, :destroy]
end
devise_scope :user do
root to: 'devise/sessions#new'
end
@ -53,6 +58,9 @@ Rails.application.routes.draw do
end
get 'search', on: :collection
end
resources :forms
resources :users

View File

@ -0,0 +1,8 @@
class CreateForms < ActiveRecord::Migration[7.1]
def change
create_table :forms do |t|
t.timestamps
end
end
end

View File

@ -0,0 +1,5 @@
class AddAdminToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :admin, :boolean, default: false, null: false
end
end

8
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[7.1].define(version: 2024_02_12_212110) do
ActiveRecord::Schema[7.1].define(version: 2024_02_15_231142) do
create_table "bank_accounts", force: :cascade do |t|
t.string "institution_name"
t.string "account_type"
@ -72,6 +72,11 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_12_212110) do
t.index ["worker_id"], name: "index_employments_on_worker_id"
end
create_table "forms", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "participants", force: :cascade do |t|
t.string "first_name"
t.string "phone"
@ -131,6 +136,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_12_212110) do
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "admin", default: false, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

View File

@ -0,0 +1,38 @@
require "test_helper"
class FormsControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get forms_index_url
assert_response :success
end
test "should get new" do
get forms_new_url
assert_response :success
end
test "should get create" do
get forms_create_url
assert_response :success
end
test "should get edit" do
get forms_edit_url
assert_response :success
end
test "should get update" do
get forms_update_url
assert_response :success
end
test "should get show" do
get forms_show_url
assert_response :success
end
test "should get destroy" do
get forms_destroy_url
assert_response :success
end
end

View File

@ -0,0 +1,38 @@
require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get users_index_url
assert_response :success
end
test "should get new" do
get users_new_url
assert_response :success
end
test "should get create" do
get users_create_url
assert_response :success
end
test "should get edit" do
get users_edit_url
assert_response :success
end
test "should get update" do
get users_update_url
assert_response :success
end
test "should get show" do
get users_show_url
assert_response :success
end
test "should get destroy" do
get users_destroy_url
assert_response :success
end
end

11
test/fixtures/forms.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

7
test/models/form_test.rb Normal file
View File

@ -0,0 +1,7 @@
require "test_helper"
class FormTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end