Skip to content

Commit

Permalink
Merge pull request #39668 from kamipo/is_distinct_from_is_not_equality
Browse files Browse the repository at this point in the history
`IsDistinctFrom` is not equality node
  • Loading branch information
kamipo committed Jun 19, 2020
2 parents 37a8dd6 + 92c1f14 commit dde9e82
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
16 changes: 16 additions & 0 deletions activerecord/lib/arel/nodes/binary.rb
Expand Up @@ -73,6 +73,22 @@ def invert
end
end

class IsDistinctFrom < Binary
include FetchAttribute

def invert
Arel::Nodes::IsNotDistinctFrom.new(left, right)
end
end

class IsNotDistinctFrom < Binary
include FetchAttribute

def invert
Arel::Nodes::IsDistinctFrom.new(left, right)
end
end

class NotEqual < Binary
include FetchAttribute

Expand Down
22 changes: 2 additions & 20 deletions activerecord/lib/arel/nodes/equality.rb
Expand Up @@ -3,31 +3,13 @@
module Arel # :nodoc: all
module Nodes
class Equality < Arel::Nodes::Binary
include FetchAttribute

def equality?; true; end

def invert
Arel::Nodes::NotEqual.new(left, right)
end

def fetch_attribute
if left.is_a?(Arel::Attributes::Attribute)
yield left
elsif right.is_a?(Arel::Attributes::Attribute)
yield right
end
end
end

class IsDistinctFrom < Equality
def invert
Arel::Nodes::IsNotDistinctFrom.new(left, right)
end
end

class IsNotDistinctFrom < Equality
def invert
Arel::Nodes::IsDistinctFrom.new(left, right)
end
end
end
end
6 changes: 6 additions & 0 deletions activerecord/test/cases/relation_test.rb
Expand Up @@ -50,6 +50,12 @@ def test_extensions
def test_empty_where_values_hash
relation = Relation.new(FakeKlass)
assert_equal({}, relation.where_values_hash)

relation.where!(relation.table[:id].not_eq(10))
assert_equal({}, relation.where_values_hash)

relation.where!(relation.table[:id].is_distinct_from(10))
assert_equal({}, relation.where_values_hash)
end

def test_where_values_hash_with_in_clause
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -1260,6 +1260,10 @@ def test_first_or_create
assert_kind_of Bird, same_parrot
assert_predicate same_parrot, :persisted?
assert_equal parrot, same_parrot

canary = Bird.where(Bird.arel_attribute(:color).is_distinct_from("green")).first_or_create(name: "canary")
assert_equal "canary", canary.name
assert_nil canary.color
end

def test_first_or_create_with_no_parameters
Expand Down Expand Up @@ -1380,6 +1384,10 @@ def test_first_or_initialize
assert_predicate parrot, :new_record?
assert_equal "parrot", parrot.name
assert_equal "green", parrot.color

canary = Bird.where(Bird.arel_attribute(:color).is_distinct_from("green")).first_or_initialize(name: "canary")
assert_equal "canary", canary.name
assert_nil canary.color
end

def test_first_or_initialize_with_no_parameters
Expand Down

0 comments on commit dde9e82

Please sign in to comment.