rails/rails — pull request #6165 by marcandre (2012-05-05 04:25:19+00:00)
Title: Make constantize look down the ancestor chain (excluding Object)

`constantize` is meant to avoid looking up in `Object`, e.g. so that `"String::Fixnum".constantize` raises an error.

Unfortunately, the ancestor chain is not looked up at all.

class A
Foo = :bar
end
class B < A
end
"B::Foo".constantize # => raises an error, should return B::Foo (which is == A::Foo)

I ran into this when registering an observer (say "some_class/observer"), where `SomeClass::Observer` is defined in a base class of `SomeClass`. That currently fails, as `Observer` has to be defined directly in `SomeClass`.

One solution is for me to write:

class SomeClass < SomeBaseClass
Observer = Observer # to satisfy Rails constantize lookup!!
end

I hope you appreciate the irony :-)

I feel that there is no reason for `constantize` to be so strict, though. The attached patch fixes this.

Thanks.