Skip to content

Commit

Permalink
Refactor controller
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMacPherson committed Jan 22, 2025
1 parent c659ee7 commit b9376be
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,36 +21,24 @@ 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
render :edit
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit b9376be

Please sign in to comment.