Sha256: 1316c4010316649da5485def45a8f136b2fb948de51222bd23d7ce0d70592af2

Contents?: true

Size: 1.87 KB

Versions: 15

Compression:

Stored size: 1.87 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"
      has_one :current_status,
              -> { where(terminated_on: nil).order([:started_on, :created_at]) },
              class_name: "RegistrationStatus",
              foreign_key: "registration_id"

      has_paper_trail(
        versions: { 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

15 entries across 15 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/transplants/registration.rb
renalware-core-2.1.0 app/models/renalware/transplants/registration.rb
renalware-core-2.0.167 app/models/renalware/transplants/registration.rb
renalware-core-2.0.166 app/models/renalware/transplants/registration.rb
renalware-core-2.0.165 app/models/renalware/transplants/registration.rb
renalware-core-2.0.164 app/models/renalware/transplants/registration.rb
renalware-core-2.0.163 app/models/renalware/transplants/registration.rb
renalware-core-2.0.162 app/models/renalware/transplants/registration.rb
renalware-core-2.0.161 app/models/renalware/transplants/registration.rb
renalware-core-2.0.160 app/models/renalware/transplants/registration.rb
renalware-core-2.0.159 app/models/renalware/transplants/registration.rb
renalware-core-2.0.158 app/models/renalware/transplants/registration.rb
renalware-core-2.0.157 app/models/renalware/transplants/registration.rb
renalware-core-2.0.156 app/models/renalware/transplants/registration.rb
renalware-core-2.0.155 app/models/renalware/transplants/registration.rb