Class: Renalware::Admissions::AdmissionSearchForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Virtus::Model
Defined in:
app/models/renalware/admissions/admission_search_form.rb

Overview

Rather than using a Ransack Search object behind the form, as we do in many other places here we use a custom form object. This is mainly because we have a drop down of (virtual) statues, where each maps to a scope on Admission, where we want the selected ‘status’ to result in its associated scope being called. Ransack doesn’t handle this, not surprisingly. So we have this thin form object around Ransack and do some pre-processing on the Status attribute (see #status_scope).

Instance Method Summary collapse

Instance Method Details

#status_dropdown_optionsObject



34
35
36
37
38
39
# File 'app/models/renalware/admissions/admission_search_form.rb', line 34

def status_dropdown_options
  [
    ["Currently admitted", :currently_admitted],
    ["Discharged but missing summary", :discharged_but_missing_a_summary]
  ]
end

#status_scopeObject

If a status was selected in the drop down, map this to a ransack way of saying “call this scope on Admission”, which is to pass the following (note the true which indicates the scope should be used):

{ name_of_scope: true }


47
48
49
50
51
# File 'app/models/renalware/admissions/admission_search_form.rb', line 47

def status_scope
  return {} if status.blank?

  { status.to_sym => true }
end

#submitObject

Pass our simple form attributes to Ransack - we lean on Ransack to do querying so as to avoid unnecessary code here, though the magic obfuscates things a little…



22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/renalware/admissions/admission_search_form.rb', line 22

def submit
  @search ||= begin
    options = {
      hospital_ward_hospital_unit_id_eq: hospital_unit_id,
      hospital_ward_id_eq: hospital_ward_id,
      identity_match: term
    }.merge!(status_scope)

    AdmissionQuery.call(options)
  end
end