Sha256: 527e780d12b8bdab6187cca21e21f8781682752ec50c34857b61d1246793180d

Contents?: true

Size: 1.99 KB

Versions: 8

Compression:

Stored size: 1.99 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
      include PatientPathologyScopes
      MODALITY_NAMES = ["Transplant"].freeze
      DEFAULT_SEARCH_PREDICATE = "hgb_date DESC".freeze
      attr_reader :q, :relation, :named_filter

      def initialize(relation: Transplants::Patient.all, named_filter: nil, q: nil)
        @q = q || {}
        @q[:s] = DEFAULT_SEARCH_PREDICATE if @q[:s].blank?
        @relation = relation
        @named_filter = named_filter || :none
      end

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

      def search
        @search ||= begin
          relation
            .extending(ModalityScopes)
            .extending(PatientPathologyScopes)
            .extending(NamedFilterScopes)
            .with_current_modality_matching(MODALITY_NAMES)
            .with_current_pathology
            .left_joins(:current_observation_set)
            .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

8 entries across 8 versions & 1 rubygems

Version Path
renalware-core-2.0.7 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.5 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.4 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.3 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.2 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.1 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0 app/models/renalware/transplants/mdm_patients_query.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/transplants/mdm_patients_query.rb