elabs/pundit — pull request #11 by lankz (2013-01-15 03:34:10+00:00)
Title: Improve Scope Resolution

`PolicyFinder` fails to find a `Scope` class when used with inheritance. For example:

class ContentPolicy
...

class Scope
def resolve
...
end
end
end

Pundit::PolicyFinder.new(Content).scope
=> ContentPolicy::Scope

class PhotoPolicy < ContentPolicy
class Scope
def resolve
...
end
end
end

Pundit::PolicyFinder.new(Photo).scope
=> PhotoPolicy::Scope

class ArticlePolicy < ContentPolicy
end

Pundit::PolicyFinder.new(Article).scope
=> nil

It looks like this happens due to the behaviour of `#constantize` and `#safe_constantize`:

"ContentPolicy::Scope".safe_constantize
=> ContentPolicy::Scope
"ArticlePolicy::Scope".safe_constantize
=> nil

With this change, `PolicyFinder` stops trying to "find" scopes. The policy is found in the normal way (using `safe_constantize` rather than `constantize`) and interrogated for the `Scope` class using `#const_get`.