From 291adbd36181ab6766ea2d8f8c0f806c5d8ca422 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Thu, 20 Jul 2006 00:34:09 +0000 Subject: fix association exception messages, fix them so the error messages actually display git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4615 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 51 ++++++---------------- .../associations/has_many_through_association.rb | 2 +- activerecord/lib/active_record/reflection.rb | 4 +- 3 files changed, 16 insertions(+), 41 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 722f78596c..5eb6495b9d 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -10,69 +10,44 @@ require 'active_record/deprecated_associations' module ActiveRecord class HasManyThroughAssociationNotFoundError < ActiveRecordError #:nodoc: - def initialize(reflection) - @reflection = reflection - end - - def message - "Could not find the association #{@reflection.options[:through].inspect} in model #{@reflection.klass}" + def initialize(owner_class_name, reflection) + super("Could not find the association #{reflection.options[:through].inspect} in model #{owner_class_name}") end end class HasManyThroughAssociationPolymorphicError < ActiveRecordError #:nodoc: def initialize(owner_class_name, reflection, source_reflection) - @owner_class_name = owner_class_name - @reflection = reflection - @source_reflection = source_reflection - end - - def message - "Cannot have a has_many :through association '#{@owner_class_name}##{@reflection.name}' on the polymorphic object '#{@source_reflection.class_name}##{@source_reflection.name}'." + source_reflection = source_reflection + super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' on the polymorphic object '#{source_reflection.class_name}##{source_reflection.name}'.") end end class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError #:nodoc: def initialize(reflection) - @reflection = reflection - @through_reflection = reflection.through_reflection - @source_reflection_names = reflection.source_reflection_names - @source_associations = reflection.through_reflection.klass.reflect_on_all_associations.collect { |a| a.name.inspect } - end - - def message - "Could not find the source association(s) #{@source_reflection_names.collect(&:inspect).to_sentence :connector => 'or'} in model #{@through_reflection.klass}. Try 'has_many #{@reflection.name.inspect}, :through => #{@through_reflection.name.inspect}, :source => '. Is it one of #{@source_associations.to_sentence :connector => 'or'}?" + through_reflection = reflection.through_reflection + source_reflection_names = reflection.source_reflection_names + source_associations = reflection.through_reflection.klass.reflect_on_all_associations.collect { |a| a.name.inspect } + super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence :connector => 'or'} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => '. Is it one of #{source_associations.to_sentence :connector => 'or'}?") end end class HasManyThroughSourceAssociationMacroError < ActiveRecordError #:nodoc def initialize(reflection) - @reflection = reflection - @through_reflection = reflection.through_reflection - @source_reflection = reflection.source_reflection - end - - def message - "Invalid source reflection macro :#{@source_reflection.macro}#{" :through" if @source_reflection.options[:through]} for has_many #{@reflection.name.inspect}, :through => #{@through_reflection.name.inspect}. Use :source to specify the source reflection." + through_reflection = reflection.through_reflection + source_reflection = reflection.source_reflection + super("Invalid source reflection macro :#{source_reflection.macro}#{" :through" if source_reflection.options[:through]} for has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}. Use :source to specify the source reflection.") end end class EagerLoadPolymorphicError < ActiveRecordError #:nodoc: def initialize(reflection) - @reflection = reflection - end - - def message - "Can not eagerly load the polymorphic association #{@reflection.name.inspect}" + super("Can not eagerly load the polymorphic association #{reflection.name.inspect}") end end class ReadOnlyAssociation < ActiveRecordError #:nodoc: def initialize(reflection) - @reflection = reflection - end - - def message - "Can not add to a has_many :through association. Try adding to #{@reflection.through_reflection.name.inspect}." + super("Can not add to a has_many :through association. Try adding to #{reflection.through_reflection.name.inspect}.") end end diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 037c375cde..c352a009d6 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -41,7 +41,7 @@ module ActiveRecord end def <<(*args) - raise ActiveRecord::ReadOnlyAssociation, @reflection + raise ActiveRecord::ReadOnlyAssociation.new(@reflection) end [:push, :concat, :create, :build].each do |method| diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index aab5444583..df23898952 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -166,7 +166,7 @@ module ActiveRecord def check_validity! if options[:through] if through_reflection.nil? - raise HasManyThroughAssociationNotFoundError.new(self) + raise HasManyThroughAssociationNotFoundError.new(active_record.name, self) end if source_reflection.nil? @@ -174,7 +174,7 @@ module ActiveRecord end if source_reflection.options[:polymorphic] - raise HasManyThroughAssociationPolymorphicError.new(class_name, self, source_reflection) + raise HasManyThroughAssociationPolymorphicError.new(active_record.name, self, source_reflection) end unless [:belongs_to, :has_many].include?(source_reflection.macro) && source_reflection.options[:through].nil? -- cgit v1.2.3