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 @@ +
+

Edit Employer Record

+ + <%= form_with(model: @employer_record, class: 'form') do |form| %> + <% if @employer_record.errors.any? %> +
+

<%= pluralize(@employer_record.errors.count, "error") %> prohibited this employer record from being saved:

+ +
+ <% end %> + +
+ <%= form.label :participant_id %> + <%= form.number_field :participant_id, class: 'form-control' %> +
+ +
+ <%= form.label :employer_id %> + <%= form.number_field :employer_id, class: 'form-control' %> +
+ +
+ <%= form.label :start_date %> + <%= form.date_field :start_date, class: 'form-control' %> +
+ +
+ <%= form.label :end_date %> + <%= form.date_field :end_date, class: 'form-control' %> +
+ +
+ <%= form.submit 'Save Changes', class: 'btn btn-primary' %> + <%= link_to 'Delete Record', employer_record_path(@employer_record), + method: :delete, + data: { confirm: 'Are you sure?' }, + class: 'btn btn-danger' %> +
+ <% end %> + + +
diff --git a/app/views/employers/show.html.erb b/app/views/employers/show.html.erb index cacad7d..4cce2be 100644 --- a/app/views/employers/show.html.erb +++ b/app/views/employers/show.html.erb @@ -53,3 +53,44 @@ + +
+ + + +
+
+

Linked Participants

+ <% if @employer.employer_records.any? %> + + + + + + + + + + + <% @employer.employer_records.each do |employer_record| %> + + <% participant = employer_record.participant %> + + + + + + <% end %> + +
Participant NameStart DateEnd DateActions
<%= "#{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' %> +
+ <% else %> +

No linked participants.

+ <% end %> +
+
+ + + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9be7724..3ceb69a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,6 +36,11 @@ Rails.application.routes.draw do end resources :employments, only: [:edit, :update, :destroy] + resources :employer_records, only: [:edit, :update, :destroy] do + collection do + post 'link_participant' + end + end resources :service_contracts resources :employers do diff --git a/db/migrate/20240203000258_add_dates_to_participants.rb b/db/migrate/20240203000258_add_dates_to_participants.rb new file mode 100644 index 0000000..71048e8 --- /dev/null +++ b/db/migrate/20240203000258_add_dates_to_participants.rb @@ -0,0 +1,6 @@ +class AddDatesToParticipants < ActiveRecord::Migration[7.1] + def change + add_column :participants, :start_date, :date + add_column :participants, :end_date, :date + end +end diff --git a/db/migrate/20240203002211_create_employer_records.rb b/db/migrate/20240203002211_create_employer_records.rb new file mode 100644 index 0000000..787c978 --- /dev/null +++ b/db/migrate/20240203002211_create_employer_records.rb @@ -0,0 +1,12 @@ +class CreateEmployerRecords < ActiveRecord::Migration[7.1] + def change + create_table :employer_records do |t| + t.references :participant, null: false, foreign_key: true + t.references :employer, null: false, foreign_key: true + t.date :start_date + t.date :end_date + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 0c02f26..4b695ab 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_01_31_072940) do +ActiveRecord::Schema[7.1].define(version: 2024_02_03_002211) do + create_table "employer_records", force: :cascade do |t| + t.integer "participant_id", null: false + t.integer "employer_id", null: false + t.date "start_date" + t.date "end_date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["employer_id"], name: "index_employer_records_on_employer_id" + t.index ["participant_id"], name: "index_employer_records_on_participant_id" + end + create_table "employers", force: :cascade do |t| t.string "first_name" t.string "phone" @@ -63,6 +74,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_31_072940) do t.string "city" t.string "state" t.string "zip" + t.date "start_date" + t.date "end_date" t.index ["employer_id"], name: "index_participants_on_employer_id" t.index ["last_name"], name: "index_participants_on_last_name" t.index ["mci"], name: "index_participants_on_mci" @@ -144,6 +157,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_31_072940) do t.string "zip" end + add_foreign_key "employer_records", "employers" + add_foreign_key "employer_records", "participants" add_foreign_key "employments", "participants" add_foreign_key "employments", "workers" add_foreign_key "participants", "employers" diff --git a/test/controllers/employer_records_controller_test.rb b/test/controllers/employer_records_controller_test.rb new file mode 100644 index 0000000..17c25ab --- /dev/null +++ b/test/controllers/employer_records_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class EmployerRecordsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/employer_records.yml b/test/fixtures/employer_records.yml new file mode 100644 index 0000000..a23833c --- /dev/null +++ b/test/fixtures/employer_records.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + participant: one + employer: one + start_date: 2024-02-02 + end_date: 2024-02-02 + +two: + participant: two + employer: two + start_date: 2024-02-02 + end_date: 2024-02-02 diff --git a/test/models/employer_record_test.rb b/test/models/employer_record_test.rb new file mode 100644 index 0000000..22c713c --- /dev/null +++ b/test/models/employer_record_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class EmployerRecordTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end