diff --git a/app/controllers/employer_records_controller.rb b/app/controllers/employer_records_controller.rb new file mode 100644 index 0000000..c3fe581 --- /dev/null +++ b/app/controllers/employer_records_controller.rb @@ -0,0 +1,54 @@ +class EmployerRecordsController < ApplicationController + before_action :set_employer_record, only: [:edit, :update] + + def edit + # Logic to edit an EmployerRecord + # Display a form to edit the EmployerRecord + end + + def update + # Logic to update an EmployerRecord + # Called when the edit form is submitted + if @employer_record.update(employer_record_params) + redirect_to employer_path(@employer_record.employer), notice: 'Employer record was successfully updated.' + else + render :edit + end + end + + def link_participant_to_employer + # Assuming you have the employer and participant IDs + employer_id = params[:employer_id] + participant_id = params[:participant_id] + + employer_record = EmployerRecord.new(employer_id: employer_id, participant_id: participant_id, start_date: Date.today) + + if employer_record.save + # Redirect or render success message + else + # Handle error + end + end + + + def destroy + if @employer_record + @employer_record.destroy + redirect_to employers_url, notice: 'Employer record was successfully deleted.' + else + redirect_to some_error_handling_path, alert: 'Employer record not found.' + end + end + + + + private + + def set_employer_record + @employer_record = EmployerRecord.find(params[:id]) + end + + def employer_record_params + params.require(:employer_record).permit(:participant_id, :employer_id, :start_date, :end_date) + end +end diff --git a/app/controllers/employers_controller.rb b/app/controllers/employers_controller.rb index 88ba859..705fb36 100644 --- a/app/controllers/employers_controller.rb +++ b/app/controllers/employers_controller.rb @@ -8,7 +8,10 @@ class EmployersController < ApplicationController # GET /employers/:id def show + @employer = Employer.includes(:employer_records, :participants).find(params[:id]) + @employer_records = @employer.employer_records.includes(:participant) end + # GET /employers/new def new diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index 0c44d03..00542a9 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -24,19 +24,15 @@ class ParticipantsController < ApplicationController Participant.transaction do if params[:is_employer] == 'yes' - employer = Employer.new(employer_params_from_participant(@participant)) - if employer.save - @participant.employer = employer - else - Rails.logger.debug "Employer creation failed: #{employer.errors.full_messages}" - raise ActiveRecord::Rollback, "Employer could not be saved" - end + create_employer_and_employer_record_for(@participant) + elsif params[:employer_id].present? + @participant.employer_id = params[:employer_id] end if @participant.save redirect_to @participant, notice: 'Participant was successfully created.' else - raise ActiveRecord::Rollback, "Participant could not be saved" + render :new end end rescue ActiveRecord::RecordInvalid => e @@ -99,14 +95,6 @@ class ParticipantsController < ApplicationController end end - private - - def service_contract_params - # Define your service contract parameters here - end - - - private def set_participant @@ -157,6 +145,16 @@ class ParticipantsController < ApplicationController def service_contract_params params.require(:service_contract).permit(:vendor_id, :start_date, :end_date) end - - + + def create_employer_and_employer_record_for(participant) + employer = Employer.new(employer_params_from_participant(participant)) + if employer.save + participant.employer_id = employer.id + # Create an EmployerRecord with start_date as current date + EmployerRecord.create!(participant: participant, employer: employer, start_date: Date.today) + else + participant.errors.add(:base, "Employer creation failed: #{employer.errors.full_messages.join(', ')}") + raise ActiveRecord::Rollback + end + end end diff --git a/app/helpers/employer_records_helper.rb b/app/helpers/employer_records_helper.rb new file mode 100644 index 0000000..5bc6c8b --- /dev/null +++ b/app/helpers/employer_records_helper.rb @@ -0,0 +1,2 @@ +module EmployerRecordsHelper +end diff --git a/app/models/employer.rb b/app/models/employer.rb index 8bacad4..d2cb5ca 100644 --- a/app/models/employer.rb +++ b/app/models/employer.rb @@ -1,6 +1,9 @@ class Employer < ApplicationRecord has_many :participants has_many :workers, through: :participants + has_many :employer_records + has_many :participants, through: :employer_records + # Association with Vendor if needed def full_name diff --git a/app/models/employer_record.rb b/app/models/employer_record.rb new file mode 100644 index 0000000..30bc2b5 --- /dev/null +++ b/app/models/employer_record.rb @@ -0,0 +1,4 @@ +class EmployerRecord < ApplicationRecord + belongs_to :employer + belongs_to :participant +end diff --git a/app/models/participant.rb b/app/models/participant.rb index 08f922b..0cf49b2 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -5,6 +5,9 @@ class Participant < ApplicationRecord has_many :workers, through: :employments has_many :service_contracts has_many :vendors, through: :service_contracts + has_many :employer_records + has_many :employers, through: :employer_records + # Validations validates :first_name, presence: true diff --git a/app/views/employer_records/edit.html.erb b/app/views/employer_records/edit.html.erb new file mode 100644 index 0000000..ed207c5 --- /dev/null +++ b/app/views/employer_records/edit.html.erb @@ -0,0 +1,46 @@ +
Participant Name | +Start Date | +End Date | +Actions | +
---|---|---|---|
<%= "#{participant.first_name} #{participant.last_name}" %> | +<%= employer_record.start_date.strftime('%B %d, %Y') if employer_record.start_date %> | +<%= employer_record.end_date.strftime('%B %d, %Y') if employer_record.end_date %> | ++ <%= link_to 'View', participant_path(participant), class: 'btn btn-sm btn-secondary' %> + <%= link_to 'Edit', edit_employer_record_path(employer_record), class: 'btn btn-sm btn-info' %> + | +
No linked participants.
+ <% end %> +