diff --git a/app/controllers/employers_controller.rb b/app/controllers/employers_controller.rb index 22a7a5c..1518fe3 100644 --- a/app/controllers/employers_controller.rb +++ b/app/controllers/employers_controller.rb @@ -44,6 +44,19 @@ class EmployersController < ApplicationController redirect_to employers_url, notice: 'Employer was successfully destroyed.' end + # GET /employers/search + # This action responds to the auto-complete AJAX requests + def search + if params[:term].present? + @employers = Employer.where("name LIKE ?", "%#{params[:term]}%") + else + @employers = Employer.none + end + + # Respond with a JSON array of employer names and IDs + render json: @employers.map { |e| { label: e.name, value: e.id } } + end + private def set_employer diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index cde1f19..fa26f7b 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -1,49 +1,84 @@ class ParticipantsController < ApplicationController - before_action :set_participant, only: [:show, :edit, :update, :destroy] + before_action :set_participant, only: [:show, :edit, :update, :destroy] - def index - @participants = Participant.all - end - - def show - end - - def new - @participant = Participant.new - end - - def create - @participant = Participant.new(participant_params) - if @participant.save - redirect_to @participant, notice: 'Participant was successfully created.' - else - render :new - end - end - - def edit - end - - def update - if @participant.update(participant_params) - redirect_to @participant, notice: 'Participant was successfully updated.' - else - render :edit - end - end - - def destroy - @participant.destroy - redirect_to participants_url, notice: 'Participant was successfully destroyed.' - end - - private - def set_participant - @participant = Participant.find(params[:id]) - end - - def participant_params - params.require(:participant).permit(:name, :address, :phone, :email, :mci, :dob, :ssn, :gender, :employer_id) - end + def index + @participants = Participant.all end - \ No newline at end of file + + def show + end + + def new + @participant = Participant.new + end + + def create + @participant = Participant.new(participant_params) + + begin + Participant.transaction do + if params[:is_employer] == 'yes' + employer = Employer.create!(employer_params_from_participant(@participant)) + @participant.employer = employer + end + + if @participant.save + redirect_to @participant, notice: 'Participant was successfully created.' + else + raise ActiveRecord::Rollback, "Participant could not be saved" + end + end + rescue ActiveRecord::RecordInvalid => e + # Log the error and set a flash message for the user + Rails.logger.error("Participant creation failed: #{e.message}") + flash.now[:alert] = "Error: #{e.message}" + render :new + end + + + + rescue => e + puts "Error: #{e.message}" # Log the error message + flash.now[:alert] = "Error: #{e.message}" # Optionally, display the error message to the user + render :new + + end + + def edit + end + + def update + if @participant.update(participant_params) + redirect_to @participant, notice: 'Participant was successfully updated.' + else + render :edit + end + end + + def destroy + @participant.destroy + redirect_to participants_url, notice: 'Participant was successfully destroyed.' + end + + private + + def set_participant + @participant = Participant.find(params[:id]) + end + + def participant_params + params.require(:participant).permit(:name, :address, :phone, :email, :mci, :dob, :ssn, :gender, :employer_id) + end + + def employer_params_from_participant(participant) + { + name: participant.name, + address: participant.address, + phone: participant.phone, + email: participant.email, + dob: participant.dob, # Mapping Date of Birth + ssn: participant.ssn, # Mapping SSN + gender: participant.gender # Mapping Gender + } + end +end diff --git a/app/models/participant.rb b/app/models/participant.rb index 170711c..a164e70 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -1,5 +1,10 @@ class Participant < ApplicationRecord - belongs_to :employer + # This makes the association to Employer optional + belongs_to :employer, optional: true + belongs_to :worker, optional: true + + # Other associations has_and_belongs_to_many :programs has_many :workers end + diff --git a/app/views/employers/index.html.erb b/app/views/employers/index.html.erb index 534fd41..35ee8a0 100644 --- a/app/views/employers/index.html.erb +++ b/app/views/employers/index.html.erb @@ -10,10 +10,10 @@ Address Phone Email - TIN + DOB - SSN - Gender + + Actions @@ -24,14 +24,13 @@ <%= employer.address %> <%= employer.phone %> <%= employer.email %> - <%= employer.tin %> + <%= employer.dob %> - <%= employer.ssn %> - <%= employer.gender %> + + <%= link_to 'Show', employer, class: 'btn btn-sm btn-info' %> <%= link_to 'Edit', edit_employer_path(employer), class: 'btn btn-sm btn-warning' %> - <%= link_to 'Destroy', @employer, method: :delete, data: { confirm: 'Are you sure?', turbo: 'false' }, class: 'btn btn-sm btn-danger' %> <% end %> diff --git a/app/views/participants/_form.html.erb b/app/views/participants/_form.html.erb index 17a175a..d9c17d4 100644 --- a/app/views/participants/_form.html.erb +++ b/app/views/participants/_form.html.erb @@ -30,6 +30,17 @@ <%= form.email_field :email, id: 'email-field', class: 'form-control', required: true, placeholder: 'Make sure to include @ sign' %> +
+ <%= form.label :is_employer, 'Is this participant also an employer?', class: 'form-label' %> +
+ <%= radio_button_tag 'is_employer', 'yes', false, class: 'form-check-input' %> + <%= label_tag 'is_employer_yes', 'Yes', class: 'form-check-label' %> + <%= radio_button_tag 'is_employer', 'no', true, class: 'form-check-input' %> + <%= label_tag 'is_employer_no', 'No', class: 'form-check-label' %> +
+
+ +
<%= form.label :mci, 'MCI', class: 'form-label' %> <%= form.text_field :mci, class: 'form-control', placeholder: 'Required for Participant' %> @@ -138,3 +149,30 @@ + + + + + diff --git a/app/views/participants/index.html.erb b/app/views/participants/index.html.erb index d6e0a0c..961762f 100644 --- a/app/views/participants/index.html.erb +++ b/app/views/participants/index.html.erb @@ -23,9 +23,9 @@ <%= participant.address %> <%= participant.phone %> <%= participant.email %> - <%= participant.program %> + <%= participant.programs.map(&:name).join(", ") %> <%= participant.mci %> - <%= participant.dob.strftime('%Y-%m-%d') if participant.dob %> + <%= participant.dob.strftime('%B, %d, %Y') if participant.dob %> <%= participant.ssn %> <%= participant.gender %> <%= link_to 'Show', participant, class: 'btn btn-sm btn-info' %> diff --git a/app/views/participants/show.html.erb b/app/views/participants/show.html.erb index df0362b..ac41440 100644 --- a/app/views/participants/show.html.erb +++ b/app/views/participants/show.html.erb @@ -15,6 +15,37 @@ <%= @participant.address %>

