Sha256: 00e28f4e6c05ca42b95a41230ad6f18b493a74646502fc45573ed4e2f2653bb8

Contents?: true

Size: 2 KB

Versions: 11

Compression:

Stored size: 2 KB

Contents

require_dependency "renalware/transplants"
require "document/base"

module Renalware
  module Transplants
    class Donation < ApplicationRecord
      include PatientScope
      extend Enumerize

      belongs_to :patient, touch: true
      belongs_to :recipient, class_name: "Patient", foreign_key: "recipient_id"

      has_paper_trail class_name: "Renalware::Transplants::Version"

      scope :ordered, -> { order(created_at: :asc) }
      scope :reversed, -> { order(created_at: :desc) }
      scope :for_recipient, ->(recipient) { where(recipient_id: recipient.id) }

      enumerize :state, in: %i(volunteered seen_in_clinic investigating handed_over unsuitable)
      enumerize :relationship_with_recipient, in: %i(
        son_or_daughter mother_or_father sibling_2_shared
        sibling_1_shared sibling_0_shared sibling
        monozygotic_twin dizygotic_twin other_living_related
        living_non_related_spouse living_non_related_partner
        pooled_paired altruistic_non_directed other_living_non_related
      )
      enumerize :blood_group_compatibility, in: %i(yes no)
      enumerize :paired_pooled_donation, in: %i(
        n_a information_given consented to_enroll_in_next_matching_un
      )

      validates :state, presence: true
      validates :relationship_with_recipient, presence: true
      validates :relationship_with_recipient_other,
                presence: true,
                if: ->(o) { o.relationship_with_recipient.try(:other_living_non_related?) }
      validates :volunteered_on, timeliness: { type: :date, allow_blank: true }
      validates :first_seen_on, timeliness: { type: :date, allow_blank: true }
      validates :workup_completed_on, timeliness: { type: :date, allow_blank: true }
      validates :donated_on, timeliness: { type: :date, allow_blank: true }
      validate :validate_recipient

      private

      def validate_recipient
        if recipient_id.present? && (recipient_id == patient_id)
          errors.add(:recipient_id, :invalid)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.rc11 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/transplants/donation.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/transplants/donation.rb