I'm trying to refactor a piece of code (and to improve my understanding of Arel::Nodes::Case as well) and I would like to convert this Arel.sql statement
Arel.sql(default_order)
def default_order
"CASE
WHEN applications.status = 'new' AND owner_id IS NULL THEN '1'
WHEN applications.is_priority = '1' AND is_read = '0' AND owner_id = '#{current_user.id}' THEN '2'
WHEN applications.is_priority = '1' THEN '3'
ELSE '4'
END, applications.code"
end
into an Arel::Nodes::Case but I'm not getting any good result. Any ideas that could help?
Many thanks in advance.
UPDATE
I did this approach, but I don't like it at all. Too difficult to read and understand:
def default_order
arel_applications = Application.arel_table
Arel::Nodes::Case.new
.when((arel_applications[:status].eq('new'))
.and(arel_applications[:owner_id].eq(nil)), 1)
.when((arel_applications[:is_priority].eq(1))
.and(arel_applications[:is_read].eq(0))
.and(arel_applications[:owner_id].eq(current_user.id)), 2)
.when(arel_applications[:is_priority].eq(1), 3)
.else(4).to_sql
end
Even so, I'm not sure how to get that applications.code that is used in the SQL to order the records.