Did a lot. Onboarding under Demographics now works for the participant model correctly linking admin forms to the right model. Styling has also been complete. Still need to do other models and check for multiple inputs on form role submission.

This commit is contained in:
Ben 2024-02-16 23:36:48 -06:00
parent d0149a50e2
commit 84c0092701
20 changed files with 284 additions and 16 deletions

View File

@ -1,5 +1,3 @@
# app/controllers/forms_controller.rb
class FormsController < ApplicationController
before_action :set_form, only: [:show, :edit, :update, :destroy]
@ -45,7 +43,7 @@ class FormsController < ApplicationController
end
def form_params
params.require(:form).permit(:name) # Adjust attributes as needed
params.require(:form).permit(:name, :required, :program, :role)
end
def check_admin

View File

@ -0,0 +1,64 @@
class OnboardingsController < ApplicationController
before_action :set_owner
def index
# Make sure to set @owner first. If not found, you should redirect or handle the error appropriately.
set_owner
if @owner
# Initialize onboarding items for each form if they don't exist yet for the owner.
@forms = Form.all
@forms.each do |form|
@owner.onboarding_items.find_or_initialize_by(form: form)
end
# After building or finding the onboarding items, load them for the view.
# This ensures we're only working with items related to the current owner.
@onboarding_items = @owner.onboarding_items.includes(:form)
else
# Handle the scenario where @owner is not found. Redirect or show an error.
redirect_to root_path, alert: "Owner not found."
end
end
def create
# Assuming `onboarding_items_params` method will handle mass assignment
@onboarding_item = @owner.onboarding_items.build(onboarding_items_params)
if @onboarding_item.save
redirect_to polymorphic_path([@owner, :onboardings]), notice: 'Onboarding item was successfully created.'
else
# This assumes you have an instance variable @forms for the form dropdown in the view
@forms = Form.all
render :index, status: :unprocessable_entity
end
end
def submit_onboarding
params[:onboarding_items].each do |id, item_params|
onboarding_item = OnboardingItem.find(id)
onboarding_item.update(item_params.permit(:note, :date_completed))
end
redirect_to polymorphic_path([@owner]), notice: 'Onboarding information updated successfully.'
end
private
def set_owner
# Example: finds "participant_id", sets @owner to the corresponding Participant object
params.each do |name, value|
if name =~ /(.+)_id$/
model = $1.classify.constantize
@owner = model.find_by(id: value)
break if @owner # Break if owner is found
end
end
redirect_to(root_path, alert: "Owner not found.") unless @owner
end
def onboarding_items_params
# Ensure you permit the correct parameters based on your form fields
params.require(:onboarding_item).permit(:form_id, :note, :date_completed)
end
end

View File

@ -124,6 +124,12 @@ class ParticipantsController < ApplicationController
end
end
def onboarding
@participant = Participant.find(params[:id])
@onboarding_items = @participant.onboarding_items.includes(:form)
end
private
def handle_employer_creation_for_participant(participant)
@ -222,7 +228,6 @@ class ParticipantsController < ApplicationController
participant.errors.add(:base, "Employer details could not be updated: #{employer.errors.full_messages.join(', ')}")
# You might not need to throw :abort if you're handling this logic in the controller.
end
end
end
end

View File

@ -0,0 +1,13 @@
module OnboardingsHelper
def submit_onboarding_path_helper(owner)
case owner
when Participant
submit_onboarding_participant_onboardings_path(owner)
when Worker
submit_onboarding_worker_onboardings_path(owner)
# Add cases for other owner types
else
root_path # Fallback path
end
end
end

View File

@ -1,2 +1,23 @@
class Form < ApplicationRecord
end
# Assuming each form can have many onboarding items associated with different owners.
has_many :onboarding_items
validates :name, presence: true
validates :role, inclusion: { in: %w[Participant Worker Vendor Employer] }
# Include more validations as necessary
after_save :update_onboarding_items
private
def update_onboarding_items
# Dynamically find the model class based on the role. Ensure 'role' matches your model name exactly.
owner_class = role.constantize
# For each instance of the model, find or create an onboarding item linked to this form.
owner_class.find_each do |owner|
OnboardingItem.find_or_create_by(owner: owner, form: self)
end
end
end

