From 2c008d9f6311d92b3660193285230505e74a114f Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 10 Nov 2019 10:54:58 +0900 Subject: [PATCH] `:polymorphic`, `:as`, and `:foreign_type` are valid for polymorphic association --- .../lib/active_record/associations/builder/belongs_to.rb | 4 +++- .../lib/active_record/associations/builder/has_many.rb | 3 ++- .../lib/active_record/associations/builder/has_one.rb | 3 ++- .../associations/builder/singular_association.rb | 2 +- .../test/cases/associations/has_many_associations_test.rb | 1 + .../test/cases/associations/has_one_associations_test.rb | 1 + activerecord/test/models/image.rb | 2 +- 7 files changed, 11 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 0d122e40fb..eb6787f829 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -7,7 +7,9 @@ module ActiveRecord::Associations::Builder # :nodoc: end def self.valid_options(options) - super + [:polymorphic, :counter_cache, :optional, :default] + valid = super + [:counter_cache, :optional, :default] + valid += [:polymorphic, :foreign_type] if options[:polymorphic] + valid end def self.valid_dependent_options diff --git a/activerecord/lib/active_record/associations/builder/has_many.rb b/activerecord/lib/active_record/associations/builder/has_many.rb index b3bad2d3fe..1f3877c52e 100644 --- a/activerecord/lib/active_record/associations/builder/has_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_many.rb @@ -7,7 +7,8 @@ module ActiveRecord::Associations::Builder # :nodoc: end def self.valid_options(options) - valid = super + [:as, :counter_cache, :join_table, :foreign_type, :index_errors] + valid = super + [:counter_cache, :join_table, :index_errors] + valid += [:as, :foreign_type] if options[:as] valid += [:through, :source, :source_type] if options[:through] valid end diff --git a/activerecord/lib/active_record/associations/builder/has_one.rb b/activerecord/lib/active_record/associations/builder/has_one.rb index d7f04c91c8..715035de9f 100644 --- a/activerecord/lib/active_record/associations/builder/has_one.rb +++ b/activerecord/lib/active_record/associations/builder/has_one.rb @@ -7,7 +7,8 @@ module ActiveRecord::Associations::Builder # :nodoc: end def self.valid_options(options) - valid = super + [:as] + valid = super + valid += [:as, :foreign_type] if options[:as] valid += [:through, :source, :source_type] if options[:through] valid end diff --git a/activerecord/lib/active_record/associations/builder/singular_association.rb b/activerecord/lib/active_record/associations/builder/singular_association.rb index 2fd0483403..7537aa468f 100644 --- a/activerecord/lib/active_record/associations/builder/singular_association.rb +++ b/activerecord/lib/active_record/associations/builder/singular_association.rb @@ -5,7 +5,7 @@ module ActiveRecord::Associations::Builder # :nodoc: class SingularAssociation < Association #:nodoc: def self.valid_options(options) - super + [:foreign_type, :required, :touch] + super + [:required, :touch] end def self.define_accessors(model, reflection) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 01ec5efed9..0e6ce32431 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -2439,6 +2439,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase post.images << image assert_equal [image], post.images + assert_equal post, image.imageable end def test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 6c2a09c296..40746f76cb 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -691,6 +691,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase post.reload assert_equal image, post.main_image + assert_equal post, image.imageable end test "dangerous association name raises ArgumentError" do diff --git a/activerecord/test/models/image.rb b/activerecord/test/models/image.rb index b4808293cc..71bbcacd3a 100644 --- a/activerecord/test/models/image.rb +++ b/activerecord/test/models/image.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Image < ActiveRecord::Base - belongs_to :imageable, foreign_key: :imageable_identifier, foreign_type: :imageable_class + belongs_to :imageable, polymorphic: true, foreign_key: :imageable_identifier, foreign_type: :imageable_class end