Class: UpdateByCase::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/update_by_case/utils.rb

Constant Summary collapse

WHERE_OPTION =
'where'.freeze

Class Method Summary collapse

Class Method Details

.build_case_statement(model, attribute_name, key_attribute, cases) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/update_by_case/utils.rb', line 9

def build_case_statement(model, attribute_name, key_attribute, cases)
  case_statement = Arel::Nodes::Case.new
  arel_table = model.arel_table
  unless cases.empty?
    cases.each do |xcase|
      condition = arel_table[key_attribute].eq(xcase.first)
      case_statement.when(condition).then(xcase.second)
    end
  else
    case_statement.when(false).then(arel_table[attribute_name])
  end
  case_statement.else(arel_table[attribute_name])
end