Skip to content
Browse files

fixing a bug where the foreign type field of a polymorphic associatio…

…n was being incorrectly set
  • Loading branch information...
1 parent 94d2c03 commit 9ba92048abc2fcc00bc70474b84839b1d5c88578 @fhwang committed Mar 19, 2012
Showing with 16 additions and 2 deletions.
  1. +2 −1 lib/sample_models/creation.rb
  2. +8 −0 lib/sample_models/model.rb
  3. +6 −1 test/unit/polymorphic_belongs_to_test.rb
View
3 lib/sample_models/creation.rb
@@ -9,7 +9,8 @@ def initialize(sampler, *args)
def belongs_to_assoc(column_name)
model.belongs_to_associations.detect { |assoc|
- assoc.foreign_key == column_name
+ assoc.foreign_key == column_name ||
+ assoc.foreign_type == column_name
}
end
View
8 lib/sample_models/model.rb
@@ -72,6 +72,14 @@ def foreign_key
@assoc.primary_key_name
end
end
+
+ def foreign_type
+ if @assoc.respond_to?(:foreign_type)
+ @assoc.foreign_type
+ else
+ @assoc.instance_variable_get(:@options)[:foreign_type]
+ end
+ end
def has_many?
@assoc.macro == :has_many
View
7 test/unit/polymorphic_belongs_to_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + "/../test_helper"
+require File.expand_path(File.join(File.dirname(__FILE__), '/../test_helper'))
class PolymorphicBelongsToTest < SampleModelsTestCase
def test_fills_association_with_anything_from_another_class
@@ -12,6 +12,11 @@ def test_can_specify_association
blog_post = BlogPost.sample
bookmark = Bookmark.sample :bookmarkable => blog_post
assert_equal blog_post, bookmark.bookmarkable
+ assert(
+ Bookmark.all(
+ :conditions => {:bookmarkable_type => 'BlogPost'}
+ ).include?(bookmark)
+ )
end
def test_can_specify_association_with_other_leading_associations

0 comments on commit 9ba9204

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