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



40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/renalware/letters/recipient.rb', line 40

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



52
53
54
55
56
57
# File 'app/models/renalware/letters/recipient.rb', line 52

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)


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

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)


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

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



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

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



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

def to_s
  (address || current_address).to_s
end