/
order.rb
84 lines (64 loc) · 1.82 KB
/
order.rb
1
2
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Administrate
class Order
def initialize(attribute = nil, direction = nil)
@attribute = attribute
@direction = direction || :asc
end
def apply(relation)
return order_by_association(relation) unless
reflect_association(relation).nil?
order = "#{relation.table_name}.#{attribute} #{direction}"
return relation.reorder(Arel.sql(order)) if
relation.columns_hash.keys.include?(attribute.to_s)
relation
end
def ordered_by?(attr)
attr.to_s == attribute.to_s
end
def order_params_for(attr)
{
order: attr,
direction: reversed_direction_param_for(attr)
}
end
attr_reader :direction
private
attr_reader :attribute
def reversed_direction_param_for(attr)
if ordered_by?(attr)
opposite_direction
else
:asc
end
end
def opposite_direction
direction.to_sym == :asc ? :desc : :asc
end
def order_by_association(relation)
return order_by_count(relation) if has_many_attribute?(relation)
return order_by_id(relation) if belongs_to_attribute?(relation)
relation
end
def order_by_count(relation)
relation.
left_joins(attribute.to_sym).
group(:id).
reorder("COUNT(#{attribute}.id) #{direction}")
end
def order_by_id(relation)
relation.reorder("#{foreign_key(relation)} #{direction}")
end
def has_many_attribute?(relation)
reflect_association(relation).macro == :has_many
end
def belongs_to_attribute?(relation)
reflect_association(relation).macro == :belongs_to
end
def reflect_association(relation)
relation.klass.reflect_on_association(attribute.to_s)
end
def foreign_key(relation)
reflect_association(relation).foreign_key
end
end
end