Skip to content

Commit

Permalink
[rubyforgood#5777] Refactor large method
Browse files Browse the repository at this point in the history
  • Loading branch information
priyapower committed Jul 17, 2024
1 parent f6e0073 commit 5d249ac
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions app/controllers/case_contacts/form_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,21 @@ class CaseContacts::FormController < ApplicationController

# wizard_path
def show
authorize @case_contact
manage_form_step
get_cases_and_contact_types
@page = wizard_steps.index(step) + 1
@total_pages = steps.count

render_wizard
wizard_path
end

def update
authorize @case_contact
@page = wizard_steps.index(step) + 1
@total_pages = steps.count
@nav_step = params[:nav_step]

manage_form_step(step_navigation: true)
params[:case_contact][:status] = step.to_s if !@case_contact.active?
remove_unwanted_contact_types
remove_nil_draft_ids

if CaseContactUpdateService.new(@case_contact).update_attrs(case_contact_params)
respond_to do |format|
format.html {
if @nav_step.present?
jump_to(@nav_step.split("/").last.to_sym)
render_wizard @case_contact, {}, {case_contact_id: @case_contact.id}
elsif step == steps.last
finish_editing
else
render_wizard @case_contact, {}, {case_contact_id: @case_contact.id}
end
manage_navigation
}
format.json { head :ok }
end
Expand All @@ -59,6 +44,13 @@ def set_case_contact
@case_contact = CaseContact.find(params[:case_contact_id])
end

def manage_form_step(step_navigation: false)
authorize @case_contact
@page = wizard_steps.index(step) + 1
@total_pages = steps.count
@nav_step = params[:nav_step] if step_navigation
end

def get_cases_and_contact_types
@casa_cases = policy_scope(current_organization.casa_cases)
@casa_cases = @casa_cases.where(id: @case_contact.casa_case_id) if @case_contact.active?
Expand All @@ -72,6 +64,17 @@ def get_cases_and_contact_types
@selected_contact_type_ids = @case_contact.contact_type_ids
end

def manage_navigation
if @nav_step.present?
jump_to(@nav_step.split("/").last.to_sym)
render_wizard @case_contact, {}, {case_contact_id: @case_contact.id}
elsif step == steps.last
finish_editing
else
render_wizard @case_contact, {}, {case_contact_id: @case_contact.id}
end
end

def finish_editing
message = ""
send_reimbursement_email(@case_contact)
Expand Down

0 comments on commit 5d249ac

Please sign in to comment.