From d5af9bb8c04d0ced44ca29ec161245b7a6812cc0 Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Fri, 21 Dec 2012 00:21:02 -0600 Subject: [PATCH] Change class_name to assert on the real class name --- .../active_record/association_matcher.rb | 11 ++++++----- .../active_record/association_matcher_spec.rb | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/shoulda/matchers/active_record/association_matcher.rb b/lib/shoulda/matchers/active_record/association_matcher.rb index 352453bd..7ff0bcf5 100644 --- a/lib/shoulda/matchers/active_record/association_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matcher.rb @@ -4,7 +4,7 @@ module Shoulda # :nodoc: # Ensure that the belongs_to relationship exists. # # Options: - # * :class_name - tests that the association makes use of the class_name option. + # * :class_name - tests that the association resolves to class_name. # * :validate - tests that the association makes use of the validate # option. # @@ -23,7 +23,7 @@ module Shoulda # :nodoc: # * through - association name for has_many :through # * dependent - tests that the association makes use of the # dependent option. - # * :class_name - tests that the association makes use of the class_name option. + # * :class_name - tests that the association resoves to class_name. # * :validate - tests that the association makes use of the validate # option. # @@ -43,7 +43,7 @@ module Shoulda # :nodoc: # Options: # * :dependent - tests that the association makes use of the # dependent option. - # * :class_name - tests that the association makes use of the class_name option. + # * :class_name - tests that the association resolves to class_name. # * :validate - tests that the association makes use of the validate # option. # @@ -58,6 +58,7 @@ module Shoulda # :nodoc: # the join table is in place. # # Options: + # * :class_name - tests that the association resolves to class_name. # * :validate - tests that the association makes use of the validate # option. # @@ -209,10 +210,10 @@ module Shoulda # :nodoc: def class_name_correct? if @options.key?(:class_name) - if @options[:class_name].to_s == reflection.options[:class_name].to_s + if @options[:class_name].to_s == reflection.klass.to_s true else - @missing = "#{@name} should have #{@options[:class_name]} as class_name" + @missing = "#{@name} should resolve to #{@options[:class_name]} for class_name" false end else diff --git a/spec/shoulda/matchers/active_record/association_matcher_spec.rb b/spec/shoulda/matchers/active_record/association_matcher_spec.rb index cc4d00f6..2a84896e 100644 --- a/spec/shoulda/matchers/active_record/association_matcher_spec.rb +++ b/spec/shoulda/matchers/active_record/association_matcher_spec.rb @@ -64,6 +64,10 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do Child.new.should_not belong_to(:parent).conditions(:adopter => true) end + it 'accepts an association without a :class_name option' do + belonging_to_parent.should belong_to(:parent).class_name('Parent') + end + it 'accepts an association with a valid :class_name option' do define_model :tree_parent define_model :child, :parent_id => :integer do @@ -234,6 +238,10 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do Parent.new.should_not have_many(:children).conditions(:adopted => true) end + it 'accepts an association without a :class_name option' do + having_many_children.should have_many(:children).class_name('Child') + end + it 'accepts an association with a valid :class_name option' do define_model :node, :parent_id => :integer define_model :parent do @@ -374,6 +382,10 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do Person.new.should_not have_one(:detail).conditions(:disabled => true) end + it 'accepts an association without a :class_name option' do + having_one_detail.should have_one(:detail).class_name('Detail') + end + it 'accepts an association with a valid :class_name option' do define_model :person_detail, :person_id => :integer define_model :person do @@ -490,6 +502,11 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do Person.new.should_not have_and_belong_to_many(:relatives).conditions(:adopted => true) end + it 'accepts an association without a :class_name option' do + having_and_belonging_to_many_relatives. + should have_and_belong_to_many(:relatives).class_name('Relative') + end + it 'accepts an association with a valid :class_name option' do define_model :person_relative, :adopted => :boolean define_model :person do