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
|
# File 'lib/active_record/filter.rb', line 21
def orderize(*attributes)
options = attributes.
if options.any?
include_table = options.fetch(:on)
table_name = include_table.to_s.classify.constantize.table_name
attributes = "#{table_name}.#{attributes.first}" if include_table.present?
else
attributes = [:name] if attributes.empty?
attributes.map! do |attribute|
attribute = arel_table[attribute].asc if attribute.is_a?(Symbol)
if attribute.is_a?(Arel::Attributes::Attribute)
attribute = attribute.asc
end
if attribute.class < Arel::Nodes::Ordering
arel_table.engine.connection.visitor.accept attribute
else
attribute
end
end
attributes = attributes.join(', ')
end
class_eval(" def self.ordered\n query = order('\#{attributes}')\n\n \#{\"query = query.joins(:\#{include_table})\" if include_table.present?}\n\n query\n end\n eoruby\nend\n", __FILE__, __LINE__ + 1)
|