提交 b8aebb02 编写于 作者: A Aaron Patterson

acually get the actual source reflection :cry::gun:

上级 2aa20b36
......@@ -371,6 +371,12 @@ def polymorphic?
VALID_AUTOMATIC_INVERSE_MACROS = [:has_many, :has_one, :belongs_to]
INVALID_AUTOMATIC_INVERSE_OPTIONS = [:conditions, :through, :polymorphic, :foreign_key]
protected
def actual_source_reflection # FIXME: this is a horrible name
self
end
private
# Attempts to find the inverse association name automatically.
# If it cannot find a suitable inverse association name, it returns
......@@ -588,15 +594,10 @@ def nested?
# We want to use the klass from this reflection, rather than just delegate straight to
# the source_reflection, because the source_reflection may be polymorphic. We still
# need to respect the source_reflection's :primary_key option, though.
def association_primary_key(klass = nil)
def association_primary_key(target_class = nil)
# Get the "actual" source reflection if the immediate source reflection has a
# source reflection itself
source_reflection = self.source_reflection
while source_reflection.source_reflection
source_reflection = source_reflection.source_reflection
end
source_reflection.options[:primary_key] || primary_key(klass || self.klass)
actual_source_reflection.options[:primary_key] || primary_key(klass || self.klass)
end
# Gets an array of possible <tt>:through</tt> source reflection names in both singular and plural form.
......@@ -675,6 +676,12 @@ def check_validity!
check_validity_of_inverse!
end
protected
def actual_source_reflection # FIXME: this is a horrible name
source_reflection.actual_source_reflection
end
private
def derive_class_name
# get the class_name of the belongs_to association of the through reflection
......
......@@ -252,8 +252,9 @@ def test_association_primary_key_raises_when_missing_primary_key
reflection = ActiveRecord::Reflection::AssociationReflection.new(:fuu, :edge, nil, {}, Author)
assert_raises(ActiveRecord::UnknownPrimaryKey) { reflection.association_primary_key }
through = ActiveRecord::Reflection::ThroughReflection.new(:fuu, :edge, nil, {}, Author)
through.stubs(:source_reflection).returns(stub_everything(:options => {}, :class_name => 'Edge'))
through = Class.new(ActiveRecord::Reflection::ThroughReflection) {
define_method(:source_reflection) { reflection }
}.new(:fuu, :edge, nil, {}, Author)
assert_raises(ActiveRecord::UnknownPrimaryKey) { through.association_primary_key }
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册