Sha256: fc9fe11f7192b36e2c09bd0a8a33d768f9c8f8398b8f278fe42aadc97af8b4ce

Contents?: true

Size: 1.59 KB

Versions: 28

Compression:

Stored size: 1.59 KB

Contents

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

28 entries across 28 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/transplants/registration.rb
renalware-core-2.0.7 app/models/renalware/transplants/registration.rb
renalware-core-2.0.5 app/models/renalware/transplants/registration.rb
renalware-core-2.0.4 app/models/renalware/transplants/registration.rb
renalware-core-2.0.3 app/models/renalware/transplants/registration.rb
renalware-core-2.0.2 app/models/renalware/transplants/registration.rb
renalware-core-2.0.1 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/transplants/registration.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/transplants/registration.rb