Sha256: 81f53afaf62e9f484d64e3f31df73d3b16cc87ac744a1706cfb2aa74cdcce750

Contents?: true

Size: 1.71 KB

Versions: 105

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/modalities"

module Renalware
  module Modalities
    class Modality < ApplicationRecord
      include Accountable
      include PatientScope

      belongs_to :description, class_name: "Description"
      belongs_to :patient, touch: true
      belongs_to :reason, class_name: "Reason"

      scope :ordered, -> { order(ended_on: :desc, updated_at: :desc) }
      scope :started_on_reversed, -> { order(started_on: :desc, updated_at: :desc) }
      scope :last_started_on, -> { started_on_reversed.where(ended_on: nil) }

      validates :patient, presence: true
      validates :started_on, presence: true
      validates :description, presence: true
      validates :started_on, timeliness: { type: :date }
      validate :validate_modality_starts_later_than_previous, on: :create, if: :patient

      def terminate_by(user, on:)
        self.ended_on = on
        self.state = "terminated"
        save_by!(user)
      end

      def to_s
        description.name
      end

      def terminated?
        state == "terminated"
      end

      private

      def validate_modality_starts_later_than_previous
        unless started_later_than_previous?
          errors.add(:started_on, "can't be before previous modality's start date.")
        end
      end

      def started_later_than_previous?
        if (previous_modality = patient.modalities.last_started_on.first)
          started_on.present? && started_on >= previous_modality.started_on
        else
          true
        end
      end

      def terminate!(successor)
        self.ended_on = successor.started_on
        self.state = "terminated"
        self.by = successor.by
        save!
      end
    end
  end
end

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
renalware-core-2.0.123 app/models/renalware/modalities/modality.rb
renalware-core-2.0.121 app/models/renalware/modalities/modality.rb
renalware-core-2.0.120 app/models/renalware/modalities/modality.rb
renalware-core-2.0.119 app/models/renalware/modalities/modality.rb
renalware-core-2.0.118 app/models/renalware/modalities/modality.rb
renalware-core-2.0.117 app/models/renalware/modalities/modality.rb
renalware-core-2.0.116 app/models/renalware/modalities/modality.rb
renalware-core-2.0.115 app/models/renalware/modalities/modality.rb
renalware-core-2.0.113 app/models/renalware/modalities/modality.rb
renalware-core-2.0.112 app/models/renalware/modalities/modality.rb
renalware-core-2.0.111 app/models/renalware/modalities/modality.rb
renalware-core-2.0.110 app/models/renalware/modalities/modality.rb
renalware-core-2.0.109 app/models/renalware/modalities/modality.rb
renalware-core-2.0.108 app/models/renalware/modalities/modality.rb
renalware-core-2.0.106 app/models/renalware/modalities/modality.rb
renalware-core-2.0.105 app/models/renalware/modalities/modality.rb
renalware-core-2.0.104 app/models/renalware/modalities/modality.rb
renalware-core-2.0.103 app/models/renalware/modalities/modality.rb
renalware-core-2.0.102 app/models/renalware/modalities/modality.rb
renalware-core-2.0.101 app/models/renalware/modalities/modality.rb