Sha256: fe0a8cd9416f26b2638650f5719e0631616dda8519157422bf308df646635971

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/accesses"

module Renalware
  module Accesses
    class Patient < ActiveType::Record[Renalware::Patient]
      has_many :profiles, dependent: :destroy
      has_many :plans, dependent: :destroy
      has_many :procedures, dependent: :destroy
      has_many :assessments, dependent: :destroy

      def current_profile
        profiles.current.first
      end

      def current_plan
        plans.current.first
      end

      scope :with_current_plan, lambda {
        joins(<<-SQL)
          left outer join access_plans on access_plans.patient_id = patients.id
            and access_plans.terminated_at is null
          left outer join access_plan_types
            on access_plans.plan_type_id = access_plan_types.id
        SQL
      }

      # Because the database allows multiple current access profiles, this scope
      # needs to choose just one, otherwise queries that merge in this scope 
      # can have duplicates, or worse, broken pagination.
      scope :with_profile, lambda {
        joins(<<-SQL)
          left outer join (
            select distinct on (patient_id) * from access_profiles 
              where
                access_profiles.terminated_on is null
                and access_profiles.started_on <= current_date
              order by patient_id, updated_at desc
          ) access_profiles on (access_profiles.patient_id = patients.id)
          left outer join access_types on access_types.id = access_profiles.type_id
        SQL
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
renalware-core-2.0.136 app/models/renalware/accesses/patient.rb
renalware-core-2.0.135 app/models/renalware/accesses/patient.rb