Class: Renalware::Letters::PdfLetterCache

Inherits:
Object
  • Object
show all
Defined in:
app/models/renalware/letters/pdf_letter_cache.rb

Class Method Summary collapse

Class Method Details

.cache_key_for(letter, **options) ⇒ Object

Note the letter must be a LetterPresenter which has a #to_html method The to_html method should (and does on the LetterPrsenter class) render the complete html including surrounding layout with inline css and images. This way if the layout changes or the image is changed for example, the cache for the pdf is no longer valid and a new key and cache entry will be created.



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

def cache_key_for(letter, **options)
  timestamp = letter&.updated_at&.strftime("%Y%m%d%H%M%S")
  pat_id = letter.patient.id
  "letter-pdf-#{letter.id}-#{pat_id}-#{timestamp}-" \
  "#{Digest::MD5.hexdigest(letter.to_html(**options))}"
end

.cache_pathObject



58
59
60
# File 'app/models/renalware/letters/pdf_letter_cache.rb', line 58

def cache_path
  Rails.root.join("tmp", "pdf_letter_cache")
end

.fetch(letter, **options) ⇒ Object



42
43
44
# File 'app/models/renalware/letters/pdf_letter_cache.rb', line 42

def fetch(letter, **options)
  store.fetch(cache_key_for(letter, **options)) { yield }
end

.storeObject



62
63
64
# File 'app/models/renalware/letters/pdf_letter_cache.rb', line 62

def store
  @store ||= ActiveSupport::Cache::FileStore.new(cache_path)
end