From def2879d7d187df945d77c1028d4cef588cbc8a0 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sun, 25 Jan 2015 15:27:43 -0700 Subject: 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` --- .../test/cases/relation/where_clause_test.rb | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'activerecord/test/cases/relation') 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 -- cgit v1.2.3