diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-10-25 07:38:56 -0500 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-10-25 07:38:56 -0500 |
commit | f8d85cf24bca522a04edc5cc48f8716e65efb107 (patch) | |
tree | 400a9d564be166d15cb6566dd78cd251f8aeaef5 /lib/arel | |
parent | df5723dfbe856abc8c91cc87c609156b432e97d3 (diff) | |
download | rails-f8d85cf24bca522a04edc5cc48f8716e65efb107.tar.gz rails-f8d85cf24bca522a04edc5cc48f8716e65efb107.tar.bz2 rails-f8d85cf24bca522a04edc5cc48f8716e65efb107.zip |
Deprecate passing ranges to `#in` and `#not_in`
The goal of these methods should be to generate in nodes, not handle
every possible permutation of more than one value. The `#between` and
`#not_between` methods have been extracted, which better represent the
semantics of handling ranges in SQL.
Diffstat (limited to 'lib/arel')
-rw-r--r-- | lib/arel/predications.rb | 84 |
1 files changed, 49 insertions, 35 deletions
diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb index 4079b4830e..a0b6728943 100644 --- a/lib/arel/predications.rb +++ b/lib/arel/predications.rb @@ -25,9 +25,23 @@ module Arel 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)) + if other.begin == -Float::INFINITY + if other.end == Float::INFINITY + not_in([]) + elsif other.exclude_end? + lt(other.end) + else + lteq(other.end) + end + elsif other.end == Float::INFINITY + gteq(other.begin) + elsif other.exclude_end? + gteq(other.begin).and(lt(other.end)) + else + left = Nodes.build_quoted(other.begin, self) + right = Nodes.build_quoted(other.end, self) + Nodes::Between.new(self, left.and(right)) + end end def in other @@ -35,21 +49,12 @@ module Arel when Arel::SelectManager Arel::Nodes::In.new(self, other.ast) when Range - if other.begin == -Float::INFINITY - if other.end == Float::INFINITY - not_in([]) - elsif other.exclude_end? - lt(other.end) - else - lteq(other.end) - end - elsif other.end == Float::INFINITY - gteq(other.begin) - elsif other.exclude_end? - gteq(other.begin).and(lt(other.end)) - else - between(other) + if $VERBOSE + warn <<-eowarn +Passing a range to `#in` is deprecated. Call `#between`, instead. + eowarn end + between(other) when Array Nodes::In.new self, other.map { |x| Nodes.build_quoted(x, self) } else @@ -65,30 +70,39 @@ module Arel grouping_all :in, others end + def not_between other + if other.begin == -Float::INFINITY # The range begins with negative infinity + if other.end == Float::INFINITY + self.in([]) + elsif other.exclude_end? + gteq(other.end) + else + gt(other.end) + end + elsif other.end == Float::INFINITY + lt(other.begin) + else + left = lt(other.begin) + right = if other.exclude_end? + gteq(other.end) + else + gt(other.end) + end + left.or(right) + end + end + def not_in other case other when Arel::SelectManager Arel::Nodes::NotIn.new(self, other.ast) when Range - if other.begin == -Float::INFINITY # The range begins with negative infinity - if other.end == Float::INFINITY - self.in([]) - elsif other.exclude_end? - gteq(other.end) - else - gt(other.end) - end - elsif other.end == Float::INFINITY - lt(other.begin) - else - left = lt(other.begin) - right = if other.exclude_end? - gteq(other.end) - else - gt(other.end) - end - left.or(right) + if $VERBOSE + warn <<-eowarn +Passing a range to `#not_in` is deprecated. Call `#not_between`, instead. + eowarn end + not_between(other) when Array Nodes::NotIn.new self, other.map { |x| Nodes.build_quoted(x, self) } else |