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