View File

@ -0,0 +1,4 @@
class OnboardingItem < ApplicationRecord
belongs_to :owner, polymorphic: true
belongs_to :form
end

View File

@ -1,4 +1,6 @@
class Participant < ApplicationRecord
after_create :create_onboarding_items_for_forms
# Associations
belongs_to :employer, optional: false
has_many :employments
@ -9,6 +11,8 @@ class Participant < ApplicationRecord
has_many :employers, through: :employer_records
has_and_belongs_to_many :programs
has_many :bank_accounts, as: :owner
has_many :onboarding_items, as: :owner
accepts_nested_attributes_for :onboarding_items
# Validations
validates :first_name, :last_name, :ssn, presence: true
@ -25,4 +29,12 @@ class Participant < ApplicationRecord
# Check if the SSN already exists in the Employer model, excluding the current participant's employer
Employer.where.not(id: employer_id).exists?(ssn: ssn)
end
def create_onboarding_items_for_forms
# Fetch all forms relevant to participants
forms = Form.where(role: 'Participant')
forms.each do |form|
self.onboarding_items.create(form: form)
end
end
end

View File

@ -1,3 +1,13 @@
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="demographicsDropdown" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Demographics
</button>
<div class="dropdown-menu" aria-labelledby="demographicsDropdown">
<li><a class="dropdown-item" href="<%= polymorphic_path([@employer, :onboardings]) %>">Onboarding</a></li>
<!-- Add other links as needed -->
</div>
</div>
<div class="container mt-5">
<div class="row">
<div class="col-12">

View File

@ -1,2 +1,13 @@
<h1>Edit Form</h1>
<%= render 'form', form: @form %>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<h1 class="mb-4 text-center">Edit Form</h1>
<%= render 'form', form: @form %>
<div class="mt-3 text-center">
<%= link_to 'Back to List', forms_path, class: "btn btn-secondary" %>
</div>
</div>
</div>
</div>

View File

@ -23,8 +23,12 @@
<td><%= form.program %></td>
<td><%= form.role %></td>
<td>
<%= link_to 'Edit', edit_form_path(form), class: 'btn btn-info btn-sm' %>
<%= link_to 'Delete', form, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger btn-sm' %>
<%= link_to edit_form_path(form), class: 'btn btn-info btn-sm' do %>
<i class="bi bi-pencil-fill" style="color: white;"></i>
<% end %>
<%= link_to form, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger btn-sm' do %>
<i class="bi bi-trash-fill"></i>
<% end %>
</td>
</tr>
<% end %>

View File

@ -0,0 +1,60 @@
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<h1 class="mb-4 text-center">Onboarding Forms</h1>
<%= form_with(model: [@owner, OnboardingItem.new], url: submit_onboarding_path_helper(@owner), local: true, html: { class: 'needs-validation', novalidate: true }) do |form| %>
<table class="table">
<thead class="table-light">
<tr>
<th>Form Name</th>
<th>Notes</th>
<th>Date Completed</th>
</tr>
</thead>
<tbody>
<% @onboarding_items.each do |item| %>
<tr>
<td><%= item.form.name %></td>
<td>
<%= text_area_tag "onboarding_items[#{item.id}][note]", item.note, class: "form-control", rows: 1 %>
</td>
<td>
<%= date_field_tag "onboarding_items[#{item.id}][date_completed]", item.date_completed, class: "form-control" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="actions mt-4">
<%= form.submit 'Save', class: 'btn btn-dark' %>
</div>
<% end %>
<div class="mt-3 text-center">
<%= link_to 'Back', polymorphic_path(@owner), class: 'btn btn-secondary' %>
</div>
</div>
</div>
</div>
<%# This is for resizing Notes area %>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('textarea.form-control').forEach(function(textarea) {
function autoResize() {
// Reset height to ensure shrinking if text is deleted
textarea.style.height = 'auto';
// Set height based on scroll height
textarea.style.height = textarea.scrollHeight + 'px';
}
// Call autoResize on input event
textarea.addEventListener('input', autoResize);
// Initialize autoResize in case of any pre-filled values
autoResize();
});
});
</script>

