Sha256: 3a539d63f4ea5a4302b301e351f6aac0a7d3e0437aa08b56475184cb608dce16

Contents?: true

Size: 1.98 KB

Versions: 9

Compression:

Stored size: 1.98 KB

Contents

require "renalware/letters/part"
require "ostruct"

module Renalware
  module Letters
    class Part::Prescriptions < DumbDelegator
      include ::PresenterHelper

      def initialize(patient, _event = Event::Unknown.new)
        @patient = patient
        super(prescriptions)
      end

      def to_partial_path
        "renalware/letters/parts/prescriptions"
      end

      private

      def prescriptions
        presenter_klass = ::Renalware::Medications::PrescriptionPresenter
        ::OpenStruct.new(
          current: present(current_prescriptions, presenter_klass),
          recently_changed: present(recently_changed_current_prescriptions, presenter_klass),
          recently_stopped: present(recently_stopped_prescriptions, presenter_klass)
        )
      end

      def current_prescriptions
        @current_prescriptions ||= patient_prescriptions.current
      end

      # Prescriptions created or with dosage changed in the last 14 days.
      # Because we terminated a prescription if the dosage changes, and create a new one,
      # we just need to search for prescriptions created in the last 14 days.
      def recently_changed_current_prescriptions
        @recently_changed_prescriptions ||= begin
          current_prescriptions.prescribed_between(from: 14.days.ago, to: ::Time.zone.now)
        end
      end

      # Find prescriptions terminated within 14 days
      def recently_stopped_prescriptions
        @recently_stopped_prescriptions ||= begin
          patient_prescriptions
            .terminated
            .terminated_between(from: 14.days.ago, to: ::Time.zone.now)
            .where.not(drug_id: current_prescriptions.map(&:drug_id))
        end
      end

      def patient_prescriptions
        @patient_prescriptions ||= begin
          @patient.prescriptions
                   .with_created_by
                   .with_medication_route
                   .with_drugs
                   .with_termination
                   .ordered
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.beta12 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.beta11 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.beta10 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.beta9 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.beta8 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.beta7 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.beta6 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.beta5 app/models/renalware/letters/part/prescriptions.rb
renalware-core-2.0.0.pre.beta4 app/models/renalware/letters/part/prescriptions.rb