From e7abc0fa505e1fd2e6dbefcc2347a64b623ce444 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 11 Apr 2024 23:48:16 -0500 Subject: [PATCH] Removed autocomplete fields from Create New User page. --- app/controllers/admin_controller.rb | 44 ++++++------- app/controllers/users_controller.rb | 21 ++++-- app/models/access_period.rb | 3 + app/models/user.rb | 26 +++++++- app/views/admin/new_user.html.erb | 37 ++++++++--- app/views/users/_form.html.erb | 64 +++++++++---------- app/views/users/index.html.erb | 2 +- config/routes.rb | 11 ++-- .../20240411215819_create_access_periods.rb | 11 ++++ db/schema.rb | 12 +++- test/fixtures/access_periods.yml | 11 ++++ test/models/access_period_test.rb | 7 ++ 12 files changed, 173 insertions(+), 76 deletions(-) create mode 100644 app/models/access_period.rb create mode 100644 db/migrate/20240411215819_create_access_periods.rb create mode 100644 test/fixtures/access_periods.yml create mode 100644 test/models/access_period_test.rb diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 3bc151a..2d091b7 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -1,26 +1,24 @@ class AdminController < ApplicationController - before_action :authenticate_user! - load_and_authorize_resource class: User - - def new_user - @user = User.new - end - - def create_user - @user = User.new(user_params) - if @user.save - # Add role to the user here if needed e.g., user.add_role :new_role - redirect_to admin_users_path, notice: 'User was successfully created.' - else - render :new_user - end - end - - private - - def user_params - params.require(:user).permit(:email, :password, :password_confirmation) - # Add other fields as needed + before_action :authenticate_user! + load_and_authorize_resource class: User + + def new_user + @user = User.new + end + + def create_user + @user = User.new(user_params) + if @user.save + redirect_to some_admin_path, notice: 'User was successfully created.' # Update the redirect path as needed + else + render :new_user end end - \ No newline at end of file + + private + + def user_params + params.require(:user).permit(:email, :password, :password_confirmation) + # Ensure you permit the right parameters + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 8bef056..f11d93a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -8,6 +8,11 @@ class UsersController < ApplicationController @users = User.all end + def new + @user = User.new + end + + def edit end @@ -31,6 +36,11 @@ class UsersController < ApplicationController end end + def show + @user = User.includes(:access_periods).find(params[:id]) + end + + def destroy @user.destroy redirect_to users_path, notice: 'User was successfully deleted.' @@ -46,7 +56,8 @@ class UsersController < ApplicationController params.require(:user).permit( :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :phone, :company, - :access_revoked, :access_start_date, :access_end_date, + :access_revoked, :access_start_date, :access_end_date, + access_periods_attributes: [:id, :start_date, :end_date, :destroy], roles: [] ) end @@ -67,12 +78,14 @@ class UsersController < ApplicationController def update_user_roles(user, roles_names) - user.roles.delete_all # Remove existing roles if you want to reset roles on update - roles_names = ['user'] if roles_names.blank? # Ensure there's a default role + return if roles_names.blank? # Do nothing if no roles provided + + user.roles.delete_all # Consider keeping this if role reset is truly intended roles_names.each do |role_name| - user.add_role(role_name) + user.add_role(role_name) unless role_name.blank? end end + end diff --git a/app/models/access_period.rb b/app/models/access_period.rb new file mode 100644 index 0000000..4525b6d --- /dev/null +++ b/app/models/access_period.rb @@ -0,0 +1,3 @@ +class AccessPeriod < ApplicationRecord + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 040caa1..7c87fb4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,9 +1,31 @@ class User < ApplicationRecord rolify devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + has_many :access_periods, dependent: :destroy + accepts_nested_attributes_for :access_periods, allow_destroy: true + after_create :assign_default_role + + # Validation for date fields + validate :end_date_after_start_date, if: -> { access_revoked && access_end_date.present? } + + # Override Devise method to consider access_revoked def active_for_authentication? super && !access_revoked end - -end \ No newline at end of file + + private + + def assign_default_role + self.add_role(:user) unless self.has_any_role? + end + + def end_date_after_start_date + if access_start_date.present? + errors.add(:access_end_date, 'must be provided when access is revoked') unless access_end_date.present? + if access_end_date && access_end_date < access_start_date + errors.add(:access_end_date, 'must be after the start date') + end + end + end +end diff --git a/app/views/admin/new_user.html.erb b/app/views/admin/new_user.html.erb index 486d9c7..6ddeeb7 100644 --- a/app/views/admin/new_user.html.erb +++ b/app/views/admin/new_user.html.erb @@ -3,22 +3,42 @@

Create New User

