aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/predications.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-10-25 07:24:18 -0500
committerSean Griffin <sean@thoughtbot.com>2014-10-25 07:24:18 -0500
commitdf5723dfbe856abc8c91cc87c609156b432e97d3 (patch)
treeda1f068d958c096e6867bc2402d8201a641d2d96 /lib/arel/predications.rb
parent438d553b76f59eed003c8e4bb569936e68e0e7c2 (diff)
downloadrails-df5723dfbe856abc8c91cc87c609156b432e97d3.tar.gz
rails-df5723dfbe856abc8c91cc87c609156b432e97d3.tar.bz2
rails-df5723dfbe856abc8c91cc87c609156b432e97d3.zip
Refactor `#in` and `#not_in`
These methods duplicate a lot of logic from the other predications. We can just use those methods directly, and only build nodes with the same name in our method directly. We've already had one bug that came from building nodes directly, rather than using the proper predicate.
Diffstat (limited to 'lib/arel/predications.rb')
-rw-r--r--lib/arel/predications.rb36
1 files changed, 20 insertions, 16 deletions
diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb
index 6f9a3c5f91..4079b4830e 100644
--- a/lib/arel/predications.rb
+++ b/lib/arel/predications.rb
@@ -24,6 +24,12 @@ module Arel
grouping_all :eq, others.map { |x| Nodes.build_quoted(x, self) }
end
+ def between other
+ left = Nodes.build_quoted(other.begin, self)
+ right = Nodes.build_quoted(other.end, self)
+ Nodes::Between.new(self, left.and(right))
+ end
+
def in other
case other
when Arel::SelectManager
@@ -31,20 +37,18 @@ module Arel
when Range
if other.begin == -Float::INFINITY
if other.end == Float::INFINITY
- Nodes::NotIn.new self, []
+ not_in([])
elsif other.exclude_end?
- Nodes::LessThan.new(self, Nodes.build_quoted(other.end, self))
+ lt(other.end)
else
- Nodes::LessThanOrEqual.new(self, Nodes.build_quoted(other.end, self))
+ lteq(other.end)
end
elsif other.end == Float::INFINITY
- Nodes::GreaterThanOrEqual.new(self, Nodes.build_quoted(other.begin, self))
+ gteq(other.begin)
elsif other.exclude_end?
- left = Nodes::GreaterThanOrEqual.new(self, Nodes.build_quoted(other.begin, self))
- right = Nodes::LessThan.new(self, Nodes.build_quoted(other.end, self))
- Nodes::And.new [left, right]
+ gteq(other.begin).and(lt(other.end))
else
- Nodes::Between.new(self, Nodes::And.new([Nodes.build_quoted(other.begin, self), Nodes.build_quoted(other.end, self)]))
+ between(other)
end
when Array
Nodes::In.new self, other.map { |x| Nodes.build_quoted(x, self) }
@@ -68,20 +72,20 @@ module Arel
when Range
if other.begin == -Float::INFINITY # The range begins with negative infinity
if other.end == Float::INFINITY
- Nodes::In.new self, [] # The range is infinite, so return an empty range
+ self.in([])
elsif other.exclude_end?
- Nodes::GreaterThanOrEqual.new(self, Nodes.build_quoted(other.end, self))
+ gteq(other.end)
else
- Nodes::GreaterThan.new(self, Nodes.build_quoted(other.end, self))
+ gt(other.end)
end
elsif other.end == Float::INFINITY
- Nodes::LessThan.new(self, Nodes.build_quoted(other.begin, self))
+ lt(other.begin)
else
- left = Nodes::LessThan.new(self, Nodes.build_quoted(other.begin, self))
- if other.exclude_end?
- right = Nodes::GreaterThanOrEqual.new(self, Nodes.build_quoted(other.end, self))
+ left = lt(other.begin)
+ right = if other.exclude_end?
+ gteq(other.end)
else
- right = Nodes::GreaterThan.new(self, Nodes.build_quoted(other.end, self))
+ gt(other.end)
end
left.or(right)
end