Aggregate reflection klass works with nested :class_name

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5923 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2007-01-14 12:08:44 +00:00
parent 937eeab103
commit b78458284b
3 changed files with 40 additions and 36 deletions

@ -149,7 +149,7 @@ def foreign_key_present
def raise_on_type_mismatch(record)
unless record.is_a?(@reflection.klass)
raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.class_name} expected, got #{record.class}"
raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.klass} expected, got #{record.class}"
end
end

@ -71,6 +71,7 @@ def reflect_on_association(association)
# those classes. Objects of AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods.
class MacroReflection
attr_reader :active_record
def initialize(macro, name, options, active_record)
@macro, @name, @options, @active_record = macro, name, options, active_record
end
@ -93,30 +94,29 @@ def options
@options
end
# Returns the class for the macro, so "composed_of :balance, :class_name => 'Money'" would return the Money class and
# "has_many :clients" would return the Client class.
def klass() end
# Returns the class for the macro, so "composed_of :balance, :class_name => 'Money'" returns the Money class and
# "has_many :clients" returns the Client class.
def klass
@klass ||= class_name.constantize
end
def class_name
@class_name ||= name_to_class_name(name.id2name)
@class_name ||= options[:class_name] || derive_class_name
end
def ==(other_aggregation)
name == other_aggregation.name && other_aggregation.options && active_record == other_aggregation.active_record
end
private
def derive_class_name
name.to_s.camelize
end
end
# Holds all the meta-data about an aggregation as it was specified in the Active Record class.
class AggregateReflection < MacroReflection #:nodoc:
def klass
@klass ||= Object.const_get(options[:class_name] || class_name)
end
private
def name_to_class_name(name)
name.capitalize.gsub(/_(.)/) { |s| $1.capitalize }
end
end
# Holds all the meta-data about an association as it was specified in the Active Record class.
@ -130,17 +130,9 @@ def table_name
end
def primary_key_name
return @primary_key_name if @primary_key_name
case
when macro == :belongs_to
@primary_key_name = options[:foreign_key] || class_name.foreign_key
when options[:as]
@primary_key_name = options[:foreign_key] || "#{options[:as]}_id"
else
@primary_key_name = options[:foreign_key] || active_record.name.foreign_key
end
@primary_key_name ||= options[:foreign_key] || derive_primary_key_name
end
def association_foreign_key
@association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
end
@ -198,19 +190,24 @@ def check_validity!
end
private
def name_to_class_name(name)
if name =~ /::/
name
def derive_class_name
# get the class_name of the belongs_to association of the through reflection
if through_reflection
source_reflection.class_name
else
if options[:class_name]
options[:class_name]
elsif through_reflection # get the class_name of the belongs_to association of the through reflection
source_reflection.class_name
else
class_name = name.to_s.camelize
class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro)
class_name
end
class_name = name.to_s.camelize
class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro)
class_name
end
end
def derive_primary_key_name
if macro == :belongs_to
class_name.foreign_key
elsif options[:as]
"#{options[:as]}_id"
else
active_record.name.foreign_key
end
end
end

@ -60,6 +60,13 @@ def test_integer_columns
assert_equal :integer, @first.column_for_attribute("id").type
end
def test_reflection_klass_for_nested_class_name
reflection = ActiveRecord::Reflection::MacroReflection.new(nil, nil, { :class_name => 'MyApplication::Business::Company' }, nil)
assert_nothing_raised do
assert_equal MyApplication::Business::Company, reflection.klass
end
end
def test_aggregation_reflection
reflection_for_address = ActiveRecord::Reflection::AggregateReflection.new(
:composed_of, :address, { :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ] }, Customer