Class: Renalware::Letters::Recipient

Inherits:
ApplicationRecord show all
Extended by:
Enumerize
Defined in:
app/models/renalware/letters/recipient.rb

Instance Method Summary collapse

Instance Method Details

#archive!Object

Archiving a letter means taking the current address for the recipient (they could be a practice, patient, gp etc) and copying their address into a new Address row so it becomes immutably recorded as a definitive statement of where the letter was sent. Without this, if for example the patient’s address subsequently changes, we would only be able to resolve the new address for the patient, not the address used at the time the letter was sent. We could also look at the archived letter content however to see what was physically on the letter, but we can’t do that in SQL.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/renalware/letters/recipient.rb', line 46

def archive!
  build_address if address.blank?

  address.copy_from(current_address)

  # Its possible a migrated address might not have a postcode. Don't let archiving fail
  # at this stage because of that as the user cannot be informed at this stage
  # so skip address validation.
  address.skip_validation = true

  address.save!
end

#current_addressObject



59
60
61
62
63
64
# File 'app/models/renalware/letters/recipient.rb', line 59

def current_address
  return address_for_patient if patient?
  return practice_address_for_patient if primary_care_physician?

  address_for_addressee_eg_contact
end

#for_contact?(contact) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'app/models/renalware/letters/recipient.rb', line 66

def for_contact?(contact)
  return false unless person_role.contact?

  addressee_id == contact.id
end

#person_role_present?Boolean

Check we have a person_role. If we don’t add an error to the parent letter if one is present, otherwise add to our errors.

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'app/models/renalware/letters/recipient.rb', line 26

def person_role_present?
  return if person_role.present?

  errs = letter&.errors || errors
  errs.add(:base, "Please select a main recipient")
end

#statment_to_indicate_letter_will_be_emailedObject



72
73
74
75
76
# File 'app/models/renalware/letters/recipient.rb', line 72

def statment_to_indicate_letter_will_be_emailed
  if primary_care_physician? && practice_email_address.present?
    "VIA EMAIL to #{practice_email_address}"
  end
end

#to_sObject



35
36
37
# File 'app/models/renalware/letters/recipient.rb', line 35

def to_s
  (address || current_address).to_s
end