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

Add support for custom policy namespace lookup from controllers and view... #179

Closed
wants to merge 2 commits into from

Conversation

DanOlson
Copy link

...s. Fixes #178

@thomasklemm
Copy link
Collaborator

While I can see the need for that, it looks weird to me and I guess I'd forget the namespace sometimes (so it would error silently and pick the wrong policy). Can we make this easier to use? I guess we can assume that all policy calls in a template should start looking for a policy in the same namespace.

@DanOlson
Copy link
Author

This doesn't change any existing behavior, it just allows for passing a custom namespace if one can't (or shouldn't) be inferred from the controller. I don't think it would silently pick the wrong policy any more often that it would currently.

@thomasklemm
Copy link
Collaborator

Well, authorize @record (in master) looks up the parent namespace: Source. We might be able to determine the namespace the view is rendered from via params[:controller], and pass it in. Behaves like authorize @record then.

@DanOlson
Copy link
Author

The issue describes a problem where the Admin::Tag policy is desired from a non-namespaced controller TagsController. I've maintained the existing lookup of the parent namespace when one is not provided explicitly here.

But, I might be misunderstanding what you're saying.

@thomasklemm
Copy link
Collaborator

I'm saying that I'm not sure that self.class.parent will lead us to the namespace expected namespace when calling the policy helper from a view. Could you check whether this works at all with the current master?

@thomasklemm
Copy link
Collaborator

BTW, personally I'd prefer admin_policy(@record) over policy(@record, namespace: 'Admin') for use in the view. Since we won't generate that automatically, you should wrap policy yourself for your primary namespaces.

admin_policy(@record)

# e.g. app/helpers/policy_helper.rb
module PolicyHelper
  def admin_policy(record, options = {})
    policy(record, options.reverse_merge(namespace: 'Admin'))
  end
end

@thomasklemm
Copy link
Collaborator

Thanks Dan for this PR, it looks good 👍

@jnicklas
Copy link
Collaborator

Support for namespaces has been yanked. Closing.

@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.

Unable to access namespaced policy in view
3 participants