Fix `belong_to` used with polymorphic association

For a polymorphic belongs_to association, the association doesn't
actually point to a class, it points to a concept, so we can't check
that the model it points to exists.
This commit is contained in:
Elliot Winkler 2014-04-12 14:49:01 -06:00
parent bb1aa83721
commit c590a60108
4 changed files with 7 additions and 4 deletions

View File

@ -91,7 +91,7 @@ module Shoulda # :nodoc:
class AssociationMatcher # :nodoc:
delegate :reflection, :model_class, :associated_class, :through?,
:join_table, to: :reflector
:join_table, :polymorphic?, to: :reflector
def initialize(macro, name)
@macro = macro
@ -188,7 +188,7 @@ module Shoulda # :nodoc:
@subject = subject
association_exists? &&
macro_correct? &&
class_exists? &&
(polymorphic? || class_exists?) &&
foreign_key_exists? &&
class_name_correct? &&
autosave_correct? &&

View File

@ -15,6 +15,10 @@ module Shoulda
reflection.klass
end
def polymorphic?
reflection.options[:polymorphic]
end
def through?
reflection.options[:through]
end

View File

@ -4,7 +4,7 @@ module Shoulda # :nodoc:
module AssociationMatchers
class ModelReflector
delegate :associated_class, :through?, :join_table,
:association_relation, to: :reflection
:association_relation, :polymorphic?, to: :reflection
def initialize(subject, name)
@subject = subject

View File

@ -30,7 +30,6 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
end
it 'accepts a polymorphic association' do
define_model :parent
define_model :child, parent_type: :string, parent_id: :integer do
belongs_to :parent, polymorphic: true
end