Skip to content
This repository
Browse code

Deprecate Relation#& alias for Relation#merge

  • Loading branch information...
commit 66003f596452aba927312c4218dfc8d408166d54 1 parent 22d1fee
Ernie Miller authored February 10, 2011 tenderlove committed February 12, 2011
5  activerecord/lib/active_record/relation/spawn_methods.rb
@@ -67,7 +67,10 @@ def merge(r)
67 67
       merged_relation
68 68
     end
69 69
 
70  
-    alias :& :merge
  70
+    def &(r)
  71
+      ActiveSupport::Deprecation.warn "Using & to merge relations has been deprecated and will be removed in Rails 3.1. Please use the relation's merge method, instead"
  72
+      merge(r)
  73
+    end
71 74
 
72 75
     def except(*skips)
73 76
       result = self.class.new(@klass, table)
2  activerecord/test/cases/method_scoping_test.rb
@@ -229,7 +229,7 @@ def test_scoped_create
229 229
   end
230 230
 
231 231
   def test_scoped_create_with_join_and_merge
232  
-    (Comment.where(:body => "but Who's Buying?").joins(:post) & Post.where(:body => 'Peace Sells...')).with_scope do
  232
+    Comment.where(:body => "but Who's Buying?").joins(:post).merge(Post.where(:body => 'Peace Sells...')).with_scope do
233 233
       assert_equal({:body => "but Who's Buying?"}, Comment.scoped.scope_for_create)
234 234
     end
235 235
   end
18  activerecord/test/cases/relations_test.rb
@@ -536,17 +536,21 @@ def test_select_argument_error
536 536
   end
537 537
 
538 538
   def test_relation_merging
539  
-    devs = Developer.where("salary >= 80000") & Developer.limit(2) & Developer.order('id ASC').where("id < 3")
  539
+    devs = Developer.where("salary >= 80000").merge(Developer.limit(2)).merge(Developer.order('id ASC').where("id < 3"))
540 540
     assert_equal [developers(:david), developers(:jamis)], devs.to_a
541 541
 
542  
-    dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*')
  542
+    dev_with_count = Developer.limit(1).merge(Developer.order('id DESC')).merge(Developer.select('developers.*'))
543 543
     assert_equal [developers(:poor_jamis)], dev_with_count.to_a
544 544
   end
545 545
 
  546
+  def test_relation_ampersand_merge_deprecation
  547
+    assert_deprecated { Developer.where("salary >= 80000") & Developer.limit(2) }
  548
+  end
  549
+
546 550
   def test_relation_merging_with_eager_load
547 551
     relations = []
548  
-    relations << (Post.order('comments.id DESC') & Post.eager_load(:last_comment) & Post.scoped)
549  
-    relations << (Post.eager_load(:last_comment) & Post.order('comments.id DESC') & Post.scoped)
  552
+    relations << Post.order('comments.id DESC').merge(Post.eager_load(:last_comment)).merge(Post.scoped)
  553
+    relations << Post.eager_load(:last_comment).merge(Post.order('comments.id DESC')).merge(Post.scoped)
550 554
 
551 555
     relations.each do |posts|
552 556
       post = posts.find { |p| p.id == 1 }
@@ -555,18 +559,18 @@ def test_relation_merging_with_eager_load
555 559
   end
556 560
 
557 561
   def test_relation_merging_with_locks
558  
-    devs = Developer.lock.where("salary >= 80000").order("id DESC") & Developer.limit(2)
  562
+    devs = Developer.lock.where("salary >= 80000").order("id DESC").merge(Developer.limit(2))
559 563
     assert_present devs.locked
560 564
   end
561 565
 
562 566
   def test_relation_merging_with_preload
563  
-    [Post.scoped & Post.preload(:author), Post.preload(:author) & Post.scoped].each do |posts|
  567
+    [Post.scoped.merge(Post.preload(:author)), Post.preload(:author).merge(Post.scoped)].each do |posts|
564 568
       assert_queries(2) { assert posts.first.author }
565 569
     end
566 570
   end
567 571
 
568 572
   def test_relation_merging_with_joins
569  
-    comments = Comment.joins(:post).where(:body => 'Thank you for the welcome') & Post.where(:body => 'Such a lovely day')
  573
+    comments = Comment.joins(:post).where(:body => 'Thank you for the welcome').merge(Post.where(:body => 'Such a lovely day'))
570 574
     assert_equal 1, comments.count
571 575
   end
572 576
 

2 notes on commit 66003f5

Alexey Trofimenko

why? it couldn't hurt anyone, and looked rather nice

Ernie Miller
ernie commented on 66003f5 May 20, 2011
Please sign in to comment.
Something went wrong with that request. Please try again.