Skip to content

Commit

Permalink
Merge pull request #64 from ernie/reverse_ordering
Browse files Browse the repository at this point in the history
Allow reversal of orderings
  • Loading branch information
tenderlove committed Jun 21, 2011
2 parents 5930020 + ba3578a commit 645afa0
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 30 deletions.
3 changes: 2 additions & 1 deletion lib/arel/nodes.rb
Expand Up @@ -11,6 +11,8 @@

# unary
require 'arel/nodes/unary'
require 'arel/nodes/ascending'
require 'arel/nodes/descending'
require 'arel/nodes/unqualified_column'
require 'arel/nodes/with'

Expand All @@ -19,7 +21,6 @@
require 'arel/nodes/equality'
require 'arel/nodes/in' # Why is this subclassed from equality?
require 'arel/nodes/join_source'
require 'arel/nodes/ordering'
require 'arel/nodes/delete_statement'
require 'arel/nodes/table_alias'
require 'arel/nodes/infix_operation'
Expand Down
23 changes: 23 additions & 0 deletions lib/arel/nodes/ascending.rb
@@ -0,0 +1,23 @@
module Arel
module Nodes
class Ascending < Ordering

def reverse
Descending.new(expr)
end

def direction
:asc
end

def ascending?
true
end

def descending?
false
end

end
end
end
23 changes: 23 additions & 0 deletions lib/arel/nodes/descending.rb
@@ -0,0 +1,23 @@
module Arel
module Nodes
class Descending < Ordering

def reverse
Ascending.new(expr)
end

def direction
:desc
end

def ascending?
false
end

def descending?
true
end

end
end
end
16 changes: 1 addition & 15 deletions lib/arel/nodes/ordering.rb
@@ -1,20 +1,6 @@
module Arel
module Nodes
class Ordering < Arel::Nodes::Binary
alias :expr :left
alias :direction :right

def initialize expr, direction = :asc
super
end

def ascending?
direction == :asc
end

def descending?
direction == :desc
end
class Ordering < Unary
end
end
end
1 change: 1 addition & 0 deletions lib/arel/nodes/unary.rb
Expand Up @@ -18,6 +18,7 @@ def initialize expr
Not
Offset
On
Ordering
Top
Lock
DistinctOn
Expand Down
4 changes: 2 additions & 2 deletions lib/arel/order_predications.rb
Expand Up @@ -2,11 +2,11 @@ module Arel
module OrderPredications

def asc
Nodes::Ordering.new self, :asc
Nodes::Ascending.new self
end

def desc
Nodes::Ordering.new self, :desc
Nodes::Descending.new self
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/arel/visitors/depth_first.rb
Expand Up @@ -22,6 +22,7 @@ def unary o
alias :visit_Arel_Nodes_Not :unary
alias :visit_Arel_Nodes_Offset :unary
alias :visit_Arel_Nodes_On :unary
alias :visit_Arel_Nodes_Ordering :unary
alias :visit_Arel_Nodes_Top :unary
alias :visit_Arel_Nodes_UnqualifiedColumn :unary

Expand Down Expand Up @@ -75,7 +76,6 @@ def binary o
alias :visit_Arel_Nodes_NotEqual :binary
alias :visit_Arel_Nodes_NotIn :binary
alias :visit_Arel_Nodes_Or :binary
alias :visit_Arel_Nodes_Ordering :binary
alias :visit_Arel_Nodes_OuterJoin :binary
alias :visit_Arel_Nodes_TableAlias :binary
alias :visit_Arel_Nodes_Values :binary
Expand Down
1 change: 0 additions & 1 deletion lib/arel/visitors/dot.rb
Expand Up @@ -30,7 +30,6 @@ def accept object
private
def visit_Arel_Nodes_Ordering o
visit_edge o, "expr"
visit_edge o, "direction"
end

def visit_Arel_Nodes_TableAlias o
Expand Down
8 changes: 6 additions & 2 deletions lib/arel/visitors/to_sql.rb
Expand Up @@ -203,8 +203,12 @@ def visit_Arel_Nodes_Grouping o
"(#{visit o.expr})"
end

def visit_Arel_Nodes_Ordering o
"#{visit o.expr} #{o.descending? ? 'DESC' : 'ASC'}"
def visit_Arel_Nodes_Ascending o
"#{visit o.expr} ASC"
end

