Sha256: d4068e66f21df0640bb2a0b4ac36d4e7bc1ecf9dc4a73ee14ffb8f592a246a68

Contents?: true

Size: 1.61 KB

Versions: 89

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/pathology"

module Renalware
  module Pathology
    class ObservationRequest < ApplicationRecord
      has_many :observations,
               foreign_key: :request_id,
               inverse_of: :request,
               dependent: :destroy
      belongs_to :description, class_name: "RequestDescription", inverse_of: :requests
      belongs_to :patient, class_name: "Patient", touch: true

      accepts_nested_attributes_for :observations

      validates :patient, presence: true
      validates :requestor_name, presence: true
      validates :requested_at, presence: true

      scope :ordered, -> { order(requested_at: :desc) }

      def requested_on
        requested_at.to_date
      end

      # Exlucludes duplicate rows; there may be an original OBR and one or more updates (with more
      # complete results) and we only want the last received one got any requestor_order_number
      # (actually for any requestor_order_number + requested_at + description_id) combination
      # because requestor_order_number is sometimes blank
      # rubocop:disable Metrics/MethodLength
      def self.distinct_for_patient_id(patient_id)
        select(
          Arel.sql(
            "DISTINCT ON (patient_id, requestor_order_number, requested_at, description_id) id"
          )
        )
        .where(patient_id: patient_id)
        .order(
          patient_id: :asc,
          requestor_order_number: :asc,
          requested_at: :asc,
          description_id: :asc,
          created_at: :desc
        )
      end
      # rubocop:enable Metrics/MethodLength
    end
  end
end

Version data entries

89 entries across 89 versions & 1 rubygems

Version Path
renalware-core-2.0.84 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.83 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.82 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.81 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.80 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.79 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.78 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.77 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.76 app/models/renalware/pathology/observation_request.rb