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