def visit_Arel_Nodes_Descending o
"#{visit o.expr} DESC"
end

def visit_Arel_Nodes_Group o
Expand Down
8 changes: 4 additions & 4 deletions test/attributes/test_attribute.rb
Expand Up @@ -619,9 +619,9 @@ module Attributes
end

describe '#asc' do
it 'should create an Ordering node' do
it 'should create an Ascending node' do
relation = Table.new(:users)
relation[:id].asc.must_be_kind_of Nodes::Ordering
relation[:id].asc.must_be_kind_of Nodes::Ascending
end

it 'should generate ASC in sql' do
Expand All @@ -635,9 +635,9 @@ module Attributes
end

describe '#desc' do
it 'should create an Ordering node' do
it 'should create a Descending node' do
relation = Table.new(:users)
relation[:id].desc.must_be_kind_of Nodes::Ordering
relation[:id].desc.must_be_kind_of Nodes::Descending
end

it 'should generate DESC in sql' do
Expand Down
34 changes: 34 additions & 0 deletions test/nodes/test_ascending.rb
@@ -0,0 +1,34 @@
require 'helper'

module Arel
module Nodes
class TestAscending < MiniTest::Unit::TestCase
def test_construct
ascending = Ascending.new 'zomg'
assert_equal 'zomg', ascending.expr
end

def test_reverse
ascending = Ascending.new 'zomg'
descending = ascending.reverse
assert_kind_of Descending, descending
assert_equal ascending.expr, descending.expr
end

def test_direction
ascending = Ascending.new 'zomg'
assert_equal :asc, ascending.direction
end

def test_ascending?
ascending = Ascending.new 'zomg'
assert ascending.ascending?
end

def test_descending?
ascending = Ascending.new 'zomg'
assert !ascending.descending?
end
end
end
end
34 changes: 34 additions & 0 deletions test/nodes/test_descending.rb
@@ -0,0 +1,34 @@
require 'helper'

module Arel
module Nodes
class TestDescending < MiniTest::Unit::TestCase
def test_construct
descending = Descending.new 'zomg'
assert_equal 'zomg', descending.expr
end

def test_reverse
descending = Descending.new 'zomg'
ascending = descending.reverse
assert_kind_of Ascending, ascending
assert_equal descending.expr, ascending.expr
end

def test_direction
descending = Descending.new 'zomg'
assert_equal :desc, descending.direction
end

def test_ascending?
descending = Descending.new 'zomg'
assert !descending.ascending?
end

def test_descending?
descending = Descending.new 'zomg'
assert descending.descending?
end
end
end
end
4 changes: 2 additions & 2 deletions test/nodes/test_infix_operation.rb
Expand Up @@ -21,9 +21,9 @@ def test_operation_alias
def test_opertaion_ordering
operation = InfixOperation.new :+, 1, 2
ordering = operation.desc
assert_kind_of Ordering, ordering
assert_kind_of Descending, ordering
assert_equal operation, ordering.expr
assert_equal :desc, ordering.direction
assert ordering.descending?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/test_depth_first.rb
Expand Up @@ -28,6 +28,7 @@ def test_raises_with_object
Arel::Nodes::On,
Arel::Nodes::Grouping,
Arel::Nodes::Offset,
Arel::Nodes::Ordering,
Arel::Nodes::Having,
Arel::Nodes::StringJoin,
Arel::Nodes::UnqualifiedColumn,
Expand Down Expand Up @@ -104,7 +105,6 @@ def test_outer_join
Arel::Nodes::Values,
Arel::Nodes::As,
Arel::Nodes::DeleteStatement,
Arel::Nodes::Ordering,
Arel::Nodes::JoinSource,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/test_dot.rb
Expand Up @@ -33,6 +33,7 @@ def test_named_function
Arel::Nodes::On,
Arel::Nodes::Grouping,
Arel::Nodes::Offset,
Arel::Nodes::Ordering,
Arel::Nodes::Having,
Arel::Nodes::UnqualifiedColumn,
Arel::Nodes::Top,
Expand Down Expand Up @@ -63,7 +64,6 @@ def test_named_function
Arel::Nodes::Values,
Arel::Nodes::As,
Arel::Nodes::DeleteStatement,
Arel::Nodes::Ordering,
Arel::Nodes::JoinSource,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
Expand Down

2 comments on commit 645afa0

@arunagw
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@raghunadhd
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Please sign in to comment.