aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/algebra/ordering.rb
blob: c1a4ef8b70ff4aa2cb02be96ff18f1454d5ddeeb (plain) (blame)
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
module Arel
  class Ordering < Struct.new(:attribute)
    delegate :relation, :to => :attribute

    def bind(relation)
      self.class.new(attribute.bind(relation))
    end

    def to_ordering
      self
    end

    def eval(row1, row2)
      (attribute.eval(row1) <=> attribute.eval(row2)) * direction
    end

    def to_sql(formatter = Sql::OrderClause.new(relation))
      formatter.ordering self
    end
  end

  class Ascending  < Ordering
    def direction; 1 end
    def direction_sql; 'ASC' end
  end

  class Descending < Ordering
    def direction_sql; 'DESC' end
    def direction; -1 end
  end
end