1
0
Fork 0
mirror of https://github.com/varvet/pundit.git synced 2022-11-09 12:30:11 -05:00

Merge pull request #152 from ecbypi/12-namespaced-policies

Lookup policies in the current namespace
This commit is contained in:
Jonas Nicklas 2014-05-23 16:58:25 +02:00
commit 617b401a8c
4 changed files with 41 additions and 14 deletions

View file

@ -3,6 +3,7 @@ require "pundit/policy_finder"
require "active_support/concern" require "active_support/concern"
require "active_support/core_ext/string/inflections" require "active_support/core_ext/string/inflections"
require "active_support/core_ext/object/blank" require "active_support/core_ext/object/blank"
require "active_support/core_ext/module/introspection"
require "active_support/dependencies/autoload" require "active_support/dependencies/autoload"
module Pundit module Pundit
@ -15,22 +16,22 @@ module Pundit
extend ActiveSupport::Concern extend ActiveSupport::Concern
class << self class << self
def policy_scope(user, scope) def policy_scope(user, scope, namespace = Object)
policy_scope = PolicyFinder.new(scope).scope policy_scope = PolicyFinder.new(scope, namespace).scope
policy_scope.new(user, scope).resolve if policy_scope policy_scope.new(user, scope).resolve if policy_scope
end end
def policy_scope!(user, scope) def policy_scope!(user, scope, namespace = Object)
PolicyFinder.new(scope).scope!.new(user, scope).resolve PolicyFinder.new(scope, namespace).scope!.new(user, scope).resolve
end end
def policy(user, record) def policy(user, record, namespace = Object)
policy = PolicyFinder.new(record).policy policy = PolicyFinder.new(record, namespace).policy
policy.new(user, record) if policy policy.new(user, record) if policy
end end
def policy!(user, record) def policy!(user, record, namespace = Object)
PolicyFinder.new(record).policy!.new(user, record) PolicyFinder.new(record, namespace).policy!.new(user, record)
end end
end end
@ -77,12 +78,12 @@ module Pundit
def policy_scope(scope) def policy_scope(scope)
@_policy_scoped = true @_policy_scoped = true
@policy_scope or Pundit.policy_scope!(pundit_user, scope) @policy_scope or Pundit.policy_scope!(pundit_user, scope, self.class.parent)
end end
attr_writer :policy_scope attr_writer :policy_scope
def policy(record) def policy(record)
@policy or Pundit.policy!(pundit_user, record) @policy or Pundit.policy!(pundit_user, record, self.class.parent)
end end
attr_writer :policy attr_writer :policy

View file

@ -1,9 +1,10 @@
module Pundit module Pundit
class PolicyFinder class PolicyFinder
attr_reader :object attr_reader :object, :namespace
def initialize(object) def initialize(object, namespace = Object)
@object = object @object = object
@namespace = namespace
end end
def scope def scope
@ -14,7 +15,7 @@ module Pundit
def policy def policy
klass = find klass = find
klass = klass.constantize if klass.is_a?(String) klass = namespace.const_get(klass) if klass.is_a?(String)
klass klass
rescue NameError rescue NameError
nil nil

View file

@ -5,9 +5,10 @@ describe Pundit do
let(:post) { Post.new(user) } let(:post) { Post.new(user) }
let(:comment) { Comment.new } let(:comment) { Comment.new }
let(:article) { Article.new } let(:article) { Article.new }
let(:controller) { double(:current_user => user, :params => { :action => "update" }).tap { |c| c.extend(Pundit) } } let(:controller) { Controller.new(user, { :action => 'update' }) }
let(:artificial_blog) { ArtificialBlog.new } let(:artificial_blog) { ArtificialBlog.new }
let(:article_tag) { ArticleTag.new } let(:article_tag) { ArticleTag.new }
let(:nested_controller) { Admin::Controller.new }
describe ".policy_scope" do describe ".policy_scope" do
it "returns an instantiated policy scope given a plain model class" do it "returns an instantiated policy scope given a plain model class" do
@ -196,6 +197,10 @@ describe Pundit do
expect { controller.policy(article) }.to raise_error(Pundit::NotDefinedError) expect { controller.policy(article) }.to raise_error(Pundit::NotDefinedError)
end end
it "looks up the policy class based on the caller's namespace" do
expect(nested_controller.policy(comment).class).to eq Admin::CommentPolicy
end
it "allows policy to be injected" do it "allows policy to be injected" do
new_policy = OpenStruct.new new_policy = OpenStruct.new
controller.policy = new_policy controller.policy = new_policy

View file

@ -54,3 +54,23 @@ class ArticleTag
end end
end end
end end
class Controller
include Pundit
attr_reader :current_user, :params
def initialize(current_user, params)
@current_user = current_user
@params = params
end
end
module Admin
class CommentPolicy < Struct.new(:user, :comment); end
class Controller
include Pundit
attr_reader :current_user, :params
end
end