diff options
author | Greg Navis <contact@gregnavis.com> | 2019-01-09 18:17:40 +0100 |
---|---|---|
committer | Greg Navis <contact@gregnavis.com> | 2019-01-11 14:27:34 +0100 |
commit | 7110dbea008dd4b80d1764003935a3c97ab10f57 (patch) | |
tree | 5bffd50bc1a8126167fcfd650bcc84e88789442f /activerecord | |
parent | ea65d92f1924648e72f93bb0e8e5fc62a56d0bac (diff) | |
download | rails-7110dbea008dd4b80d1764003935a3c97ab10f57.tar.gz rails-7110dbea008dd4b80d1764003935a3c97ab10f57.tar.bz2 rails-7110dbea008dd4b80d1764003935a3c97ab10f57.zip |
Support endless ranges in where
This commit adds support for endless ranges, e.g. (1..), that were added
in Ruby 2.6. They're functionally equivalent to explicitly specifying
Float::INFINITY as the end of the range.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/arel/predications.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/arel/attributes/attribute_test.rb | 12 |
3 files changed, 18 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index ca072be5e1..e468cc3169 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add support for endless ranges introduces in Ruby 2.6. + + *Greg Navis* + * Deprecate passing `migrations_paths` to `connection.assume_migrated_upto_version`. *Ryuta Kamizono* diff --git a/activerecord/lib/arel/predications.rb b/activerecord/lib/arel/predications.rb index 28679ae892..0c03e93138 100644 --- a/activerecord/lib/arel/predications.rb +++ b/activerecord/lib/arel/predications.rb @@ -36,14 +36,14 @@ module Arel # :nodoc: all def between(other) if infinity?(other.begin) - if infinity?(other.end) + if other.end.nil? || infinity?(other.end) not_in([]) elsif other.exclude_end? lt(other.end) else lteq(other.end) end - elsif infinity?(other.end) + elsif other.end.nil? || infinity?(other.end) gteq(other.begin) elsif other.exclude_end? gteq(other.begin).and(lt(other.end)) diff --git a/activerecord/test/cases/arel/attributes/attribute_test.rb b/activerecord/test/cases/arel/attributes/attribute_test.rb index 671e273543..6ad54ab5df 100644 --- a/activerecord/test/cases/arel/attributes/attribute_test.rb +++ b/activerecord/test/cases/arel/attributes/attribute_test.rb @@ -639,6 +639,18 @@ module Arel ) end + if Gem::Version.new("2.6.0") <= Gem::Version.new(RUBY_VERSION) + it "can be constructed with a range implicitly ending at Infinity" do + attribute = Attribute.new nil, nil + node = attribute.between(eval("0..")) # Use eval for compatibility with Ruby < 2.6 parser + + node.must_equal Nodes::GreaterThanOrEqual.new( + attribute, + Nodes::Casted.new(0, attribute) + ) + end + end + it "can be constructed with a quoted range ending at Infinity" do attribute = Attribute.new nil, nil node = attribute.between(quoted_range(0, ::Float::INFINITY, false)) |