Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes policy lookup to look at controller's namespace and then fallback to model's namespace #190

Closed
wants to merge 3 commits into from

Conversation

divoxx
Copy link

@divoxx divoxx commented Aug 12, 2014

This is related to #172

Basically, if your model lives inside a namespace that would not be considered when falling back when looking up for the policy.

With this change, if one had a controller Api::V1::Foo::Bar and a model Foo::Bar, when looking for the policy it would try Api::V1::Foo::BarPolicy and then BarPolicy. Now it will look for Foo::BarPolicy.

@shekibobo
Copy link

I just tested this, and what I'm seeing is this:

Pundit::NotDefinedError:
       unable to find scope [Object::AudioPolicy::Scope, ActiveRecord::AudioPolicy::Scope] for #<ActiveRecord::Relation::ActiveRecord_Relation_Media_Audio:0x007fbbdf19dfc8>

Instead of looking up Media::AudioPolicy, it's looking up Object::AudioPolicy. This is happening in all cases of namespaced models. The top-level namespace module is simply being replaced with Object.

@divoxx
Copy link
Author

divoxx commented Aug 12, 2014

@shekibobo how are your controller and models defined?

@shekibobo
Copy link

My controllers are under the Admin namespace. My models are not, but some are namespaced, such as Media::Audio, Media::Video, etc. Any models that are namespaced like that end up with the Object::<demodulized_class_name>Policy lookup failing. My policies are defined like so;

app/
  policies/
    media/
      audio_policy.rb
      video_policy.rb
    application_policy.rb

@divoxx
Copy link
Author

divoxx commented Aug 12, 2014

What about the FQN for the controller and model? Admin::Media::AudioController and Media::Audio ?

@divoxx
Copy link
Author

divoxx commented Aug 12, 2014

@shekibobo I pushed a new change, can you try again?

@shekibobo
Copy link

Now it looks like it's trying to call ::Scope directly on whatever the object is.

Pundit::NotDefinedError:
   unable to find scope ::Scope for #<ActiveRecord::Relation::ActiveRecord_Relation_Catalog_Category:0x007fe61c96b860>
     # ~/.gem/ruby/2.1.2/bundler/gems/pundit-610c2722a33d/lib/pundit/policy_finder.rb:29:in `scope!'
     # ~/.gem/ruby/2.1.2/bundler/gems/pundit-610c2722a33d/lib/pundit.rb:26:in `policy_scope!'
     # ...

Pundit::NotDefinedError:
   unable to find scope ::Scope for Analytics::LogEntry

@jnicklas
Copy link
Collaborator

Support for namespaces has been removed, so this is no longer valid.

@jnicklas jnicklas closed this Aug 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants