Class: Renalware::Medications::SummaryPart

Inherits:
SummaryPart
  • Object
show all
Defined in:
app/presenters/renalware/medications/summary_part.rb

Constant Summary

Constants inherited from SummaryPart

SummaryPart::DATE_FORMAT

Instance Method Summary collapse

Methods inherited from SummaryPart

#cache?, #render?

Instance Method Details

#cache_keyObject

The cache key here must be such that a change to any prescription or its associated drug (including a reclassification of drug_type e.g. changing from ESA to immunosuppressant or a change in the drug name) will invalidate the cache for this patient’s prescriptions. So we build a composite cache_key based on prescriptions and their drugs. Note we rely on an a couple of things here:

  • an after_remove callback in Drug -> Classification to set updated_at on the drug; removing classifications uses a delete not a destroy so otherwise would not touch the parent drug.

  • adding a drug classification to a drug (e.g. making a drug ESA) touches the drug and this generates a new cache_key here

  • note that although adding ‘belongs_to :prescription, touch: true` to Drug would solve our cache validation here, that would mean knowing the Drugs module knowing about the Prescriptions module, when currently only the reverse is acceptable (i.e. Prescriptions -> Drugs)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/presenters/renalware/medications/summary_part.rb', line 39

def cache_key
  # Based on AR::Relation.cache_key, this key incorporates the following, scoped to the
  # current patient:
  # - patient cachekey eg renalware/patients/166-20180306184938146827
  # - max(prescriptions.updated)
  # - count(prescriptions)
  # - max(drug.updated_at) in drugs across all prescriptions
  # - count(drugs) across all prescriptions (same as prescriptions.count so not really
  #   required, but comes for free with AR::Relation.cache_key)
  [
    patient.cache_key,
    prescriptions.cache_key,
    Drugs::Drug.where(id: prescriptions.pluck(:drug_id)).cache_key
  ].join("$")
end

#current_prescriptionsObject



11
12
13
14
15
16
17
18
19
20
# File 'app/presenters/renalware/medications/summary_part.rb', line 11

def current_prescriptions
  @current_prescriptions ||= begin
    prescripts = prescriptions
                   .includes(drug: [:drug_types, :classifications])
                   .includes(:medication_route)
                   .current
                   .ordered
    CollectionPresenter.new(prescripts, Medications::PrescriptionPresenter)
  end
end

#to_partial_pathObject



22
23
24
# File 'app/presenters/renalware/medications/summary_part.rb', line 22

def to_partial_path
  "renalware/medications/summary_part"
end