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.1.1 app/models/renalware/pathology/observation_request.rb
renalware-core-2.1.0 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.167 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.166 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.165 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.164 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.163 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.162 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.161 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.160 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.159 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.158 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.157 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.156 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.155 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.153 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.152 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.151 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.149 app/models/renalware/pathology/observation_request.rb
renalware-core-2.0.148 app/models/renalware/pathology/observation_request.rb