`:polymorphic`, `:as`, and `:foreign_type` are valid for polymorphic association

This commit is contained in:
Ryuta Kamizono 2019-11-10 10:54:58 +09:00
parent 1286ab6c49
commit 2c008d9f63
7 changed files with 11 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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