Class: Renalware::Letters::Letter

Inherits:
ApplicationRecord show all
Extended by:
Enumerize
Includes:
Accountable, ExplicitStateModel
Defined in:
app/models/renalware/letters/letter.rb

Direct Known Subclasses

Approved, Completed, Draft, PendingReview

Defined Under Namespace

Classes: Approved, Completed, Draft, PendingReview

Constant Summary collapse

EVENTS_MAP =
{
  Clinics::ClinicVisit => Event::ClinicVisit,
  NilClass => Event::Unknown
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Accountable

#first_or_create_by!, #save_by, #save_by!, #update_by

Class Method Details

.for_event(event) ⇒ Object



76
77
78
# File 'app/models/renalware/letters/letter.rb', line 76

def self.for_event(event)
  where(event: event).first
end

.policy_classObject



72
73
74
# File 'app/models/renalware/letters/letter.rb', line 72

def self.policy_class
  LetterPolicy
end

Instance Method Details

#archive_recipients!Object



123
124
125
# File 'app/models/renalware/letters/letter.rb', line 123

def archive_recipients!
  recipients.each(&:archive!)
end

#archived?Boolean



115
116
117
# File 'app/models/renalware/letters/letter.rb', line 115

def archived?
  archive.present?
end

#archived_byObject



119
120
121
# File 'app/models/renalware/letters/letter.rb', line 119

def archived_by
  archive.created_by
end

#determine_counterpart_ccsObject



107
108
109
# File 'app/models/renalware/letters/letter.rb', line 107

def determine_counterpart_ccs
  DetermineCounterpartCCs.new(self).call
end

#find_cc_recipient_for_contact(contact) ⇒ Object



103
104
105
# File 'app/models/renalware/letters/letter.rb', line 103

def find_cc_recipient_for_contact(contact)
  cc_recipients.detect { |recipient| recipient.for_contact?(contact) }
end

#letter_eventObject

A Letter Event is unrelated to Events::Event. Instead it is an un-persisted decorator around the polymorphic event relationship (determined by event_class and event_id); each concrete Event class decorates that relationship with some helpers, for example #part_classes which determined which extra letter ‘parts’ should be rendered for that specific polymorphic relationship. Note that general clinical parts_classes are defined on Letter, because a relationship to say a ClinicVisit (the letter event) is not required for a letter to want to have clinical content (prescriptions etc.). Clinical parts will always be included if #clinical? is true. This for example enables us to create a ‘clinical letter’ which is a simple letter with the added clinical parts, but which is unrelated to a clinic_visit for instance.



95
96
97
# File 'app/models/renalware/letters/letter.rb', line 95

def letter_event
  EVENTS_MAP.fetch(event.class).new(event, clinical: clinical?)
end

#signed?Boolean



111
112
113
# File 'app/models/renalware/letters/letter.rb', line 111

def signed?
  signature.present?
end

#subject?(other_patient) ⇒ Boolean



99
100
101
# File 'app/models/renalware/letters/letter.rb', line 99

def subject?(other_patient)
  patient.id == other_patient.id
end