From b9376be57b77555e2700108d89dee8db0368e7ea Mon Sep 17 00:00:00 2001 From: Kyle MacPherson Date: Wed, 22 Jan 2025 12:29:36 +0000 Subject: [PATCH] Refactor controller --- ...rofessional_body_memberships_controller.rb | 37 ++++++++----------- .../confirm_destroy.html.slim | 2 +- .../edit.html.slim | 2 +- .../new.html.slim | 2 +- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/app/controllers/jobseekers/profiles/professional_body_memberships_controller.rb b/app/controllers/jobseekers/profiles/professional_body_memberships_controller.rb index 02a6746ec5..bf6849273e 100644 --- a/app/controllers/jobseekers/profiles/professional_body_memberships_controller.rb +++ b/app/controllers/jobseekers/profiles/professional_body_memberships_controller.rb @@ -1,11 +1,18 @@ class Jobseekers::Profiles::ProfessionalBodyMembershipsController < Jobseekers::ProfilesController - helper_method :jobseeker_profile, :form, :professional_body_membership + helper_method :jobseeker_profile, :professional_body_membership - def new; end - def edit; end + def new + @form = Jobseekers::ProfessionalBodyMembershipForm.new + end + + def edit + @form = Jobseekers::ProfessionalBodyMembershipForm.new(professional_body_membership.slice(:name, :membership_type, :membership_number, :date_membership_obtained, :exam_taken)) + end def create - if form.valid? + @form = Jobseekers::ProfessionalBodyMembershipForm.new(professional_body_memberships_form_params) + + if @form.valid? profile.professional_body_memberships.create!(professional_body_memberships_form_params) redirect_to review_jobseekers_profile_professional_body_memberships_path else @@ -14,7 +21,8 @@ def create end def update - if form.valid? + @form = Jobseekers::ProfessionalBodyMembershipForm.new(professional_body_memberships_form_params) + if @form.valid? professional_body_membership.update!(professional_body_memberships_form_params) redirect_to review_jobseekers_profile_professional_body_memberships_path else @@ -22,28 +30,15 @@ def update end end - def confirm_destroy; end + def confirm_destroy + @form = Jobseekers::ProfessionalBodyMembershipForm.new + end def destroy professional_body_membership.destroy! redirect_to review_jobseekers_profile_professional_body_memberships_path, success: t(".success") end - def form - @form ||= Jobseekers::ProfessionalBodyMembershipForm.new(form_attributes) - end - - def form_attributes - case action_name - when "new" - {} - when "create", "update" - professional_body_memberships_form_params - when "edit" - professional_body_membership.slice(:name, :membership_type, :membership_number, :date_membership_obtained, :exam_taken) - end - end - def professional_body_memberships_form_params params.require(:jobseekers_professional_body_membership_form) .permit(:name, :membership_type, :membership_number, :date_membership_obtained, :exam_taken) diff --git a/app/views/jobseekers/profiles/professional_body_memberships/confirm_destroy.html.slim b/app/views/jobseekers/profiles/professional_body_memberships/confirm_destroy.html.slim index a258ddf9ee..b4147fa6da 100644 --- a/app/views/jobseekers/profiles/professional_body_memberships/confirm_destroy.html.slim +++ b/app/views/jobseekers/profiles/professional_body_memberships/confirm_destroy.html.slim @@ -9,7 +9,7 @@ span.govuk-caption-l = t(".caption") h1.govuk-heading-l = t(".heading") - = form_for form, url: jobseekers_profile_professional_body_membership_path(professional_body_membership), method: :delete do |f| + = form_for @form, url: jobseekers_profile_professional_body_membership_path(professional_body_membership), method: :delete do |f| = govuk_summary_list classes: "govuk-!-margin-bottom-10" do |summary_list| - summary_list.with_row do |row| - row.with_key text: t("helpers.label.jobseekers_professional_body_membership_form.name") diff --git a/app/views/jobseekers/profiles/professional_body_memberships/edit.html.slim b/app/views/jobseekers/profiles/professional_body_memberships/edit.html.slim index b37dcd0ef0..699276d66b 100644 --- a/app/views/jobseekers/profiles/professional_body_memberships/edit.html.slim +++ b/app/views/jobseekers/profiles/professional_body_memberships/edit.html.slim @@ -5,7 +5,7 @@ span.govuk-caption-l = t(".page_title") h1.govuk-heading-xl = t(".heading") - = form_for form, url: jobseekers_profile_professional_body_membership_path, method: :put do |f| + = form_for @form, url: jobseekers_profile_professional_body_membership_path, method: :put do |f| = f.govuk_error_summary = f.govuk_text_field :name, label: { size: "s" }, aria: { required: true } diff --git a/app/views/jobseekers/profiles/professional_body_memberships/new.html.slim b/app/views/jobseekers/profiles/professional_body_memberships/new.html.slim index f109706817..7c4f5defb1 100644 --- a/app/views/jobseekers/profiles/professional_body_memberships/new.html.slim +++ b/app/views/jobseekers/profiles/professional_body_memberships/new.html.slim @@ -5,7 +5,7 @@ span.govuk-caption-l = t(".page_title") h1.govuk-heading-xl = t(".heading") - = form_for form, url: jobseekers_profile_professional_body_memberships_path(profile), method: :post do |f| + = form_for @form, url: jobseekers_profile_professional_body_memberships_path(profile), method: :post do |f| = f.govuk_error_summary = f.govuk_text_field :name, label: { size: "s" }, aria: { required: true }