mirror of
https://github.com/varvet/pundit.git
synced 2022-11-09 12:30:11 -05:00
Revert namespaces
This commit is contained in:
parent
165c7f4e5c
commit
753bb0a2b6
6 changed files with 18 additions and 60 deletions
|
@ -2,7 +2,6 @@
|
|||
|
||||
## 0.3.0 (unreleased)
|
||||
|
||||
- Enable namespaced policies (#152)
|
||||
- Extend the default `ApplicationPolicy` with an `ApplicationPolicy::Scope` (#120)
|
||||
- Fix RSpec 3 deprecation warnings for built-in matchers (#162)
|
||||
- Generate blank policy spec/test files for Rspec/MiniTest/Test::Unit in Rails (#138)
|
||||
|
|
27
README.md
27
README.md
|
@ -54,7 +54,7 @@ class PostPolicy
|
|||
end
|
||||
```
|
||||
|
||||
As you can see, this is just a plain Ruby class. Pundit makes the following
|
||||
As you can see, this is just a plain Ruby class. Pundit makes the following
|
||||
assumptions about this class:
|
||||
|
||||
- The class has the same name as some kind of model class, only suffixed
|
||||
|
@ -69,7 +69,7 @@ assumptions about this class:
|
|||
|
||||
That's it really.
|
||||
|
||||
Usually you'll want to inherit from the application policy created by the
|
||||
Usually you'll want to inherit from the application policy created by the
|
||||
generator, or set up your own base class to inherit from:
|
||||
|
||||
``` ruby
|
||||
|
@ -126,26 +126,9 @@ conditionally showing links or buttons in the view:
|
|||
<%= link_to "Edit post", edit_post_path(@post) %>
|
||||
<% end %>
|
||||
```
|
||||
## Namespaced policies
|
||||
|
||||
You can namespace your policies, e.g. for using an `Admin::PostPolicy`
|
||||
in your `Admin::PostsController`. Since Pundit guesses the namespace
|
||||
from the controller you're calling `authorize @post` from, all you
|
||||
need to do to use a namespaced policy is create it.
|
||||
|
||||
```ruby
|
||||
# app/policies/admin/post_policy.rb
|
||||
class Admin::PostPolicy < Admin::ApplicationPolicy
|
||||
# ...
|
||||
end
|
||||
```
|
||||
|
||||
When no namespaced policy can be found, Pundit falls back to using the
|
||||
non-namespaced policy.
|
||||
|
||||
## Headless policies
|
||||
|
||||
Given there is a policy without a corresponding model / ruby class,
|
||||
Given there is a policy without a corresponding model / ruby class,
|
||||
you can retrieve it by passing a symbol.
|
||||
|
||||
```ruby
|
||||
|
@ -198,12 +181,12 @@ define a class called a policy scope. It can look something like this:
|
|||
class PostPolicy < ApplicationPolicy
|
||||
class Scope
|
||||
attr_reader :user, :scope
|
||||
|
||||
|
||||
def initialize(user, scope)
|
||||
@user = user
|
||||
@scope = scope
|
||||
end
|
||||
|
||||
|
||||
def resolve
|
||||
if user.admin?
|
||||
scope.all
|
||||
|
|
|
@ -17,22 +17,22 @@ module Pundit
|
|||
extend ActiveSupport::Concern
|
||||
|
||||
class << self
|
||||
def policy_scope(user, scope, namespace = Object)
|
||||
policy_scope = PolicyFinder.new(scope, namespace).scope
|
||||
def policy_scope(user, scope)
|
||||
policy_scope = PolicyFinder.new(scope).scope
|
||||
policy_scope.new(user, scope).resolve if policy_scope
|
||||
end
|
||||
|
||||
def policy_scope!(user, scope, namespace = Object)
|
||||
PolicyFinder.new(scope, namespace).scope!.new(user, scope).resolve
|
||||
def policy_scope!(user, scope)
|
||||
PolicyFinder.new(scope).scope!.new(user, scope).resolve
|
||||
end
|
||||
|
||||
def policy(user, record, namespace = Object)
|
||||
policy = PolicyFinder.new(record, namespace).policy
|
||||
def policy(user, record)
|
||||
policy = PolicyFinder.new(record).policy
|
||||
policy.new(user, record) if policy
|
||||
end
|
||||
|
||||
def policy!(user, record, namespace = Object)
|
||||
PolicyFinder.new(record, namespace).policy!.new(user, record)
|
||||
def policy!(user, record)
|
||||
PolicyFinder.new(record).policy!.new(user, record)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -79,12 +79,12 @@ module Pundit
|
|||
|
||||
def policy_scope(scope)
|
||||
@_policy_scoped = true
|
||||
@policy_scope or Pundit.policy_scope!(pundit_user, scope, self.class.parent)
|
||||
@policy_scope or Pundit.policy_scope!(pundit_user, scope)
|
||||
end
|
||||
attr_writer :policy_scope
|
||||
|
||||
def policy(record)
|
||||
@_policy or Pundit.policy!(pundit_user, record, self.class.parent)
|
||||
@_policy or Pundit.policy!(pundit_user, record)
|
||||
end
|
||||
|
||||
def policy=(policy)
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
module Pundit
|
||||
class PolicyFinder
|
||||
attr_reader :object, :namespace
|
||||
attr_reader :object
|
||||
|
||||
def initialize(object, namespace = Object)
|
||||
def initialize(object)
|
||||
@object = object
|
||||
@namespace = namespace
|
||||
end
|
||||
|
||||
def scope
|
||||
|
@ -15,7 +14,7 @@ module Pundit
|
|||
|
||||
def policy
|
||||
klass = find
|
||||
klass = namespace.const_get(klass.demodulize) if klass.is_a?(String)
|
||||
klass = klass.constantize if klass.is_a?(String)
|
||||
klass
|
||||
rescue NameError
|
||||
nil
|
||||
|
|
|
@ -4,12 +4,10 @@ describe Pundit do
|
|||
let(:user) { double }
|
||||
let(:post) { Post.new(user) }
|
||||
let(:comment) { Comment.new }
|
||||
let(:nested_comment) { Admin::Comment.new }
|
||||
let(:article) { Article.new }
|
||||
let(:controller) { Controller.new(user, { :action => 'update' }) }
|
||||
let(:artificial_blog) { ArtificialBlog.new }
|
||||
let(:article_tag) { ArticleTag.new }
|
||||
let(:nested_controller) { Admin::Controller.new }
|
||||
|
||||
describe ".policy_scope" do
|
||||
it "returns an instantiated policy scope given a plain model class" do
|
||||
|
@ -212,15 +210,6 @@ describe Pundit do
|
|||
expect { controller.policy(article) }.to raise_error(Pundit::NotDefinedError)
|
||||
end
|
||||
|
||||
it "looks up the policy class based on the caller's namespace" do
|
||||
expect(nested_controller.policy(comment).class).to eq Admin::CommentPolicy
|
||||
expect(nested_controller.policy(nested_comment).class).to eq Admin::CommentPolicy
|
||||
end
|
||||
|
||||
it "falls back to the non-namespaced policy class if there isn't a namespaced one" do
|
||||
expect(nested_controller.policy(post).class).to eq PostPolicy
|
||||
end
|
||||
|
||||
it "allows policy to be injected" do
|
||||
new_policy = OpenStruct.new
|
||||
controller.policy = new_policy
|
||||
|
|
|
@ -60,9 +60,6 @@ class CommentPolicy::Scope < Struct.new(:user, :scope)
|
|||
end
|
||||
end
|
||||
class Comment; extend ActiveModel::Naming; end
|
||||
module Admin
|
||||
class Comment; end
|
||||
end
|
||||
|
||||
class Article; end
|
||||
|
||||
|
@ -98,12 +95,3 @@ class Controller
|
|||
@params = params
|
||||
end
|
||||
end
|
||||
|
||||
module Admin
|
||||
class CommentPolicy < Struct.new(:user, :comment); end
|
||||
class Controller
|
||||
include Pundit
|
||||
|
||||
attr_reader :current_user, :params
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue