Skip to content

Commit

Permalink
Merge pull request #5945 from guswhitten/issue-5893
Browse files Browse the repository at this point in the history
Fix N+1 issues in form_controller#show
  • Loading branch information
compwron authored Jul 26, 2024
2 parents e9cafb9 + cbc1e81 commit 8b8d8e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -663,4 +663,4 @@ RUBY VERSION
ruby 3.2.2p53

BUNDLED WITH
2.5.14
2.5.16
23 changes: 11 additions & 12 deletions app/controllers/case_contacts/form_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,21 @@ def update
private

def set_case_contact
@case_contact = CaseContact.find(params[:case_contact_id])
@case_contact = CaseContact.includes(:creator, contact_topic_answers: :contact_topic)
.find(params[:case_contact_id])
end

def get_cases_and_contact_types
@casa_cases = policy_scope(current_organization.casa_cases)
@casa_cases = policy_scope(current_organization.casa_cases).includes([:volunteers])
@casa_cases = @casa_cases.where(id: @case_contact.casa_case_id) if @case_contact.active?

@contact_types = ContactType.joins(:casa_case_contact_types).where(casa_case_contact_types: {casa_case_id: @casa_cases.pluck(:id)})
unless @contact_types.present?
@contact_types = current_organization.contact_types
end
@contact_types = ContactType.includes(:contact_type_group)
.joins(:casa_case_contact_types)
.where(casa_case_contact_types: {casa_case_id: @casa_cases.pluck(:id)})

@contact_types = current_organization.contact_types.includes(:contact_type_group) if @contact_types.empty?
@contact_types.order(name: :asc)

@selected_cases = @case_contact.draft_case_ids
@selected_contact_type_ids = @case_contact.contact_type_ids
end
Expand Down Expand Up @@ -130,15 +133,11 @@ def case_contact_params
# Deletes the current associations (from the join table) only if the submitted form body has the parameters for
# the contact_type ids.
def remove_unwanted_contact_types
if params.dig(:case_contact, :contact_type_ids)
@case_contact.contact_types.clear
end
@case_contact.contact_types.clear if params.dig(:case_contact, :contact_type_ids)
end

def remove_nil_draft_ids
if params.dig(:case_contact, :draft_case_ids)
params[:case_contact][:draft_case_ids] -= [""]
end
params[:case_contact][:draft_case_ids] -= [""] if params.dig(:case_contact, :draft_case_ids)
end

def set_progress
Expand Down

0 comments on commit 8b8d8e7

Please sign in to comment.