Skip to content
This repository
Browse code

Remove unintentional API changes. [#1108]

  • Loading branch information...
commit d5921cdb7a00b393d0ba5f2f795da60a33b11cf1 1 parent 4a06489
Pratik authored July 14, 2010
13  activerecord/lib/active_record/associations/association_collection.rb
@@ -418,15 +418,10 @@ def load_target
418 418
         end
419 419
 
420 420
         def method_missing(method, *args)
421  
-          case method.to_s
422  
-          when 'find_or_create'
423  
-            return find(:first, :conditions => args.first) || create(args.first)
424  
-          when /^find_or_create_by_(.*)$/
425  
-            rest = $1
426  
-            return  send("find_by_#{rest}", *args) ||
427  
-                    method_missing("create_by_#{rest}", *args)
428  
-          when /^create_by_(.*)$/
429  
-            return create Hash[$1.split('_and_').zip(args)]
  421
+          match = DynamicFinderMatch.match(method)
  422
+          if match && match.creator?
  423
+            attributes = match.attribute_names
  424
+            return send(:"find_by_#{attributes.join('and')}", *args) || create(Hash[attributes.zip(args)])
430 425
           end
431 426
 
432 427
           if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
4  activerecord/lib/active_record/dynamic_finder_match.rb
@@ -42,6 +42,10 @@ def instantiator?
42 42
       @finder == :first && !@instantiator.nil?
43 43
     end
44 44
 
  45
+    def creator?
  46
+      @finder == :first && @instantiator == :create
  47
+    end
  48
+
45 49
     def bang?
46 50
       @bang
47 51
     end
50  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -47,14 +47,14 @@ def setup
47 47
     Client.destroyed_client_ids.clear
48 48
   end
49 49
 
50  
-  def test_create_by
51  
-    person = Person.create! :first_name => 'tenderlove'
52  
-    post   = Post.find :first
  50
+  def test_create_resets_cached_counters
  51
+    person = Person.create!(:first_name => 'tenderlove')
  52
+    post   = Post.first
53 53
 
54 54
     assert_equal [], person.readers
55  
-    assert_nil person.readers.find_by_post_id post.id
  55
+    assert_nil person.readers.find_by_post_id(post.id)
56 56
 
57  
-    reader = person.readers.create_by_post_id post.id
  57
+    reader = person.readers.create(:post_id => post.id)
58 58
 
59 59
     assert_equal 1, person.readers.count
60 60
     assert_equal 1, person.readers.length
@@ -62,13 +62,14 @@ def test_create_by
62 62
     assert_equal person, person.readers.first.person
63 63
   end
64 64
 
65  
-  def test_create_by_multi
  65
+  def test_find_or_create_by_resets_cached_counters
66 66
     person = Person.create! :first_name => 'tenderlove'
67  
-    post   = Post.find :first
  67
+    post   = Post.first
68 68
 
69 69
     assert_equal [], person.readers
  70
+    assert_nil person.readers.find_by_post_id(post.id)
70 71
 
71  
-    reader = person.readers.create_by_post_id_and_skimmer post.id, false
  72
+    reader = person.readers.find_or_create_by_post_id(post.id)
72 73
 
73 74
     assert_equal 1, person.readers.count
74 75
     assert_equal 1, person.readers.length
@@ -76,39 +77,6 @@ def test_create_by_multi
76 77
     assert_equal person, person.readers.first.person
77 78
   end
78 79
 
79  
-  def test_find_or_create_by
80  
-    person = Person.create! :first_name => 'tenderlove'
81  
-    post   = Post.find :first
82  
-
83  
-    assert_equal [], person.readers
84  
-    assert_nil person.readers.find_by_post_id post.id
85  
-
86  
-    reader = person.readers.find_or_create_by_post_id post.id
87  
-
88  
-    assert_equal 1, person.readers.count
89  
-    assert_equal 1, person.readers.length
90  
-    assert_equal post, person.readers.first.post
91  
-    assert_equal person, person.readers.first.person
92  
-  end
93  
-
94  
-  def test_find_or_create
95  
-    person = Person.create! :first_name => 'tenderlove'
96  
-    post   = Post.find :first
97  
-
98  
-    assert_equal [], person.readers
99  
-    assert_nil person.readers.find(:first, :conditions => {
100  
-      :post_id => post.id
101  
-    })
102  
-
103  
-    reader = person.readers.find_or_create :post_id => post.id
104  
-
105  
-    assert_equal 1, person.readers.count
106  
-    assert_equal 1, person.readers.length
107  
-    assert_equal post, person.readers.first.post
108  
-    assert_equal person, person.readers.first.person
109  
-  end
110  
-
111  
-
112 80
   def force_signal37_to_load_all_clients_of_firm
113 81
     companies(:first_firm).clients_of_firm.each {|f| }
114 82
   end

0 notes on commit d5921cd

Please sign in to comment.
Something went wrong with that request. Please try again.