aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
diff options
context:
space:
mode:
authorUlisses Almeida + Kassio Borges <anizark+kassioborgesm@gmail.com>2014-12-08 17:43:16 -0200
committerUlisses Almeida + Kassio Borges <anizark+kassioborgesm@gmail.com>2014-12-08 18:13:15 -0200
commit075c81feec87b1e5bfec27ed585c4ab613c2d174 (patch)
tree2fad903219acd89c1437d83c9b7737da3b14292e /activerecord/test/cases/associations
parent7daeb98c769a3968a2ccf48ff593909bf1a9d7b0 (diff)
downloadrails-075c81feec87b1e5bfec27ed585c4ab613c2d174.tar.gz
rails-075c81feec87b1e5bfec27ed585c4ab613c2d174.tar.bz2
rails-075c81feec87b1e5bfec27ed585c4ab613c2d174.zip
Add foreign_type option for polymorphic has_one and has_many.
To be possible to use a custom column name to save/read the polymorphic associated type in a has_many or has_one polymorphic association, now users can use the option :foreign_type to inform in what column the associated object type will be saved.
Diffstat (limited to 'activerecord/test/cases/associations')
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb10
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb11
2 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index c075872617..d3b74aa616 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -7,6 +7,7 @@ require 'models/contract'
require 'models/topic'
require 'models/reply'
require 'models/category'
+require 'models/image'
require 'models/post'
require 'models/author'
require 'models/essay'
@@ -1783,6 +1784,15 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal [tagging], post.taggings
end
+ def test_with_polymorphic_has_many_with_custom_columns_name
+ post = Post.create! :title => 'foo', :body => 'bar'
+ image = Image.create!
+
+ post.images << image
+
+ assert_equal [image], post.images
+ end
+
def test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id
welcome = posts(:welcome)
tagging = welcome.taggings.build(:taggable_id => 99, :taggable_type => 'ShouldNotChange')
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 1a6d25f7d0..a69f7a5262 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -8,6 +8,7 @@ require 'models/pirate'
require 'models/car'
require 'models/bulb'
require 'models/author'
+require 'models/image'
require 'models/post'
class HasOneAssociationsTest < ActiveRecord::TestCase
@@ -573,6 +574,16 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_with_polymorphic_has_one_with_custom_columns_name
+ post = Post.create! :title => 'foo', :body => 'bar'
+ image = Image.create!
+
+ post.main_image = image
+ post.reload
+
+ assert_equal image, post.main_image
+ end
+
test 'dangerous association name raises ArgumentError' do
[:errors, 'errors', :save, 'save'].each do |name|
assert_raises(ArgumentError, "Association #{name} should not be allowed") do