View File

@ -4,6 +4,7 @@
</button>
<ul class="dropdown-menu" aria-labelledby="demographicsDropdown">
<li><a class="dropdown-item" href="<%= participant_bank_accounts_path(@participant) %>">Bank Information</a></li>
<li><a class="dropdown-item" href="<%= polymorphic_path([@participant, :onboardings]) %>">Onboarding</a></li>
<!-- Add other demographic options here as needed -->
</ul>
</div>

View File

@ -3,7 +3,8 @@
Demographics
</button>
<div class="dropdown-menu" aria-labelledby="demographicsDropdown">
<%= link_to 'Bank Information', vendor_bank_accounts_path(@vendor), class: 'dropdown-item' %>
<li><a class="dropdown-item" href="<%= vendor_bank_accounts_path(@vendor) %>">Bank Information</a></li>
<li><a class="dropdown-item" href="<%= polymorphic_path([@vendor, :onboardings]) %>">Onboarding</a></li>
<!-- Add other links as needed -->
</div>
</div>

View File

@ -4,6 +4,7 @@
</button>
<ul class="dropdown-menu" aria-labelledby="demographicsDropdown">
<li><a class="dropdown-item" href="<%= worker_bank_accounts_path(@worker) %>">Bank Information</a></li>
<li><a class="dropdown-item" href="<%= polymorphic_path([@worker, :onboardings]) %>">Onboarding</a></li>
</ul>
</div>

View File

@ -15,19 +15,26 @@ Rails.application.routes.draw do
end
resources :participants do
resources :onboardings, only: [:index, :create, :update] do
collection do
post 'submit_onboarding'
end
end
resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy]
collection do
get 'search' # Define search on the collection, not on a member
end
member do
post 'link_worker' # Other member routes
post 'link_vendor' #Add this line to link a vendor
post 'link_vendor'
end
end
resources :workers do
resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy] # Added :index action
resources :workers do
resources :onboardings, only: [:index, :create, :update]
post 'submit_onboarding', on: :collection
resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy]
member do
post 'link_participant'
post 'link_employer'
@ -37,7 +44,9 @@ Rails.application.routes.draw do
resources :vendors do
resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy] # Added :index action
resources :onboardings, only: [:index, :create, :update]
post 'submit_onboarding', on: :collection
resources :bank_accounts, only: [:index, :new, :create, :edit, :update, :destroy]
collection do
get 'search'
end
@ -53,6 +62,8 @@ Rails.application.routes.draw do
resources :service_contracts
resources :employers do
resources :onboardings, only: [:index, :create, :update]
post 'submit_onboarding', on: :collection
member do
post "link_worker"
end

View File

@ -0,0 +1,12 @@
class CreateOnboardingItems < ActiveRecord::Migration[7.1]
def change
create_table :onboarding_items do |t|
t.integer :form_id
t.text :note
t.date :date_completed
t.references :owner, polymorphic: true, null: false
t.timestamps
end
end
end

13
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_16_230305) do
ActiveRecord::Schema[7.1].define(version: 2024_02_17_005105) do
create_table "bank_accounts", force: :cascade do |t|
t.string "institution_name"
t.string "account_type"
@ -81,6 +81,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_16_230305) do
t.string "role"
end
create_table "onboarding_items", force: :cascade do |t|
t.integer "form_id"
t.text "note"
t.date "date_completed"
t.string "owner_type", null: false
t.integer "owner_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["owner_type", "owner_id"], name: "index_onboarding_items_on_owner"
end
create_table "participants", force: :cascade do |t|
t.string "first_name"
t.string "phone"

View File

@ -0,0 +1,7 @@
require "test_helper"
class OnboardingsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

15
test/fixtures/onboarding_items.yml vendored Normal file
View File

@ -0,0 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
form_id: 1
note: MyText
date_completed: 2024-02-16
owner: one
owner_type: Owner
two:
form_id: 1
note: MyText
date_completed: 2024-02-16
owner: two
owner_type: Owner

View File

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