diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-25 15:27:43 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-25 16:31:21 -0700 |
commit | def2879d7d187df945d77c1028d4cef588cbc8a0 (patch) | |
tree | 29325310fe304bbfe52a6b2811b803b8a1c1ae6a /activerecord/test/cases/relation | |
parent | 2c46d6db4feaf4284415f2fb6ceceb1bb535f278 (diff) | |
download | rails-def2879d7d187df945d77c1028d4cef588cbc8a0.tar.gz rails-def2879d7d187df945d77c1028d4cef588cbc8a0.tar.bz2 rails-def2879d7d187df945d77c1028d4cef588cbc8a0.zip |
Move where merging logic over to `WhereClause`
This object being a black box, it knows the details of how to merge
itself with another where clause. This removes all references to where
values or bind values in `Relation::Merger`
Diffstat (limited to 'activerecord/test/cases/relation')
-rw-r--r-- | activerecord/test/cases/relation/where_clause_test.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/test/cases/relation/where_clause_test.rb b/activerecord/test/cases/relation/where_clause_test.rb index 66e2f3ab48..ec334240cc 100644 --- a/activerecord/test/cases/relation/where_clause_test.rb +++ b/activerecord/test/cases/relation/where_clause_test.rb @@ -28,6 +28,44 @@ class ActiveRecord::Relation assert_equal clause, clause + WhereClause.empty end + test "merge combines two where clauses" do + a = WhereClause.new([table["id"].eq(1)], []) + b = WhereClause.new([table["name"].eq("Sean")], []) + expected = WhereClause.new([table["id"].eq(1), table["name"].eq("Sean")], []) + + assert_equal expected, a.merge(b) + end + + test "merge keeps the right side, when two equality clauses reference the same column" do + a = WhereClause.new([table["id"].eq(1), table["name"].eq("Sean")], []) + b = WhereClause.new([table["name"].eq("Jim")], []) + expected = WhereClause.new([table["id"].eq(1), table["name"].eq("Jim")], []) + + assert_equal expected, a.merge(b) + end + + test "merge removes bind parameters matching overlapping equality clauses" do + a = WhereClause.new( + [table["id"].eq(bind_param), table["name"].eq(bind_param)], + [[column("id"), 1], [column("name"), "Sean"]], + ) + b = WhereClause.new( + [table["name"].eq(bind_param)], + [[column("name"), "Jim"]] + ) + expected = WhereClause.new( + [table["id"].eq(bind_param), table["name"].eq(bind_param)], + [[column("id"), 1], [column("name"), "Jim"]], + ) + + assert_equal expected, a.merge(b) + end + + test "merge allows for columns with the same name from different tables" do + skip "This is not possible as of 4.2, and the binds do not yet contain sufficient information for this to happen" + # We might be able to change the implementation to remove conflicts by index, rather than column name + end + private def table @@ -37,5 +75,9 @@ class ActiveRecord::Relation def bind_param Arel::Nodes::BindParam.new end + + def column(name) + ActiveRecord::ConnectionAdapters::Column.new(name, nil, nil) + end end end |