3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/pattern_query_helper/sorting.rb', line 3
def self.parse_sorting_params(sort, valid_columns)
sort_sql = []
if sort
sorts = sort.split(",")
sorts.each_with_index do |sort, index|
column = sort.split(":")[0]
direction = sort.split(":")[1]
modifier = sort.split(":")[2]
raise ArgumentError.new("Sorting not allowed on column '#{column}'") unless valid_columns.include? column
if direction == "desc"
case PatternQueryHelper.active_record_adapter
when "sqlite3"
direction = "desc"
else
direction = "desc nulls last"
end
else
direction = "asc"
end
case modifier
when "lowercase"
column = "lower(#{column})"
end
sort_sql << "#{column} #{direction}"
end
end
sort_sql.join(", ")
end
|