13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'app/controllers/concerns/headmin/sortable.rb', line 13
def sort_by_params(collection)
sort_params.each do |key, value|
next unless %w[asc desc].include?(value)
attribute = key.gsub("sort_", "")
if collection.attribute_names.include?(attribute.to_s)
column = collection.arel_table[attribute]
elsif collection.respond_to?(:translation_class) && collection.translation_class.attribute_names.include?(attribute.to_s)
column = collection.translation_class.arel_table[attribute]
else
next
end
order_scope = "order_by_#{attribute}".to_sym
sort_scope = "sort_by_#{attribute}".to_sym
collection = if collection.respond_to?(order_scope)
collection.send(order_scope, value)
elsif collection.respond_to?(sort_scope)
collection.send(sort_scope, value)
else
collection.order(column.send(value))
end
end
collection
end
|