Sha256: b6d35479db95dcd190dda82e41515fef799154065f6ee8278dd70d05b25d19c3

Contents?: true

Size: 1.54 KB

Versions: 62

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

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

62 entries across 62 versions & 1 rubygems

Version Path
renalware-core-2.0.77 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.76 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.75 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.74 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.73 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.72 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.71 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.70 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.69 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.68 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.67 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.64 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.63 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.62 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.61 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.60 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.58 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.57 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.56 app/models/renalware/pathology/requests/patient_rule.rb
renalware-core-2.0.55 app/models/renalware/pathology/requests/patient_rule.rb