File tree 1 file changed +22
-2
lines changed
1 file changed +22
-2
lines changed Original file line number Diff line number Diff line change 1
1
module ApiV2 ::Helpers ::SharedHelpers
2
2
extend Grape ::API ::Helpers
3
3
4
+ # Use case-insensitive ASCII-based sorting on text columns
4
5
def apply_sort ( relation , secondary_col = nil , secondary_dir = :asc )
5
6
attribute , direction = params [ :sort ] . split ( ":" )
6
- relation = relation . order ( "#{ relation . table_name } .#{ attribute } #{ direction } " )
7
+ table_name = relation . table_name
8
+
9
+ primary_column = relation . connection . schema_cache . columns ( table_name ) . find { |col | col . name == attribute }
10
+ primary_sort =
11
+ if primary_column && %i[ string text ] . include? ( primary_column . type )
12
+ "LOWER(#{ table_name } .#{ attribute } ) COLLATE \" C\" #{ direction } "
13
+ else
14
+ "#{ table_name } .#{ attribute } #{ direction } "
15
+ end
16
+ relation = relation . order ( Arel . sql ( primary_sort ) )
17
+
7
18
if secondary_col && attribute != secondary_col
8
- relation = relation . order ( "#{ relation . table_name } .#{ secondary_col } #{ secondary_dir } " )
19
+ secondary_column = relation . connection . schema_cache . columns ( table_name ) . find { |col | col . name == secondary_col }
20
+ secondary_sort =
21
+ if secondary_column && %i[ string text ] . include? ( secondary_column . type )
22
+ "LOWER(#{ table_name } .#{ secondary_col } ) COLLATE \" C\" #{ secondary_dir } "
23
+ else
24
+ "#{ table_name } .#{ secondary_col } #{ secondary_dir } "
25
+ end
26
+
27
+ relation = relation . order ( Arel . sql ( secondary_sort ) )
9
28
end
29
+
10
30
relation
11
31
end
12
32
You can’t perform that action at this time.
0 commit comments