Sha256: e05522a4a3962d042f839aa1261e533933c1635a46e665681da2c10f7e5a7d94

Contents?: true

Size: 1.82 KB

Versions: 17

Compression:

Stored size: 1.82 KB

Contents

require "renalware/letters/part"
require "attr_extras"

# When rendered, the template in `to_partial_path` will be used, and our Part object here will be
# available in the partial as `recent_pathology_results`.
module Renalware
  module Letters
    class Part::RecentPathologyResults < Part
      delegate :each, :any?, :present?, to: :results

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

      def results
        @recent_pathology_results ||= begin
          snapshot = letter.pathology_snapshot
          return if snapshot.blank?
          group_snapshot_by_code_and_date(snapshot)
        end
      end

      private

      def group_snapshot_by_code_and_date(snapshot)
        snapshot = snapshot_ordered_by_obx_codes_relevant_to_letters(snapshot)
        snapshot_grouped_by_date = group_snapshot_by_date(snapshot)
        format_pathology_string(snapshot_grouped_by_date)
      end

      def snapshot_ordered_by_obx_codes_relevant_to_letters(snapshot)
        RelevantObservationDescription.codes.each_with_object({}) do |code, hash|
          hash[code.to_sym] = snapshot[code.to_sym]
        end
      end

      def group_snapshot_by_date(snapshot)
        current_date = NullObject.instance
        snapshot.each_with_object({}) do |observation, h|
          code, obs = observation
          next if obs.nil?
          date = Time.zone.parse(obs[:observed_at])

          if date != current_date
            current_date = date
            h[date] = []
          end

          h[date] << "#{code} #{obs[:result]}"
        end
      end

      def format_pathology_string(grouped_snapshot)
        str = ""
        grouped_snapshot.each do |date, observations|
          str << "#{I18n.l(date&.to_date)}: #{observations.join(', ')}; "
        end
        str.strip
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.7 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.5 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.4 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.3 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.2 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.1 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/letters/part/recent_pathology_results.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/letters/part/recent_pathology_results.rb