Skip to content

Commit

Permalink
made fns generic
Browse files Browse the repository at this point in the history
  • Loading branch information
agravell047 committed Apr 24, 2024
1 parent 702c414 commit 04b4640
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 21 additions & 17 deletions modules/my_health/app/helpers/my_health/prescription_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 04b4640

Please sign in to comment.