Sha256: 59bb403e6d046100247aa2a8cec88203d528d5f4824146b8b5eb256befff3e2f

Contents?: true

Size: 1.51 KB

Versions: 28

Compression:

Stored size: 1.51 KB

Contents

require_dependency "renalware/pathology"

module Renalware
  module Pathology
    module Requests
      class PatientRule < ApplicationRecord
        include FrequencyMethods

        self.table_name = "pathology_requests_patient_rules"

        belongs_to :patient, class_name: "::Renalware::Pathology::Patient"
        belongs_to :lab, class_name: "::Renalware::Pathology::Lab"
        has_and_belongs_to_many :requests,
          class_name: "::Renalware::Pathology::Requests::Request"

        validates :lab, presence: true
        validates :test_description, presence: true
        validates :patient_id, presence: true

        OBSERVATION_REQUIRED = true
        OBSERVATION_NOT_REQUIRED = false

        def required?(date)
          return OBSERVATION_NOT_REQUIRED unless today_within_range?(date)
          return OBSERVATION_REQUIRED if last_observed_at.nil?

          days_ago_observed = date - last_observed_at.to_date

          frequency.observation_required?(days_ago_observed)
        end

        def last_observed_at
          last_request = patient.last_request_for_patient_rule(self)
          return if last_request.nil?

          last_request.created_at
        end

        private

        def today_within_range?(today)
          return OBSERVATION_REQUIRED unless start_date.present? && end_date.present?

          if today.between?(start_date.to_date, end_date.to_date)
            OBSERVATION_REQUIRED
          else
            OBSERVATION_NOT_REQUIRED
          end
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.7 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.5 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.4 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.3 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.2 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.1 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/pathology/requests/patient_rule.rb