Skip to content
This repository
Browse code

Merge pull request #64 from ernie/reverse_ordering

Allow reversal of orderings
  • Loading branch information...
commit 645afa0c08af21381b63831e8f488a186a6ad9b0 2 parents 5930020 + ba3578a
Aaron Patterson tenderlove authored
3  lib/arel/nodes.rb
@@ -11,6 +11,8 @@
11 11
12 12 # unary
13 13 require 'arel/nodes/unary'
  14 +require 'arel/nodes/ascending'
  15 +require 'arel/nodes/descending'
14 16 require 'arel/nodes/unqualified_column'
15 17 require 'arel/nodes/with'
16 18
@@ -19,7 +21,6 @@
19 21 require 'arel/nodes/equality'
20 22 require 'arel/nodes/in' # Why is this subclassed from equality?
21 23 require 'arel/nodes/join_source'
22   -require 'arel/nodes/ordering'
23 24 require 'arel/nodes/delete_statement'
24 25 require 'arel/nodes/table_alias'
25 26 require 'arel/nodes/infix_operation'
23 lib/arel/nodes/ascending.rb
... ... @@ -0,0 +1,23 @@
  1 +module Arel
  2 + module Nodes
  3 + class Ascending < Ordering
  4 +
  5 + def reverse
  6 + Descending.new(expr)
  7 + end
  8 +
  9 + def direction
  10 + :asc
  11 + end
  12 +
  13 + def ascending?
  14 + true
  15 + end
  16 +
  17 + def descending?
  18 + false
  19 + end
  20 +
  21 + end
  22 + end
  23 +end
23 lib/arel/nodes/descending.rb
... ... @@ -0,0 +1,23 @@
  1 +module Arel
  2 + module Nodes
  3 + class Descending < Ordering
  4 +
  5 + def reverse
  6 + Ascending.new(expr)
  7 + end
  8 +
  9 + def direction
  10 + :desc
  11 + end
  12 +
  13 + def ascending?
  14 + false
  15 + end
  16 +
  17 + def descending?
  18 + true
  19 + end
  20 +
  21 + end
  22 + end
  23 +end
16 lib/arel/nodes/ordering.rb
... ... @@ -1,20 +1,6 @@
1 1 module Arel
2 2 module Nodes
3   - class Ordering < Arel::Nodes::Binary
4   - alias :expr :left
5   - alias :direction :right
6   -
7   - def initialize expr, direction = :asc
8   - super
9   - end
10   -
11   - def ascending?
12   - direction == :asc
13   - end
14   -
15   - def descending?
16   - direction == :desc
17   - end
  3 + class Ordering < Unary
18 4 end
19 5 end
20 6 end
1  lib/arel/nodes/unary.rb
@@ -18,6 +18,7 @@ def initialize expr
18 18 Not
19 19 Offset
20 20 On
  21 + Ordering
21 22 Top
22 23 Lock
23 24 DistinctOn
4 lib/arel/order_predications.rb
@@ -2,11 +2,11 @@ module Arel
2 2 module OrderPredications
3 3
4 4 def asc
5   - Nodes::Ordering.new self, :asc
  5 + Nodes::Ascending.new self
6 6 end
7 7
8 8 def desc
9   - Nodes::Ordering.new self, :desc
  9 + Nodes::Descending.new self
10 10 end
11 11
12 12 end
2  lib/arel/visitors/depth_first.rb
@@ -22,6 +22,7 @@ def unary o
22 22 alias :visit_Arel_Nodes_Not :unary
23 23 alias :visit_Arel_Nodes_Offset :unary
24 24 alias :visit_Arel_Nodes_On :unary
  25 + alias :visit_Arel_Nodes_Ordering :unary
25 26 alias :visit_Arel_Nodes_Top :unary
26 27 alias :visit_Arel_Nodes_UnqualifiedColumn :unary
27 28
@@ -75,7 +76,6 @@ def binary o
75 76 alias :visit_Arel_Nodes_NotEqual :binary
76 77 alias :visit_Arel_Nodes_NotIn :binary
77 78 alias :visit_Arel_Nodes_Or :binary
78   - alias :visit_Arel_Nodes_Ordering :binary
79 79 alias :visit_Arel_Nodes_OuterJoin :binary
80 80 alias :visit_Arel_Nodes_TableAlias :binary
81 81 alias :visit_Arel_Nodes_Values :binary
1  lib/arel/visitors/dot.rb
@@ -30,7 +30,6 @@ def accept object
30 30 private
31 31 def visit_Arel_Nodes_Ordering o
32 32 visit_edge o, "expr"
33   - visit_edge o, "direction"
34 33 end
35 34
36 35 def visit_Arel_Nodes_TableAlias o
8 lib/arel/visitors/to_sql.rb
@@ -203,8 +203,12 @@ def visit_Arel_Nodes_Grouping o
203 203 "(#{visit o.expr})"
204 204 end
205 205
206   - def visit_Arel_Nodes_Ordering o
207   - "#{visit o.expr} #{o.descending? ? 'DESC' : 'ASC'}"
  206 + def visit_Arel_Nodes_Ascending o
  207 + "#{visit o.expr} ASC"
  208 + end
  209 +
  210 + def visit_Arel_Nodes_Descending o
  211 + "#{visit o.expr} DESC"
208 212 end
209 213
210 214 def visit_Arel_Nodes_Group o
8 test/attributes/test_attribute.rb
@@ -619,9 +619,9 @@ module Attributes
619 619 end
620 620
621 621 describe '#asc' do
622   - it 'should create an Ordering node' do
  622 + it 'should create an Ascending node' do
