From 04b464064ff76e7644c34f2f6593d062ae73cd59 Mon Sep 17 00:00:00 2001 From: agravell047 Date: Wed, 24 Apr 2024 12:01:20 -0400 Subject: [PATCH] made fns generic --- .../my_health/v1/prescriptions_controller.rb | 2 +- .../helpers/my_health/prescription_helper.rb | 38 ++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/modules/my_health/app/controllers/my_health/v1/prescriptions_controller.rb b/modules/my_health/app/controllers/my_health/v1/prescriptions_controller.rb index 8c733ddf313..f95883144fd 100644 --- a/modules/my_health/app/controllers/my_health/v1/prescriptions_controller.rb +++ b/modules/my_health/app/controllers/my_health/v1/prescriptions_controller.rb @@ -16,7 +16,7 @@ def index resource = collection_resource resource = params[:filter].present? ? resource.find_by(filter_params) : resource resource.data = filter_non_va_meds(resource.data) - resource.data = sort_by(resource.data, *params[:sort]) + resource = params[:sort].is_a?(Array) ? sort_by(resource, params[:sort]) : resource.sort(params[:sort]) is_using_pagination = params[:page].present? || params[:per_page].present? resource.data = params[:include_image].present? ? fetch_and_include_images(resource.data) : resource.data resource = is_using_pagination ? resource.paginate(**pagination_params) : resource diff --git a/modules/my_health/app/helpers/my_health/prescription_helper.rb b/modules/my_health/app/helpers/my_health/prescription_helper.rb index 817f720c336..df13a22472f 100644 --- a/modules/my_health/app/helpers/my_health/prescription_helper.rb +++ b/modules/my_health/app/helpers/my_health/prescription_helper.rb @@ -18,26 +18,27 @@ def filter_non_va_meds(data) data.reject { |item| item[:prescription_source] == 'NV' && item[:disp_status] != 'Active: Non-VA' } end - def sort_by(data, field1, field2 = nil, field3 = nil) - data.sort do |a, b| - field1_a, field1_b = populate_sort_vars(field1, a, b) - field2_a, field2_b = populate_sort_vars(field2, a, b) - field3_a, field3_b = populate_sort_vars(field3, a, b) - field1_descending, field2_descending, field3_descending = get_sort_order(field1, field2, field3) - - comparison = compare_fields(field1_a, field1_b, field1_descending) - comparison = compare_fields(field2_a, field2_b, field2_descending) if comparison.zero? && field2.present? - comparison = compare_fields(field3_a, field3_b, field3_descending) if comparison.zero? && field3.present? - + def sort_by(resource, sort_params) + sort_orders = get_sort_order(sort_params) + resource.data = resource.data.sort do |a, b| + comparison = 0 + sort_params.each_with_index do |field, index| + field1_a, field1_b = populate_sort_vars(field, a, b) + comparison = compare_fields(field1_a, field1_b, sort_orders[index]) + break if !comparison.zero? || field.nil? + end comparison end + sort_params.each_with_index do |field, index| + (resource.metadata[:sort] ||= {}).merge!({ field => sort_orders[index] ? 'DESC' : 'ASC' }) if field.present? + end + resource end - def get_sort_order(field1, field2, field3) - field1_descending = field1.to_s.start_with?('-') - field2_descending = field2.to_s.start_with?('-') - field3_descending = field3.to_s.start_with?('-') - [field1_descending, field2_descending, field3_descending] + def get_sort_order(fields) + fields.map do |field| + field.to_s.start_with?('-') + end end def compare_fields(field_a, field_b, descending) @@ -68,8 +69,11 @@ def get_field_data(field, data) else data[:orderable_item] end - else + when 'disp_status' data[:disp_status] + else + key = field.sub(/^-/, '') + data[key] end end