diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index 836041c..861e3a7 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -8,7 +8,10 @@ class ParticipantsController < ApplicationController def show + @participant = Participant.includes(:employments).find(params[:id]) + @workers = @participant.workers # Fetch associated workers end + def new @participant = Participant.new @@ -16,34 +19,22 @@ class ParticipantsController < ApplicationController 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 + 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 - 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 + rescue ActiveRecord::RecordInvalid => e + Rails.logger.error("Participant creation failed: #{e.message}") + flash.now[:alert] = "Error: #{e.message}" render :new - end def edit @@ -62,6 +53,17 @@ class ParticipantsController < ApplicationController redirect_to participants_url, notice: 'Participant was successfully destroyed.' end + def link_worker + @participant = Participant.find(params[:id]) + employment = @participant.employments.new(employment_params) + if employment.save + redirect_to @participant, notice: 'Worker was successfully linked.' + else + # Assuming you have a show or edit page to render + render 'show', alert: 'Failed to link worker.' + end + end + private def set_participant @@ -83,5 +85,8 @@ class ParticipantsController < ApplicationController gender: participant.gender # Mapping Gender } end - + + def employment_params + params.require(:employment).permit(:worker_id, :start_date, :end_date) + end end diff --git a/app/models/employment.rb b/app/models/employment.rb new file mode 100644 index 0000000..1846279 --- /dev/null +++ b/app/models/employment.rb @@ -0,0 +1,4 @@ +class Employment < ApplicationRecord + belongs_to :worker + belongs_to :participant +end diff --git a/app/models/participant.rb b/app/models/participant.rb index 27b3a6a..3131413 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -1,12 +1,13 @@ class Participant < ApplicationRecord - # This makes the association to Employer optional + # Associations belongs_to :employer, optional: true - belongs_to :worker, optional: true + has_many :employments + has_many :workers, through: :employments + + # Validations validates :first_name, presence: true validates :last_name, presence: true # Other associations has_and_belongs_to_many :programs - has_many :workers end - diff --git a/app/models/worker.rb b/app/models/worker.rb index de3f1cb..930f3c8 100644 --- a/app/models/worker.rb +++ b/app/models/worker.rb @@ -1,6 +1,7 @@ class Worker < ApplicationRecord # One-to-many relationship with Participants - has_many :participants + has_many :participants, through: :employments + has_many :employments # Many-to-many relationship with Employers through Participants has_many :employers, through: :participants diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index d037815..27f726b 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -22,7 +22,7 @@ <% end %>
- <%= f.submit "Log in", class: 'btn btn-primary' %> + <%= f.submit "Log in", class: 'btn btn-dark' %>
<% end %> diff --git a/app/views/participants/show.html.erb b/app/views/participants/show.html.erb index 47477c4..7b02c67 100644 --- a/app/views/participants/show.html.erb +++ b/app/views/participants/show.html.erb @@ -59,3 +59,46 @@ + + +

Linked Workers

+ + + + + + + + + + + <% @participant.employments.each do |employment| %> + + + + + + <% end %> + +
Worker NameStart DateEnd Date
<%= employment.worker.name %><%= employment.start_date %><%= employment.end_date %>
+ + + +<%= form_with(url: link_worker_participant_path(@participant), method: :post) do |form| %> +
+ <%= form.label :worker_id, "Select Worker" %> + <%= form.collection_select :worker_id, Worker.all, :id, :name, include_blank: true %> +
+ +
+ <%= form.label :start_date %> + <%= form.date_field :start_date %> +
+ +
+ <%= form.label :end_date %> + <%= form.date_field :end_date %> +
+ + <%= form.submit "Link Worker" %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index c33e69e..fa584e5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,11 @@ Rails.application.routes.draw do root to: 'devise/sessions#new' end - resources :participants + resources :participants do + member do + post 'link_worker' + end + end resources :workers resources :vendors resources :employers do diff --git a/db/migrate/20240123042316_create_employments.rb b/db/migrate/20240123042316_create_employments.rb new file mode 100644 index 0000000..0f7d9fd --- /dev/null +++ b/db/migrate/20240123042316_create_employments.rb @@ -0,0 +1,12 @@ +class CreateEmployments < ActiveRecord::Migration[7.1] + def change + create_table :employments do |t| + t.references :worker, null: false, foreign_key: true + t.references :participant, 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 8dfeec7..e778d6b 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_23_023259) do +ActiveRecord::Schema[7.1].define(version: 2024_01_23_042316) do create_table "employers", force: :cascade do |t| t.string "name" t.string "address" @@ -29,6 +29,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_023259) do t.integer "employer_id", null: false end + create_table "employments", force: :cascade do |t| + t.integer "worker_id", null: false + t.integer "participant_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 ["participant_id"], name: "index_employments_on_participant_id" + t.index ["worker_id"], name: "index_employments_on_worker_id" + end + create_table "participants", force: :cascade do |t| t.string "first_name" t.string "address" @@ -103,6 +114,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_023259) do t.datetime "updated_at", null: false end + add_foreign_key "employments", "participants" + add_foreign_key "employments", "workers" add_foreign_key "participants", "employers" add_foreign_key "participants", "workers" end diff --git a/test/fixtures/employments.yml b/test/fixtures/employments.yml new file mode 100644 index 0000000..06aa81e --- /dev/null +++ b/test/fixtures/employments.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + worker: one + participant: one + start_date: 2024-01-22 + end_date: 2024-01-22 + +two: + worker: two + participant: two + start_date: 2024-01-22 + end_date: 2024-01-22 diff --git a/test/models/employment_test.rb b/test/models/employment_test.rb new file mode 100644 index 0000000..23ecf1d --- /dev/null +++ b/test/models/employment_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class EmploymentTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end