Sha256: 0a150824a87ae3c30b995a5a8d382c314aad99b65e5b8c093c66d583cbe9f764

Contents?: true

Size: 1.62 KB

Versions: 35

Compression:

Stored size: 1.62 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"
      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

      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

35 entries across 35 versions & 1 rubygems

Version Path
renalware-core-2.0.46 app/models/renalware/transplants/registration.rb
renalware-core-2.0.45 app/models/renalware/transplants/registration.rb
renalware-core-2.0.44 app/models/renalware/transplants/registration.rb
renalware-core-2.0.43 app/models/renalware/transplants/registration.rb
renalware-core-2.0.42 app/models/renalware/transplants/registration.rb
renalware-core-2.0.41 app/models/renalware/transplants/registration.rb
renalware-core-2.0.40 app/models/renalware/transplants/registration.rb
renalware-core-2.0.39 app/models/renalware/transplants/registration.rb
renalware-core-2.0.38 app/models/renalware/transplants/registration.rb
renalware-core-2.0.37 app/models/renalware/transplants/registration.rb
renalware-core-2.0.36 app/models/renalware/transplants/registration.rb
renalware-core-2.0.35 app/models/renalware/transplants/registration.rb
renalware-core-2.0.34 app/models/renalware/transplants/registration.rb
renalware-core-2.0.33 app/models/renalware/transplants/registration.rb
renalware-core-2.0.32 app/models/renalware/transplants/registration.rb
renalware-core-2.0.31 app/models/renalware/transplants/registration.rb
renalware-core-2.0.30 app/models/renalware/transplants/registration.rb
renalware-core-2.0.28 app/models/renalware/transplants/registration.rb
renalware-core-2.0.27 app/models/renalware/transplants/registration.rb
renalware-core-2.0.26 app/models/renalware/transplants/registration.rb