623 623 relation = Table.new(:users)
624   - relation[:id].asc.must_be_kind_of Nodes::Ordering
  624 + relation[:id].asc.must_be_kind_of Nodes::Ascending
625 625 end
626 626
627 627 it 'should generate ASC in sql' do
@@ -635,9 +635,9 @@ module Attributes
635 635 end
636 636
637 637 describe '#desc' do
638   - it 'should create an Ordering node' do
  638 + it 'should create a Descending node' do
639 639 relation = Table.new(:users)
640   - relation[:id].desc.must_be_kind_of Nodes::Ordering
  640 + relation[:id].desc.must_be_kind_of Nodes::Descending
641 641 end
642 642
643 643 it 'should generate DESC in sql' do
34 test/nodes/test_ascending.rb
... ... @@ -0,0 +1,34 @@
  1 +require 'helper'
  2 +
  3 +module Arel
  4 + module Nodes
  5 + class TestAscending < MiniTest::Unit::TestCase
  6 + def test_construct
  7 + ascending = Ascending.new 'zomg'
  8 + assert_equal 'zomg', ascending.expr
  9 + end
  10 +
  11 + def test_reverse
  12 + ascending = Ascending.new 'zomg'
  13 + descending = ascending.reverse
  14 + assert_kind_of Descending, descending
  15 + assert_equal ascending.expr, descending.expr
  16 + end
  17 +
  18 + def test_direction
  19 + ascending = Ascending.new 'zomg'
  20 + assert_equal :asc, ascending.direction
  21 + end
  22 +
  23 + def test_ascending?
  24 + ascending = Ascending.new 'zomg'
  25 + assert ascending.ascending?
  26 + end
  27 +
  28 + def test_descending?
  29 + ascending = Ascending.new 'zomg'
  30 + assert !ascending.descending?
  31 + end
  32 + end
  33 + end
  34 +end
34 test/nodes/test_descending.rb
... ... @@ -0,0 +1,34 @@
  1 +require 'helper'
  2 +
  3 +module Arel
  4 + module Nodes
  5 + class TestDescending < MiniTest::Unit::TestCase
  6 + def test_construct
  7 + descending = Descending.new 'zomg'
  8 + assert_equal 'zomg', descending.expr
  9 + end
  10 +
  11 + def test_reverse
  12 + descending = Descending.new 'zomg'
  13 + ascending = descending.reverse
  14 + assert_kind_of Ascending, ascending
  15 + assert_equal descending.expr, ascending.expr
  16 + end
  17 +
  18 + def test_direction
  19 + descending = Descending.new 'zomg'
  20 + assert_equal :desc, descending.direction
  21 + end
  22 +
  23 + def test_ascending?
  24 + descending = Descending.new 'zomg'
  25 + assert !descending.ascending?
  26 + end
  27 +
  28 + def test_descending?
  29 + descending = Descending.new 'zomg'
  30 + assert descending.descending?
  31 + end
  32 + end
  33 + end
  34 +end
4 test/nodes/test_infix_operation.rb
@@ -21,9 +21,9 @@ def test_operation_alias
21 21 def test_opertaion_ordering
22 22 operation = InfixOperation.new :+, 1, 2
23 23 ordering = operation.desc
24   - assert_kind_of Ordering, ordering
  24 + assert_kind_of Descending, ordering
25 25 assert_equal operation, ordering.expr
26   - assert_equal :desc, ordering.direction
  26 + assert ordering.descending?
27 27 end
28 28 end
29 29 end
2  test/visitors/test_depth_first.rb
@@ -28,6 +28,7 @@ def test_raises_with_object
28 28 Arel::Nodes::On,
29 29 Arel::Nodes::Grouping,
30 30 Arel::Nodes::Offset,
  31 + Arel::Nodes::Ordering,
31 32 Arel::Nodes::Having,
32 33 Arel::Nodes::StringJoin,
33 34 Arel::Nodes::UnqualifiedColumn,
@@ -104,7 +105,6 @@ def test_outer_join
104 105 Arel::Nodes::Values,
105 106 Arel::Nodes::As,
106 107 Arel::Nodes::DeleteStatement,
107   - Arel::Nodes::Ordering,
108 108 Arel::Nodes::JoinSource,
109 109 ].each do |klass|
110 110 define_method("test_#{klass.name.gsub('::', '_')}") do
2  test/visitors/test_dot.rb
@@ -33,6 +33,7 @@ def test_named_function
33 33 Arel::Nodes::On,
34 34 Arel::Nodes::Grouping,
35 35 Arel::Nodes::Offset,
  36 + Arel::Nodes::Ordering,
36 37 Arel::Nodes::Having,
37 38 Arel::Nodes::UnqualifiedColumn,
38 39 Arel::Nodes::Top,
@@ -63,7 +64,6 @@ def test_named_function
63 64 Arel::Nodes::Values,
64 65 Arel::Nodes::As,
65 66 Arel::Nodes::DeleteStatement,
66   - Arel::Nodes::Ordering,
67 67 Arel::Nodes::JoinSource,
68 68 ].each do |klass|
69 69 define_method("test_#{klass.name.gsub('::', '_')}") do

2 comments on commit 645afa0

Arun Agrawal

I think we need a new version 2.1.2 here. As Rails tests are failing. for reverse. Rails with Arel master works fine.

Raghunadh

+1

Please sign in to comment.
Something went wrong with that request. Please try again.