Class: Renalware::Pathology::ObservationDescriptionsByCodeQuery

Inherits:
Object
  • Object
show all
Defined in:
app/models/renalware/pathology/observation_descriptions_by_code_query.rb

Instance Method Summary collapse

Constructor Details

#initialize(codes:, relation: ObservationDescription) ⇒ ObservationDescriptionsByCodeQuery

Returns a new instance of ObservationDescriptionsByCodeQuery.



9
10
11
12
# File 'app/models/renalware/pathology/observation_descriptions_by_code_query.rb', line 9

def initialize(codes:, relation: ObservationDescription)
  @relation = relation
  @codes = Array(codes)
end

Instance Method Details

#callObject

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">,
  ...
]


30
31
32
33
34
35
# File 'app/models/renalware/pathology/observation_descriptions_by_code_query.rb', line 30

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