Class: Renalware::Renal::Registry::PreflightChecks::PatientsQuery
- Inherits:
-
Object
- Object
- Renalware::Renal::Registry::PreflightChecks::PatientsQuery
- Includes:
- ModalityScopes
- Defined in:
- app/models/renalware/renal/registry/preflight_checks/patients_query.rb
Defined Under Namespace
Modules: QueryablePatient
Constant Summary collapse
- MODALITY_NAMES =
%w(HD PD Transplant).freeze
Instance Attribute Summary collapse
-
#query_params ⇒ Object
readonly
Returns the value of attribute query_params.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Class Method Summary collapse
-
.missing_data_for(patient) ⇒ Object
Putting this here for now so all incomplete data criteria is all in one place.
Instance Method Summary collapse
- #call ⇒ Object
-
#default_relation ⇒ Object
The way I understand Ransack is that if you use a query like profile_esrf_gt (that’s the RenalProfile in this case as we are dealing with a Renal::Patient) it will do the left out join itself onto renal_profiles.
-
#initialize(relation: nil, query_params: {}) ⇒ PatientsQuery
constructor
rubocop:disable Lint/UnusedMethodArgument.
- #search ⇒ Object
- #where_conditions ⇒ Object
Methods included from ModalityScopes
#with_current_modality_matching, #with_current_modality_of_class
Constructor Details
#initialize(relation: nil, query_params: {}) ⇒ PatientsQuery
rubocop:disable Lint/UnusedMethodArgument
15 16 17 18 19 |
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 15 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_params ⇒ Object (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 |
#relation ⇒ Object (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
79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 79 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
#call ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 47 def call search .result .merge(HD::Patient.with_profile) .eager_load(:profile) .extending(ModalityScopes) .preload(current_modality: [:description]) .with_current_modality_matching(MODALITY_NAMES) .where(where_conditions) end |
#default_relation ⇒ Object
The way I understand Ransack is that if you use a query like profile_esrf_gt (that’s the RenalProfile in this case as we are dealing with a Renal::Patient) it will do the left out join itself onto renal_profiles. At one point I did a manual join onto renal_profiles here like this .joins(“LEFT OUTER JOIN renal_profiles ON renal_profiles.patient_id = patients.id”) but that only worked when there was no search predicate passed; when you pass eg profile_esrf_gt it joins onto renal_profiles again and you get a duplicate join. However using left_outer_joins here somehow prevents a duplicate join; one join onto renal_profiles is there whether there is a renal_profile_esrf_gt present or not. However it ONLY works if you use left_outer_joins BEFORE calling .ransack
-
calling it afterwards (in #call below) still results in a duplicate join IF
renal_profile_esrf_gt is present in the query.
I have to say, ransack eats up a lot of time and presents a lot of cognitive friction. Building a custom form object would have shaved an hour off the writing (mainly debugging) of this code.
Update: moved loading of profile to #call. Having it in #default_relation leads to a duplicate alias if sorting on esrf for instance. Having to use .eager_load(:profile) in #call. However this creates n+1 queries. Ransack again creating more confusion…
43 44 45 |
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 43 def default_relation Renalware::Renal::Patient.all end |
#search ⇒ Object
58 59 60 |
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 58 def search @search ||= relation.include(QueryablePatient).ransack(query_params) end |
#where_conditions ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/renalware/renal/registry/preflight_checks/patients_query.rb', line 62 def where_conditions <<-SQL.squish (patients.ethnicity_id is NULL) OR (renal_profiles.id IS NULL) OR (renal_profiles.prd_description_id IS NULL) OR (renal_profiles.first_seen_on IS NULL) OR (renal_profiles.document #>> '{comorbidities,ischaemic_heart_dis,status}' is null) SQL end |