Initial commit
This commit is contained in:
24
app/models/custom_rank_order.rb
Normal file
24
app/models/custom_rank_order.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
class CustomRankOrder
|
||||
def initialize(attribute, ordered_values)
|
||||
@attribute = attribute
|
||||
@ordered_values = ordered_values
|
||||
end
|
||||
|
||||
def sql
|
||||
Arel.sql case_statement
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_accessor :attribute, :ordered_values
|
||||
|
||||
def case_statement
|
||||
["CASE", case_conditions, "END"].join(" ")
|
||||
end
|
||||
|
||||
def case_conditions
|
||||
ordered_values.map.with_index do |release, index|
|
||||
"WHEN #{attribute}='#{release}' THEN '#{index+1}'"
|
||||
end.join(" ")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user