+

+ Phone: + <%= @participant.phone %> +

+ +

+ Email: + <%= @participant.email %> +

+ +

+ MCI: + <%= @participant.mci %> +

+ +

+ DOB: + <%= @participant.dob.strftime('%B, %d, %Y') if @participant.dob.present? %> +

+ + +

+ SSN: + <%= @participant.ssn %> +

+ +

+ Gender: + <%= @participant.gender %> +

+
diff --git a/config/routes.rb b/config/routes.rb index 9852521..c33e69e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,9 +10,12 @@ Rails.application.routes.draw do end resources :participants - resources :employers resources :workers resources :vendors + resources :employers do + get 'search', on: :collection + end + diff --git a/db/migrate/20240120051441_change_worker_id_to_be_optional_in_participants.rb b/db/migrate/20240120051441_change_worker_id_to_be_optional_in_participants.rb new file mode 100644 index 0000000..f7681d8 --- /dev/null +++ b/db/migrate/20240120051441_change_worker_id_to_be_optional_in_participants.rb @@ -0,0 +1,5 @@ +class ChangeWorkerIdToBeOptionalInParticipants < ActiveRecord::Migration[6.0] + def change + change_column_null :participants, :worker_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 76a6169..7b23bfd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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_01_17_025159) do +ActiveRecord::Schema[7.1].define(version: 2024_01_20_051441) do create_table "employers", force: :cascade do |t| t.string "name" t.string "address" @@ -41,7 +41,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_17_025159) do t.integer "employer_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "worker_id", null: false + t.integer "worker_id" t.index ["employer_id"], name: "index_participants_on_employer_id" t.index ["worker_id"], name: "index_participants_on_worker_id" end