aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-03-04 12:53:15 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-03-04 12:53:15 -0800
commitda8f6c7af7ea2305618f02378736c013f61646f1 (patch)
tree58be03c7c513a725fd862881c873ed19967d4f73
parent518d6b34e863d68b009674a45a1fe5105a38aee1 (diff)
parent0df9ab8442fc4a33b1962a49cebd66a3d1b0faf5 (diff)
downloadrails-da8f6c7af7ea2305618f02378736c013f61646f1.tar.gz
rails-da8f6c7af7ea2305618f02378736c013f61646f1.tar.bz2
rails-da8f6c7af7ea2305618f02378736c013f61646f1.zip
Merge pull request #164 from tpope/infinity-ranges
Support Float::INFINITY in ranges
-rw-r--r--lib/arel/predications.rb20
-rw-r--r--test/visitors/test_to_sql.rb34
2 files changed, 52 insertions, 2 deletions
diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb
index e3f72d46a2..c485de07e3 100644
--- a/lib/arel/predications.rb
+++ b/lib/arel/predications.rb
@@ -29,7 +29,15 @@ module Arel
when Arel::SelectManager
Arel::Nodes::In.new(self, other.ast)
when Range
- if other.exclude_end?
+ if other.begin == -Float::INFINITY && other.end == Float::INFINITY
+ Nodes::NotIn.new self, []
+ elsif other.end == Float::INFINITY
+ Nodes::GreaterThanOrEqual.new(self, other.begin)
+ elsif other.begin == -Float::INFINITY && other.exclude_end?
+ Nodes::LessThan.new(self, other.end)
+ elsif other.begin == -Float::INFINITY
+ Nodes::LessThanOrEqual.new(self, other.end)
+ elsif other.exclude_end?
left = Nodes::GreaterThanOrEqual.new(self, other.begin)
right = Nodes::LessThan.new(self, other.end)
Nodes::And.new [left, right]
@@ -54,7 +62,15 @@ module Arel
when Arel::SelectManager
Arel::Nodes::NotIn.new(self, other.ast)
when Range
- if other.exclude_end?
+ if other.begin == -Float::INFINITY && other.end == Float::INFINITY
+ Nodes::In.new self, []
+ elsif other.end == Float::INFINITY
+ Nodes::LessThan.new(self, other.begin)
+ elsif other.begin == -Float::INFINITY && other.exclude_end?
+ Nodes::GreaterThanOrEqual.new(self, other.end)
+ elsif other.begin == -Float::INFINITY
+ Nodes::GreaterThan.new(self, other.end)
+ elsif other.exclude_end?
left = Nodes::LessThan.new(self, other.begin)
right = Nodes::GreaterThanOrEqual.new(self, other.end)
Nodes::Or.new left, right
diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb
index 08cf4566d1..d3e4dae963 100644
--- a/test/visitors/test_to_sql.rb
+++ b/test/visitors/test_to_sql.rb
@@ -230,6 +230,23 @@ module Arel
}
end
+ it 'can handle ranges bounded by infinity' do
+ node = @attr.in 1..Float::INFINITY
+ @visitor.accept(node).must_be_like %{
+ "users"."id" >= 1
+ }
+ node = @attr.in(-Float::INFINITY..3)
+ @visitor.accept(node).must_be_like %{
+ "users"."id" <= 3
+ }
+ node = @attr.in(-Float::INFINITY...3)
+ @visitor.accept(node).must_be_like %{
+ "users"."id" < 3
+ }
+ node = @attr.in(-Float::INFINITY..Float::INFINITY)
+ @visitor.accept(node).must_be_like %{1=1}
+ end
+
it 'can handle subqueries' do
table = Table.new(:users)
subquery = table.project(:id).where(table[:name].eq('Aaron'))
@@ -316,6 +333,23 @@ module Arel
}
end
+ it 'can handle ranges bounded by infinity' do
+ node = @attr.not_in 1..Float::INFINITY
+ @visitor.accept(node).must_be_like %{
+ "users"."id" < 1
+ }
+ node = @attr.not_in(-Float::INFINITY..3)
+ @visitor.accept(node).must_be_like %{
+ "users"."id" > 3
+ }
+ node = @attr.not_in(-Float::INFINITY...3)
+ @visitor.accept(node).must_be_like %{
+ "users"."id" >= 3
+ }
+ node = @attr.not_in(-Float::INFINITY..Float::INFINITY)
+ @visitor.accept(node).must_be_like %{1=0}
+ end
+
it 'can handle subqueries' do
table = Table.new(:users)
subquery = table.project(:id).where(table[:name].eq('Aaron'))