Sha256: be514b18aad579caddbafee9b987843ad9853eb653fb8fa772f1493610ec1a49

Contents?: true

Size: 1.41 KB

Versions: 13

Compression:

Stored size: 1.41 KB

Contents

require_dependency "renalware/letters"

module Renalware
  module Letters
    class Recipient < ApplicationRecord
      extend Enumerize

      belongs_to :letter
      belongs_to :addressee, polymorphic: true
      has_one :address, as: :addressable # for archiving purposes

      enumerize :role, in: %i(main cc)
      enumerize :person_role, in: %i(patient primary_care_physician contact)

      accepts_nested_attributes_for :address,
                                    allow_destroy: true,
                                    reject_if: :patient_or_primary_care_physician?

      validates :addressee_id, presence: { if: :contact? }

      delegate :primary_care_physician?, :patient?, :contact?, to: :person_role

      def to_s
        (address || current_address).to_s
      end

      def archive!
        build_address if address.blank?

        address.copy_from(current_address)
        address.save!
      end

      def current_address
        case
        when patient?
          letter.patient.current_address
        when primary_care_physician?
          letter.primary_care_physician.current_address
        else
          addressee.address
        end
      end

      def for_contact?(contact)
        return false unless person_role.contact?
        addressee_id == contact.id
      end

      private

      def patient_or_primary_care_physician?
        patient? || primary_care_physician?
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
renalware-core-2.0.1 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/letters/recipient.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/letters/recipient.rb