Class: Renalware::Renal::Registry::PreflightChecks::PatientsQuery

Inherits:
Object
  • Object
show all
Includes:
ModalityScopes
Defined in:
app/models/renalware/renal/registry/preflight_checks/patients_query.rb

Constant Summary collapse

MODALITY_NAMES =
%w(HD PD Transplant).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModalityScopes

#with_current_modality_matching, #with_current_modality_of_class

Constructor Details

#initialize(relation: nil, query_params: {}) ⇒ PatientsQuery

Returns a new instance of PatientsQuery.



14
15
16
17
18
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 14

def initialize(relation: nil, query_params: {})
  @relation ||= default_relation
  @query_params = query_params
  @query_params[:s] = "modality_descriptions_name ASC" if @query_params[:s].blank?
end

Instance Attribute Details

#query_paramsObject (readonly)

Returns the value of attribute query_params.



12
13
14
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 12

def query_params
  @query_params
end

#relationObject (readonly)

Returns the value of attribute relation.



12
13
14
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 12

def relation
  @relation
end

Class Method Details

.missing_data_for(patient) ⇒ Object

Putting this here for now so all incomplete data criteria is all in one place. Build an array of symbols for all missing data identified by the above query rubocop:disable Metrics/AbcSize



57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 57

def self.missing_data_for(patient)
  renal_profile = Renal.cast_patient(patient).profile
  missing = []
  missing << :ethnicity if patient.ethnicity_id.blank?
  missing << :prd if renal_profile&.prd_description_id.blank?
  missing << :first_seen_on if renal_profile&.first_seen_on.blank?
  if renal_profile&.document&.comorbidities&.ischaemic_heart_dis&.status.blank?
    missing << :comorbidities_at_esrf
  end
  missing
end

Instance Method Details

#callObject



26
27
28
29
30
31
32
33
34
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 26

def call
  search
    .result
    .extending(ModalityScopes)
    .with_current_modality_matching(MODALITY_NAMES)
    .merge(HD::Patient.with_profile)
    .joins("LEFT OUTER JOIN renal_profiles ON renal_profiles.patient_id = patients.id")
    .where(where_conditions)
end

#default_relationObject



20
21
22
23
24
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 20

def default_relation
  Renalware::Patient
    .preload(current_modality: [:description])
    .all
end

#searchObject



36
37
38
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 36

def search
  @search ||= relation.ransack(query_params)
end

#where_conditionsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 40

def where_conditions
  "    (patients.ethnicity_id is NULL)\n    OR\n    (renal_profiles.id IS NULL)\n    OR\n    (renal_profiles.prd_description_id IS NULL)\n    OR\n    (renal_profiles.first_seen_on IS NULL)\n    OR\n    (renal_profiles.document #>> '{comorbidities,ischaemic_heart_dis,status}' is null)\n  SQL\nend\n".squish