aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-01-14 12:08:44 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-01-14 12:08:44 +0000
commitb78458284bf0c7d5158181fd4d7e0a507f77edca (patch)
treed9dc51ddd9a55d1c451638ce30c63431803e7221 /activerecord
parent937eeab103a14bb4db61ec1841ad57e01636bf21 (diff)
downloadrails-b78458284bf0c7d5158181fd4d7e0a507f77edca.tar.gz
rails-b78458284bf0c7d5158181fd4d7e0a507f77edca.tar.bz2
rails-b78458284bf0c7d5158181fd4d7e0a507f77edca.zip
Aggregate reflection klass works with nested :class_name
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5923 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb2
-rw-r--r--activerecord/lib/active_record/reflection.rb67
-rw-r--r--activerecord/test/reflection_test.rb7
3 files changed, 40 insertions, 36 deletions
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index f84a2a89b7..010e4e396b 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -149,7 +149,7 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 81af6ec164..2adadb29c5 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -71,6 +71,7 @@ module ActiveRecord
# 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 @@ module ActiveRecord
@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 @@ module ActiveRecord
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 @@ module ActiveRecord
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
+ 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
- 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
+ active_record.name.foreign_key
end
end
end
diff --git a/activerecord/test/reflection_test.rb b/activerecord/test/reflection_test.rb
index 796076bc81..cc06d31299 100644
--- a/activerecord/test/reflection_test.rb
+++ b/activerecord/test/reflection_test.rb
@@ -60,6 +60,13 @@ class ReflectionTest < Test::Unit::TestCase
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