Skip to content

Commit

Permalink
Support beginless ranges in hash conditions.
Browse files Browse the repository at this point in the history
Ruby 2.7 introduces beginless ranges (..value and ...value) and as with
endless ranges we can turn these into inequalities, enabling expressions
such as

    Order.where(created_at: ..1.year.ago)
    User.where(karma: ...0)
  • Loading branch information
inopinatus committed Jul 17, 2019
1 parent 32e5f90 commit b191504
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Add support for beginless ranges, introduced in Ruby 2.7.

*Josh Goodall*

* Add database_exists? method to connection adapters to check if a database exists.

*Guilherme Mansur*
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/arel/predications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def eq_all(others)
def between(other)
if unboundable?(other.begin) == 1 || unboundable?(other.end) == -1
self.in([])
elsif open_ended?(other.begin)
elsif other.begin.nil? || open_ended?(other.begin)
if other.end.nil? || open_ended?(other.end)
not_in([])
elsif other.exclude_end?
Expand Down Expand Up @@ -85,7 +85,7 @@ def in_all(others)
def not_between(other)
if unboundable?(other.begin) == 1 || unboundable?(other.end) == -1
not_in([])
elsif open_ended?(other.begin)
elsif other.begin.nil? || open_ended?(other.begin)
if other.end.nil? || open_ended?(other.end)
self.in([])
elsif other.exclude_end?
Expand Down
24 changes: 24 additions & 0 deletions activerecord/test/cases/arel/attributes/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,18 @@ class AttributeTest < Arel::Spec
)
end

if Gem::Version.new("2.7.0") <= Gem::Version.new(RUBY_VERSION)
it "can be constructed with a range implicitly starting at Infinity" do
attribute = Attribute.new nil, nil
node = attribute.between(eval("..0")) # eval for backwards compatibility

node.must_equal Nodes::LessThanOrEqual.new(
attribute,
Nodes::Casted.new(0, attribute)
)
end
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
Expand Down Expand Up @@ -839,6 +851,18 @@ class AttributeTest < Arel::Spec
)
end

if Gem::Version.new("2.7.0") <= Gem::Version.new(RUBY_VERSION)
it "can be constructed with a range implicitly starting at Infinity" do
attribute = Attribute.new nil, nil
node = attribute.not_between(eval("..0")) # eval for backwards compatibility

node.must_equal Nodes::GreaterThan.new(
attribute,
Nodes::Casted.new(0, attribute)
)
end
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
Expand Down

0 comments on commit b191504

Please sign in to comment.