Module: Renalware::PatientPathologyScopes

Included in:
HD::MDMPatientsQuery, Renalware::PD::MDMPatientsQuery, Renalware::Patients::MDMPatientsQuery, Transplants::MDMPatientsQuery
Defined in:
app/models/concerns/renalware/patient_pathology_scopes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Define some ransackers to make it easier to sort the table (using sort_link) on pathology dates and values. Note it wasn’t possible just to use say

sort_link(.., “pathology_current_key_observation_sets.cre_observed_at”, ..)

or

sort_link(.., :pathology_current_key_observation_set_cre_observed_at“, ..)

as ransack wasn’t happy and discarded the sort predicate. So here we mix in useful ransackers as short cut to use in sort_link

Example usage:

= sort_link(<url>, :cre_date, "CRE date")


26
27
28
29
30
31
32
33
34
35
# File 'app/models/concerns/renalware/patient_pathology_scopes.rb', line 26

def self.extended(base)
  %i(hgb ure cre urr phos pth).each do |code|
    base.ransacker(code) { pathology_result_sort_predicate(code) }
    base.ransacker(:"#{code}_date") { pathology_date_sort_predicate(code) }
  end

  %i(egfr).each do |code|
    base.ransacker(code) { pathology_result_sort_predicate(code) }
  end
end

.pathology_date_sort_predicate(column) ⇒ Object



52
53
54
55
56
57
# File 'app/models/concerns/renalware/patient_pathology_scopes.rb', line 52

def self.pathology_date_sort_predicate(column)
  sanitized_column = Arel.sql(sanitize(column.to_s.upcase))
  Arel.sql(
    "cast(values -> #{sanitized_column} ->> 'observed_at' as date)"
  )
end

.pathology_result_sort_predicate(column) ⇒ Object

Note that because #result could have text like “Test Cancelled” in it, we need to handle that when sorting, and we do that by calling a custom sql function which returns 0 if casting to a float fails. Null results to we map to a 0. This way results appear in this order:

  1. Valid floats e.g. 10.4

  2. Results with a message like Test Cancelled (mapped to 0) - more significant than a null

so should rise above them when sorted
  1. Nulls (mapped to -1)



45
46
47
48
49
50
# File 'app/models/concerns/renalware/patient_pathology_scopes.rb', line 45

def self.pathology_result_sort_predicate(column)
  sanitized_column = Arel.sql(sanitize(column.to_s.upcase))
  Arel.sql(
    "coalesce(convert_to_float(values -> #{sanitized_column} ->> 'result'), -1)"
  )
end

.sanitize(sql) ⇒ Object



59
60
61
# File 'app/models/concerns/renalware/patient_pathology_scopes.rb', line 59

def self.sanitize(sql)
  ActiveRecord::Base.connection.quote(sql)
end

Instance Method Details

#with_current_pathologyObject



11
12
13
# File 'app/models/concerns/renalware/patient_pathology_scopes.rb', line 11

def with_current_pathology
  includes(:current_observation_set)
end