- <%= form_for(@user, url: admin_users_path, html: { class: 'needs-validation', novalidate: true }) do |f| %> + <%= form_for(@user, url: users_path, html: { class: 'needs-validation', novalidate: true }) do |f| %> +
+ <%= f.label :first_name, 'First Name', class: 'form-label' %> + <%= f.text_field :first_name, class: 'form-control', placeholder: 'Enter first name', required: true %> +
+ +
+ <%= f.label :last_name, 'Last Name', class: 'form-label' %> + <%= f.text_field :last_name, class: 'form-control', placeholder: 'Enter last name', required: true %> +
+
<%= f.label :email, class: 'form-label' %> - <%= f.email_field :email, class: 'form-control', placeholder: 'Enter email', required: true %> + <%= f.email_field :email, class: 'form-control', placeholder: 'Enter email', required: true, autocomplete: "new-password" %>
- +
- <%= f.label :password, class: 'form-label' %> - <%= f.password_field :password, class: 'form-control', placeholder: 'Password', required: true %> + <%= f.label :password, 'Password', class: 'form-label' %> + <%= f.password_field :password, class: 'form-control', placeholder: 'Password', required: true, autocomplete: "new-password" %>
- +
- <%= f.label :password_confirmation, "Confirm Password", class: 'form-label' %> + <%= f.label :password_confirmation, 'Confirm Password', class: 'form-label' %> <%= f.password_field :password_confirmation, class: 'form-control', placeholder: 'Confirm Password', required: true %>
- + +
+ <%= f.label :phone, 'Phone Number', class: 'form-label' %> + <%= f.telephone_field :phone, class: 'form-control', placeholder: 'Enter phone number' %> +
+ +
+ <%= f.label :company, 'Company', class: 'form-label' %> + <%= f.text_field :company, class: 'form-control', placeholder: 'Enter company name' %> +
+
<%= f.submit "Create User", class: 'btn btn-dark' %>
@@ -26,6 +46,7 @@
+ <%# This is to indicate to the User if the passwords didn't match %> +<% end %> \ No newline at end of file diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 1ca70bd..06df59c 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -41,7 +41,7 @@
<% if can?(:create, User) %> - <%= link_to 'Add New User', new_admin_user_path, class: "btn btn-dark" %> + <%= link_to 'Add New User', new_user_admin_path, class: "btn btn-dark" %> <% end %> <%= link_to 'Back to Home', root_path, class: "btn btn-secondary" %> diff --git a/config/routes.rb b/config/routes.rb index a04d0da..487bffd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -85,12 +85,13 @@ Rails.application.routes.draw do resources :forms resources :users - # Custom route for admin to create a new user - get 'admin/users/new', to: 'admin#new_user', as: :new_admin_user - post 'admin/users', to: 'admin#create_user', as: :admin_users - + resource :admin, controller: 'admin', only: [] do + get 'users/new', to: 'admin#new_user', as: 'new_user' + post 'users', to: 'admin#create_user', as: 'create_user' + # Define other routes as needed + end + - get 'home/index' # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html diff --git a/db/migrate/20240411215819_create_access_periods.rb b/db/migrate/20240411215819_create_access_periods.rb new file mode 100644 index 0000000..bcf6ddb --- /dev/null +++ b/db/migrate/20240411215819_create_access_periods.rb @@ -0,0 +1,11 @@ +class CreateAccessPeriods < ActiveRecord::Migration[7.1] + def change + create_table :access_periods do |t| + t.date :start_date + t.date :end_date + t.references :user, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 4ac86c5..cc315ae 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,16 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_04_09_215110) do +ActiveRecord::Schema[7.1].define(version: 2024_04_11_215819) do + create_table "access_periods", force: :cascade do |t| + t.date "start_date" + t.date "end_date" + t.integer "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["user_id"], name: "index_access_periods_on_user_id" + end + create_table "bank_accounts", force: :cascade do |t| t.string "institution_name" t.string "account_type" @@ -236,6 +245,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_04_09_215110) do t.index ["ssn"], name: "index_workers_on_ssn", unique: true end + add_foreign_key "access_periods", "users" add_foreign_key "employer_records", "employers" add_foreign_key "employer_records", "participants" add_foreign_key "employments", "participants" diff --git a/test/fixtures/access_periods.yml b/test/fixtures/access_periods.yml new file mode 100644 index 0000000..4c9ab21 --- /dev/null +++ b/test/fixtures/access_periods.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + start_date: 2024-04-11 + end_date: 2024-04-11 + user: one + +two: + start_date: 2024-04-11 + end_date: 2024-04-11 + user: two diff --git a/test/models/access_period_test.rb b/test/models/access_period_test.rb new file mode 100644 index 0000000..9e775ca --- /dev/null +++ b/test/models/access_period_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class AccessPeriodTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end