Class: SQL::IndexedCaseStmt

Inherits:
Object
  • Object
show all
Defined in:
lib/sql/indexed_case_stmt.rb

Overview

Example:

indexed_case_stmt(:code, "DT", "AC", "XY")

Will return the string:

CASE code
   WHEN 'DT' THEN 1
   WHEN 'AC' THEN 2
END

Used for creating an explicit sort order in conjunction with a find:

Description.where(code: codes).order(indexed_case_stmt(:code, codes))

Instance Method Summary collapse

Instance Method Details

#generateObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/sql/indexed_case_stmt.rb', line 24

def generate
  return if items.blank?

  clauses = []
  Array(items).each_with_index do |item, index|
    clauses << Arel.sql("WHEN '#{item}' THEN #{index}")
  end

  Arel.sql("CASE #{column} #{clauses.join(' ')} END")
end