6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/sorty/sort.rb', line 6
def sorty(params)
return scoped if params[:column].blank?
column = params[:column].dup
direction = params[:direction] || 'asc'
relation = column[/^(.*)\./, 1]
if relation && self.reflections.has_key?(relation.to_sym)
column.gsub!(/#{relation}\./, "#{self.reflections[relation.to_sym].klass.table_name}.")
query = includes(relation.to_sym)
else
column = "#{table_name}.#{column}"
query = scoped
end
query.reorder("#{column} #{direction}")
end
|