Sha256: 7ea2032c1130ac1132fa10428e18454730400724ab23f2be0a65c9231c15e4a2

Contents?: true

Size: 1.68 KB

Versions: 23

Compression:

Stored size: 1.68 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

      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

23 entries across 23 versions & 1 rubygems

Version Path
renalware-core-2.0.73 app/models/renalware/transplants/registration.rb
renalware-core-2.0.72 app/models/renalware/transplants/registration.rb
renalware-core-2.0.71 app/models/renalware/transplants/registration.rb
renalware-core-2.0.70 app/models/renalware/transplants/registration.rb
renalware-core-2.0.69 app/models/renalware/transplants/registration.rb
renalware-core-2.0.68 app/models/renalware/transplants/registration.rb
renalware-core-2.0.67 app/models/renalware/transplants/registration.rb
renalware-core-2.0.64 app/models/renalware/transplants/registration.rb
renalware-core-2.0.63 app/models/renalware/transplants/registration.rb
renalware-core-2.0.62 app/models/renalware/transplants/registration.rb
renalware-core-2.0.61 app/models/renalware/transplants/registration.rb
renalware-core-2.0.60 app/models/renalware/transplants/registration.rb
renalware-core-2.0.58 app/models/renalware/transplants/registration.rb
renalware-core-2.0.57 app/models/renalware/transplants/registration.rb
renalware-core-2.0.56 app/models/renalware/transplants/registration.rb
renalware-core-2.0.55 app/models/renalware/transplants/registration.rb
renalware-core-2.0.54 app/models/renalware/transplants/registration.rb
renalware-core-2.0.53 app/models/renalware/transplants/registration.rb
renalware-core-2.0.52 app/models/renalware/transplants/registration.rb
renalware-core-2.0.51 app/models/renalware/transplants/registration.rb