Sha256: 138a451d58c87070f45486642bfc8b1ccb3af9461a0435947e62ba7c70fb1dcc

Contents?: true

Size: 1.87 KB

Versions: 35

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/pathology"

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

        has_many :rules, as: :rule_set, class_name: "GlobalRule"

        belongs_to :request_description
        belongs_to :clinic, class_name: "Clinics::Clinic"
        validates :request_description, presence: true
        validates :clinic, presence: true
        validate :constrain_request_description

        scope :for_clinic, ->(clinic) { where(clinic: clinic) }
        scope :ordered, lambda {
          includes(request_description: :lab)
            .order("pathology_labs.name ASC, pathology_request_descriptions.code ASC")
        }

        def observation_required_for_patient?(patient, date)
          PatientRuleSetDecision.new(patient, self, date).call
        end

        def to_s
          if rules.length >= 1
            rules_str = rules.map(&:to_s).join(" and ")
            "if #{rules_str} then #{frequency}"
          else
            frequency.to_s
          end
        end

        private

        def constrain_request_description
          return if request_description.blank?

          validate_presence_of_required_observation_description_for_request_description
          validate_presence_of_bottle_type_for_request_description
        end

        def validate_presence_of_required_observation_description_for_request_description
          return if request_description.required_observation_description.present?
          errors.add(:request_description, "required observation description can't be blank")
        end

        def validate_presence_of_bottle_type_for_request_description
          return if request_description.bottle_type.present?
          errors.add(:request_description, "bottle type can't be blank")
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
renalware-core-2.0.46 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.45 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.44 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.43 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.42 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.41 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.40 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.39 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.38 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.37 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.36 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.35 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.34 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.33 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.32 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.31 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.30 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.28 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.27 app/models/renalware/pathology/requests/global_rule_set.rb
renalware-core-2.0.26 app/models/renalware/pathology/requests/global_rule_set.rb