Sha256: 11bf7ba3dea0b9ae09115d8ead43e7cb7fcd802fe1906f273d40e68f4d620092

Contents?: true

Size: 1.86 KB

Versions: 35

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Renalware
  module Admissions
    # 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).
    class AdmissionSearchForm
      include ActiveModel::Model
      include Virtus::Model

      attribute :hospital_unit_id, Integer
      attribute :hospital_ward_id, Integer
      attribute :status, String
      attribute :term, String

      # 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...
      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

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

      # 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 }
      #
      def status_scope
        return {} if status.blank?
        { status.to_sym => true }
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
renalware-core-2.0.25 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.24 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.23 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.22 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.21 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.20 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.18 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.17 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.16 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.15 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.14 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.13 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.12 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.11 app/models/renalware/admissions/admission_search_form.rb
renalware-core-2.0.9 app/models/renalware/admissions/admission_search_form.rb