Sha256: 4717014b18006de06aa5307ee76974852cd60e539c0f2fef77604015b74eba6c

Contents?: true

Size: 1.75 KB

Versions: 9

Compression:

Stored size: 1.75 KB

Contents

module Renalware
  module Transplants
    #  a list of everyone whose Modality is Transplant and the sort should
    #  be based on date of latest creatinine result descending (most recent
    #  at the top) - the default sort order should be same for all of the
    #  different filter groups
    class MDMPatientsQuery
      include ModalityScopes
      MODALITY_NAMES = ["Transplant"].freeze
      attr_reader :q, :relation, :named_filter

      def initialize(relation: Transplants::Patient.all, named_filter: nil, q: {})
        @q = q
        @relation = relation
        @named_filter = named_filter || :none
      end

      def call
        search.sorts = %w(given_name)
        search.result
      end

      def search
        @search ||= begin
          relation
            .includes(:current_key_observation_set)
            .extending(ModalityScopes)
            .extending(NamedFilterScopes)
            .with_current_modality_matching(MODALITY_NAMES)
            .public_send(named_filter.to_s)
            .search(q)
        end
      end

      module NamedFilterScopes
        def none
          self # NOOP
        end

        def patients_with_a_transplant_date_in_the_past_3_months
          joins(<<-SQL)
            LEFT JOIN transplant_recipient_operations
            ON patients.id = transplant_recipient_operations.patient_id
          SQL
          .where("transplant_recipient_operations.performed_on >= ?", 3.months.ago)
        end
        alias_method :recent, :patients_with_a_transplant_date_in_the_past_3_months

        def patients_on_the_worry_board
          joins("RIGHT OUTER JOIN patient_worries ON patient_worries.patient_id = patients.id")
        end
        alias_method :on_worryboard, :patients_on_the_worry_board
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.rc1 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.beta11 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.beta10 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.beta9 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.beta8 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.beta7 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.beta6 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.beta5 app/models/renalware/transplants/mdm_patients_query.rb