Sha256: 5727ec7dfec4df32d1b7c39875232c7686789c7b7332776b7e300a62f1ba759d

Contents?: true

Size: 1.99 KB

Versions: 5

Compression:

Stored size: 1.99 KB

Contents

require_dependency "renalware/hd"
require "duration_calculator"

module Renalware
  module HD
    class Session < ApplicationRecord
      include PatientScope
      include Accountable
      include ExplicitStateModel

      validates_presence_of :type # Prevent instances of this of this base from being saved

      has_states :open, :closed, :dna

      belongs_to :patient, touch: true
      belongs_to :profile
      belongs_to :dry_weight, class_name: "Renalware::Clinical::DryWeight"
      belongs_to :modality_description, class_name: "Modalities::Description"
      belongs_to :hospital_unit, class_name: "Hospitals::Unit"
      belongs_to :signed_on_by, class_name: "User", foreign_key: "signed_on_by_id"
      belongs_to :signed_off_by, class_name: "User", foreign_key: "signed_off_by_id"
      has_many :prescription_administrations,
               class_name: "PrescriptionAdministration",
               foreign_key: "hd_session_id",
               dependent: :destroy
      accepts_nested_attributes_for :prescription_administrations

      has_paper_trail class_name: "Renalware::HD::Version"

      before_create :assign_modality
      before_save :compute_duration

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

      validates :patient, presence: true
      validates :hospital_unit, presence: true
      validates :signed_on_by, presence: true
      validates :performed_on, presence: true
      validates :performed_on, timeliness: { type: :date }
      validates :start_time, timeliness: { type: :time, allow_blank: true }
      validates :end_time, timeliness: { type: :time, allow_blank: true, after: :start_time }

      delegate :hospital_centre, to: :hospital_unit, allow_nil: true

      private

      def assign_modality
        self.modality_description = patient.modality_description
      end

      def compute_duration
        return unless start_time_changed? || end_time_changed?

        self.duration = DurationCalculator.in_minutes(start_time, end_time)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.beta8 app/models/renalware/hd/session.rb
renalware-core-2.0.0.pre.beta7 app/models/renalware/hd/session.rb
renalware-core-2.0.0.pre.beta6 app/models/renalware/hd/session.rb
renalware-core-2.0.0.pre.beta5 app/models/renalware/hd/session.rb
renalware-core-2.0.0.pre.beta4 app/models/renalware/hd/session.rb