Sha256: 71df450b0dd5d1d7dc4f7063384c2e796da05878edc17d466d6adf2c1a60e287

Contents?: true

Size: 1.75 KB

Versions: 21

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/admissions"

module Renalware
  module Admissions
    class ConsultQuery
      attr_reader :query

      def initialize(query = nil)
        @query = query || {}
        @query[:ended_on_null] ||= true
        @query[:s] ||= "hospital_ward_name"
      end

      def call
        search.result
      end

      # Note we *MUST* join onto patients for PatientsRansackHelper.identity_match to work.
      # It might be better to refactor PatientsRansackHelper so we can include where required
      # eg below using .extending(PatientsRansackHelper) rather than relying on it being in
      # included in the model file.
      # note that adding .includes(:created_by) here creates an ambigous column
      # 'family_name' error
      # rubocop:disable Metrics/MethodLength
      def search
        @search ||= begin
          Consult
            .extend(RansackScopes)
            .joins(:patient)
            .eager_load(patient: [current_modality: :description])
            .includes(
              :consult_site,
              :created_by,
              patient: { current_modality: :description },
              hospital_ward: :hospital_unit
            )
            .order(created_at: :desc)
            .ransack(query)
        end
      end
      # rubocop:enable Metrics/MethodLength

      module RansackScopes
        def self.extended(base)
          # Using a custom ransacker here in order to sort by modality description name
          # because using a predicate like  :patient_current_modality_description_name
          # results in an INNER JOIN onto modalities.
          base.ransacker :modality_desc do
            Arel.sql("modality_descriptions.name")
          end
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
renalware-core-2.0.131 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.130 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.129 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.128 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.127 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.126 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.125 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.124 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.123 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.121 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.120 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.119 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.118 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.117 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.116 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.115 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.113 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.112 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.111 app/models/renalware/admissions/consult_query.rb
renalware-core-2.0.110 app/models/renalware/admissions/consult_query.rb