Skip to content
This repository
Browse code

Make polymorphic_inverse_of in Reflection throw an InverseOfAssociati…

…onNotFoundError if the supplied class doesn't have the appropriate association. [#3520 state:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information...
commit ff508640e28914da2b546f6a8c9f215bab201b61 1 parent 6a74ee7
Murray Steele authored December 28, 2009 alloy committed December 28, 2009
4  activerecord/lib/active_record/associations.rb
@@ -3,8 +3,8 @@
3 3
 
4 4
 module ActiveRecord
5 5
   class InverseOfAssociationNotFoundError < ActiveRecordError #:nodoc:
6  
-    def initialize(reflection)
7  
-      super("Could not find the inverse association for #{reflection.name} (#{reflection.options[:inverse_of].inspect} in #{reflection.class_name})")
  6
+    def initialize(reflection, associated_class = nil)
  7
+      super("Could not find the inverse association for #{reflection.name} (#{reflection.options[:inverse_of].inspect} in #{associated_class.nil? ? reflection.class_name : associated_class.name})")
8 8
     end
9 9
   end
10 10
 
10  activerecord/lib/active_record/reflection.rb
@@ -239,16 +239,16 @@ def has_inverse?
239 239
       def inverse_of
240 240
         if has_inverse?
241 241
           @inverse_of ||= klass.reflect_on_association(options[:inverse_of])
242  
-        else
243  
-          nil
244 242
         end
245 243
       end
246 244
 
247 245
       def polymorphic_inverse_of(associated_class)
248 246
         if has_inverse?
249  
-          associated_class.reflect_on_association(options[:inverse_of])
250  
-        else
251  
-          nil
  247
+          if inverse_relationship = associated_class.reflect_on_association(options[:inverse_of])
  248
+            inverse_relationship
  249
+          else
  250
+            raise InverseOfAssociationNotFoundError.new(self, associated_class)
  251
+          end
252 252
         end
253 253
       end
254 254
 
17  activerecord/test/cases/associations/inverse_associations_test.rb
@@ -524,8 +524,21 @@ def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
524 524
     assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
525 525
   end
526 526
 
527  
-  def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
528  
-    assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_man }
  527
+  def test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error
  528
+    # Ideally this would, if only for symmetry's sake with other association types
  529
+    assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_polymorphic_man }
  530
+  end
  531
+
  532
+  def test_trying_to_set_polymorphic_inverses_that_dont_exist_at_all_should_raise_an_error
  533
+    # fails because no class has the correct inverse_of for horrible_polymorphic_man
  534
+    assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_polymorphic_man = Man.first }
  535
+  end
  536
+
  537
+  def test_trying_to_set_polymorphic_inverses_that_dont_exist_on_the_instance_being_set_should_raise_an_error
  538
+    # passes because Man does have the correct inverse_of
  539
+    assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).polymorphic_man = Man.first }
  540
+    # fails because Interest does have the correct inverse_of
  541
+    assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).polymorphic_man = Interest.first }
529 542
   end
530 543
 end
531 544
 
3  activerecord/test/models/face.rb
... ...
@@ -1,6 +1,7 @@
1 1
 class Face < ActiveRecord::Base
2 2
   belongs_to :man, :inverse_of => :face
3 3
   belongs_to :polymorphic_man, :polymorphic => true, :inverse_of => :polymorphic_face
4  
-  # This is a "broken" inverse_of for the purposes of testing
  4
+  # These is a "broken" inverse_of for the purposes of testing
5 5
   belongs_to :horrible_man, :class_name => 'Man', :inverse_of => :horrible_face
  6
+  belongs_to :horrible_polymorphic_man, :polymorphic => true, :inverse_of => :horrible_polymorphic_face
6 7
 end

0 notes on commit ff50864

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