Skip to content

Commit

Permalink
rewrite to use AR (was SQL), some logic moved to the Model
Browse files Browse the repository at this point in the history
  • Loading branch information
VoldemLive committed Jan 8, 2025
1 parent 01833c3 commit 38d5494
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
32 changes: 3 additions & 29 deletions app/controllers/lab_tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ class LabTestsController < ApplicationController
def index
authorize LabTest

# Get unique dates first, using the latest record for each date
@recordables = policy_scope(LabTest)
.select(<<~SQL.squish)
DISTINCT ON (DATE(lab_tests.created_at))#{' '}
DATE(lab_tests.created_at) as created_at,#{' '}
lab_tests.recordable_id,#{' '}
lab_tests.created_at as full_created_at
SQL
.where(user_id: @chosen_user_id)
.where(date_range_condition)
.order('DATE(lab_tests.created_at) ASC, lab_tests.created_at DESC, lab_tests.recordable_id DESC')
.in_date_range(params[:start_date], params[:end_date])
.latest_records
.ordered_by_date

# Keep existing biomarkers query
@biomarkers = policy_scope(Biomarker)
.includes(:reference_ranges, :lab_tests)
.where(lab_tests: { user_id: @chosen_user_id })
Expand Down Expand Up @@ -147,24 +140,5 @@ def lab_test_params
def filter_params
params.permit(:user_id)
end

def date_range_condition
return nil unless params[:start_date].present? || params[:end_date].present?

values = {}
conditions = []

add_date_condition(conditions, values, :start_date, '>=')
add_date_condition(conditions, values, :end_date, '<=')

[conditions.join(' AND '), values]
end

def add_date_condition(conditions, values, date_param, operator)
return if params[date_param].blank?

conditions << "DATE(lab_tests.created_at) #{operator} :#{date_param}"
values[date_param] = Date.parse(params[date_param])
end
end
# rubocop:enable Metrics/ClassLength
29 changes: 29 additions & 0 deletions app/models/lab_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ class LabTest < ApplicationRecord
message: :must_be_nonnegative_and_numeric
}

scope :in_date_range, lambda { |start_date, end_date|
scope = all
scope = scope.where(created_at: Date.parse(start_date).beginning_of_day..) if start_date.present?
scope = scope.where(created_at: ..Date.parse(end_date).end_of_day) if end_date.present?
scope
}

scope :by_date, lambda { |date|
where(created_at: date.all_day)
}

# Show all records, ordered by newest first
scope :latest_records, lambda {
order(created_at: :asc)
}

# Changed to descending order to match the requirement
scope :ordered_by_date, lambda {
order(created_at: :asc, id: :asc)
}

# here code with group by tests by day (...DISTINCT ON)
# scope :latest_records, lambda {
# records = all.group_by { |record| record.created_at.to_date }
# .transform_values { |group| group.max_by(&:created_at) }
# .values
# where(id: records)
# }

class Status
NORMAL = :normal
HIGH = :high
Expand Down

0 comments on commit 38d5494

Please sign in to comment.