Sha256: 59a9133867d341342a933ba6713274284d99ea00cbd0d3acc2109b95080b181c

Contents?: true

Size: 1.54 KB

Versions: 15

Compression:

Stored size: 1.54 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.squish)
          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.squish)
          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

15 entries across 15 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/accesses/patient.rb
renalware-core-2.1.0 app/models/renalware/accesses/patient.rb
renalware-core-2.0.167 app/models/renalware/accesses/patient.rb
renalware-core-2.0.166 app/models/renalware/accesses/patient.rb
renalware-core-2.0.165 app/models/renalware/accesses/patient.rb
renalware-core-2.0.164 app/models/renalware/accesses/patient.rb
renalware-core-2.0.163 app/models/renalware/accesses/patient.rb
renalware-core-2.0.162 app/models/renalware/accesses/patient.rb
renalware-core-2.0.161 app/models/renalware/accesses/patient.rb
renalware-core-2.0.160 app/models/renalware/accesses/patient.rb
renalware-core-2.0.159 app/models/renalware/accesses/patient.rb
renalware-core-2.0.158 app/models/renalware/accesses/patient.rb
renalware-core-2.0.157 app/models/renalware/accesses/patient.rb
renalware-core-2.0.156 app/models/renalware/accesses/patient.rb
renalware-core-2.0.155 app/models/renalware/accesses/patient.rb