Sha256: 17572e7ffc4cbd4abeda38eee34c3fbbd2657f1e07ebb4b23e680c6a74604661

Contents?: true

Size: 1.56 KB

Versions: 17

Compression:

Stored size: 1.56 KB

Contents

require_dependency "renalware/pathology"
require "sql/index_case_stmt"

module Renalware
  module Pathology
    class ObservationDescriptionsByCodeQuery
      def initialize(relation: ObservationDescription, codes:)
        @relation = relation
        @codes = codes
      end

      # Executes SQL that looks like this:
      #   SELECT "pathology_observation_descriptions".* FROM "pathology_observation_descriptions"
      #   WHERE "pathology_observation_descriptions"."code" IN ('HGB', 'MCV', 'MCH',...
      #   ORDER BY CASE code
      #     WHEN 'HGB' THEN 0
      #     WHEN 'MCV' THEN 1
      #     WHEN 'MCH' THEN 2
      #     ...
      #   END
      # and returns results that look like this:
      #   [
      #     #<Renalware::Pathology::ObservationDescription id: 767, code: "HGB", name: "HGB">,
      #     #<Renalware::Pathology::ObservationDescription id: 1058, code: "MCV", name: "MCV">,
      #     #<Renalware::Pathology::ObservationDescription id: 1055, code: "MCH", name: "MCH">,
      #     ...
      #   ]
      def call
        stmt = SQL::IndexedCaseStmt.new(:code, @codes) # Generate a CASE statement for ordering
        records = @relation.where(code: @codes).order(stmt.generate)
        verify_all_records_found(records)
        records
      end

      private

      def verify_all_records_found(records)
        found_codes = records.map(&:code)

        missing_records = found_codes - @codes
        if missing_records.present?
          raise ActiveRecord::RecordNotFound, "Missing records for #{missing_records}"
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.rc9 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta11 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta10 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta9 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta8 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta7 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta6 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta5 app/models/renalware/pathology/observation_descriptions_by_code_query.rb
renalware-core-2.0.0.pre.beta4 app/models/renalware/pathology/observation_descriptions_by_code_query.rb