Sha256: 01ff863e05fa64cf8e6090a334376e81d97a9ed05a32c48646656465235aba82

Contents?: true

Size: 1.71 KB

Versions: 15

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/pathology"
require "attr_extras"

module Renalware
  module Pathology
    # A helper class wrapping a custom relation object which has aggregated a
    # patient's pathology results by date of observation using raw SQL + PGResult.
    # Note we implement the required interface for kaminari pagination (we delegate to the
    # underlying relation to achieve this).
    #
    # This class exists to make the underlying PGResult set (wrapped in a custom relation-like
    # object) easier to consume.
    #
    # Example usage:
    # (ruby)
    # codes = %i(HGB PLT WBC)
    # rows = ObservationsGroupedByDateQuery.new(patient_id: 1, codes: codes)
    # table = ObservationsGroupedByDateTable.new(rows: rows, codes: %i(HGB PLT WBC))
    # (html)
    # table
    #   tr
    #     td Date
    #     - table.codes do |code|
    #       td= code
    #   tbody
    #     tr
    #       - table.each_row do |row|
    #         td= l(row.observed_on)
    #         - table.codes.each do |code|
    #           td= row.result_for(code)
    #
    # = paginate(table)
    #
    class ObservationsGroupedByDateTable
      attr_reader_initialize [:observation_descriptions!, :relation!]
      delegate :current_page, :total_pages, :limit_value, to: :relation

      def rows
        @rows ||= relation.all.map(&:with_indifferent_access).map { |row| Row.new(row) }
      end

      class Row
        pattr_initialize :row

        def observed_on
          Date.parse(row[:observed_on])
        end

        def result_for(code)
          results[code&.to_sym]
        end

        def results
          @results = JSON.parse(row[:results]).with_indifferent_access
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
renalware-core-2.0.25 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.24 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.23 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.22 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.21 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.20 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.18 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.17 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.16 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.15 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.14 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.13 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.12 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.11 app/models/renalware/pathology/observations_grouped_by_date_table.rb
renalware-core-2.0.9 app/models/renalware/pathology/observations_grouped_by_date_table.rb