Sha256: 9c0238fa7c7db9e26ba928baf6c8716fd8cbd2fc6afe8c286d0679b0948df87b

Contents?: true

Size: 1.92 KB

Versions: 72

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/transplants"
require "document/base"

module Renalware
  module Transplants
    class Registration < ApplicationRecord
      include Document::Base
      include PatientScope

      belongs_to :patient, touch: true
      has_many :statuses,
               class_name: "RegistrationStatus",
               foreign_key: "registration_id"
      has_one :current_status,
              -> { where(terminated_on: nil).order([:started_on, :created_at]) },
              class_name: "RegistrationStatus",
              foreign_key: "registration_id"

      has_paper_trail class_name: "Renalware::Transplants::Version",
                      on: [:create, :update, :destroy]
      has_document class_name: "Renalware::Transplants::RegistrationDocument"

      accepts_nested_attributes_for :statuses

      def add_status!(params)
        Registration.transaction do
          statuses.create(params).tap do |status|
            recompute_termination_dates! if status.valid?
          end
        end
      end

      def update_status!(status, params)
        Registration.transaction do
          if status.update(params)
            recompute_termination_dates!
          end
          status
        end
      end

      def delete_status!(status)
        Registration.transaction do
          status.destroy
          recompute_termination_dates!
        end
      end

      private

      # Set the most recent status terminated_on to nil (so it is active)
      # and then walk back across all other statuses and set the terminated_on
      # to be started_on of the next status. Allow for > 1 status created on the same day.
      def recompute_termination_dates!
        previous_started_on = nil
        statuses.reversed.each do |status|
          status.update_column(:terminated_on, previous_started_on)
          previous_started_on = status.started_on
        end
      end
    end
  end
end

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
renalware-core-2.0.148 app/models/renalware/transplants/registration.rb
renalware-core-2.0.147 app/models/renalware/transplants/registration.rb
renalware-core-2.0.146 app/models/renalware/transplants/registration.rb
renalware-core-2.0.145 app/models/renalware/transplants/registration.rb
renalware-core-2.0.144 app/models/renalware/transplants/registration.rb
renalware-core-2.0.143 app/models/renalware/transplants/registration.rb
renalware-core-2.0.142 app/models/renalware/transplants/registration.rb
renalware-core-2.0.141 app/models/renalware/transplants/registration.rb
renalware-core-2.0.140 app/models/renalware/transplants/registration.rb
renalware-core-2.0.139 app/models/renalware/transplants/registration.rb
renalware-core-2.0.138 app/models/renalware/transplants/registration.rb
renalware-core-2.0.137 app/models/renalware/transplants/registration.rb
renalware-core-2.0.136 app/models/renalware/transplants/registration.rb
renalware-core-2.0.135 app/models/renalware/transplants/registration.rb
renalware-core-2.0.134 app/models/renalware/transplants/registration.rb
renalware-core-2.0.133 app/models/renalware/transplants/registration.rb
renalware-core-2.0.132 app/models/renalware/transplants/registration.rb
renalware-core-2.0.131 app/models/renalware/transplants/registration.rb
renalware-core-2.0.130 app/models/renalware/transplants/registration.rb
renalware-core-2.0.129 app/models/renalware/transplants/registration.rb