Skip to content

Commit

Permalink
dm-4157 refactor #origin_display_name (#720)
Browse files Browse the repository at this point in the history
* refactor #origin_display_name

breaks into helper methods

implements conditional preloading for `PracticeOriginFacilities` query

* fix conditional in `origin_display_name`
  • Loading branch information
PhilipDeFraties authored Oct 18, 2023
1 parent 0b53200 commit 7080da9
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,9 @@ def overview_statement(statement, start_char=0, num_chars=360)
def origin_display_name(practice)
if practice.initiating_facility_type?
if practice.facility? && practice.practice_origin_facilities.present?
fac_type = Practice.initiating_facility_types[practice.initiating_facility_type]
locs = practice.practice_origin_facilities.includes([:va_facility]).where(facility_type: fac_type)
facility_names = String.new
locs.each_with_index do |loc, index|
has_va_facility = loc.va_facility.present?
official_station_name = has_va_facility ? loc.va_facility.official_station_name : loc.clinical_resource_hub.official_station_name
common_name = loc.va_facility.common_name if has_va_facility

facility_names += "#{has_va_facility ? facility_name_with_common_name(official_station_name, common_name) : official_station_name}#{', ' if locs.size != index + 1 && locs.size > 1}"
end
facility_names
generate_facility_names(practice)
elsif practice.initiating_facility?
# TODO: Modify once visn, dept, other is moved from Practice to a separate table
case practice.initiating_facility_type
when 'visn'
visn = Visn.get_by_initiating_facility(practice.initiating_facility.to_i)
return "VISN-#{visn.number.to_s}"
when 'department'
dept_id = practice.initiating_department_office_id
office = origin_data_json['departments'][dept_id - 1]['offices'].find { |o| o['id'] == practice.initiating_facility.to_i }
office['name']
when 'other'
practice.initiating_facility
else
''
end
handle_initiating_facility(practice)
else
''
end
Expand All @@ -84,6 +61,41 @@ def origin_display_name(practice)
end
end

def generate_facility_names(practice)
fac_type = Practice.initiating_facility_types[practice.initiating_facility_type]
locs = practice.practice_origin_facilities.includes(determine_associations(practice)).where(facility_type: fac_type)
locs.map { |loc| construct_facility_name(loc) }.join(', ')
end

def determine_associations(practice)
associations = [:va_facility]
if practice.practice_origin_facilities.where(va_facility: nil).exists?
associations << :clinical_resource_hub
end
associations
end

def construct_facility_name(loc)
has_va_facility = loc.va_facility.present?
official_station_name = has_va_facility ? loc.va_facility.official_station_name : loc.clinical_resource_hub.official_station_name
common_name = loc.va_facility.common_name if has_va_facility
has_va_facility ? facility_name_with_common_name(official_station_name, common_name) : official_station_name
end

def handle_initiating_facility(practice)
case practice.initiating_facility_type
when 'visn'
visn = Visn.get_by_initiating_facility(practice.initiating_facility.to_i)
"VISN-#{visn.number.to_s}"
when 'department'
dept_id = practice.initiating_department_office_id
office = origin_data_json['departments'][dept_id - 1]['offices'].find { |o| o['id'] == practice.initiating_facility.to_i }
office['name']
when 'other'
practice.initiating_facility
end
end

def origin_display(practice)
display_name = origin_display_name(practice)
if display_name.present?
Expand Down

0 comments on commit 7080da9

Please sign in to comment.