pundit_extra 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +100 -0
- data/lib/pundit_extra.rb +3 -0
- data/lib/pundit_extra/controller_mixin.rb +13 -0
- data/lib/pundit_extra/helpers.rb +15 -0
- data/lib/pundit_extra/resource_autoload.rb +71 -0
- data/lib/pundit_extra/version.rb +3 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 258e0ba6e9d58cf4d45228e19508f7ce450a3953
|
4
|
+
data.tar.gz: b894cb1c27571894af39a308e171fc3bfafdc99f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2a0a718d91254cc1bc64908f4b0e746cdefbdaf5e336ee01eb4d1cb2e709420b6bd822466ad73419596820c089a79bc5071026e9be6ea6645a2871da28cb48d8
|
7
|
+
data.tar.gz: 359f33ab68954463098ec4688053ad77b3868e3cec02e8ef6581339a264e3fa0175c7c031d8220a57c90efca55a6f6dbc56de87467e8d972dc66f0b77878303b
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# PunditExtra
|
2
|
+
|
3
|
+
This library borrows functionality from [CanCan(Can)][2] and adds it to [Pundit][1].
|
4
|
+
|
5
|
+
- `can?` and `cannot?` view helpers
|
6
|
+
- `load_resource`, `authorize_resource`, `load_and_authorize_resource` and
|
7
|
+
`skip_authorization` controller filters
|
8
|
+
|
9
|
+
|
10
|
+
## Install
|
11
|
+
|
12
|
+
Add to your Gemfile:
|
13
|
+
|
14
|
+
```
|
15
|
+
gem 'pundit_extra'
|
16
|
+
```
|
17
|
+
|
18
|
+
Add to your `ApplicationController`:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class ApplicationController < ActionController::Base
|
22
|
+
include Pundit
|
23
|
+
include PunditExtra
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
## View Helpers: `can?` and `cannot?`
|
29
|
+
|
30
|
+
You can use the convenience methods `can?` and `cannot?` in any convreoller
|
31
|
+
and view.
|
32
|
+
|
33
|
+
`if can? :assign, @task` is the same as Pundit's `policy(@task).assign?`
|
34
|
+
`if can? :index, Task` is the same as Pundit's `policy(Task).index?`
|
35
|
+
`if cannot? :assign, @task` is the opposite of `can?`
|
36
|
+
|
37
|
+
|
38
|
+
## Autoload and Authorize Resource
|
39
|
+
|
40
|
+
You can add these to your controllers to automatically load the resource
|
41
|
+
and/or authorize it.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class TasksController < ApplicationController
|
45
|
+
before_action :authenticate_user!
|
46
|
+
load_resource except: [:index, :create]
|
47
|
+
authorize_resource except: [:create]
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
The `load_resource` filter will create the appropriate instance variable
|
52
|
+
based onm the current action.
|
53
|
+
|
54
|
+
The `authorize_resource` filter will call Pundit's `authorize @model` in each
|
55
|
+
action.
|
56
|
+
|
57
|
+
You can use `except: :action`, or `only: :action` to limit the filter to a
|
58
|
+
given action or an array of actions.
|
59
|
+
|
60
|
+
Example:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class TasksController < ApplicationController
|
64
|
+
before_action :authenticate_user!
|
65
|
+
load_resource except: [:index, :edit]
|
66
|
+
authorize_resource except: :index
|
67
|
+
|
68
|
+
def show
|
69
|
+
# this happens automatically
|
70
|
+
# @task = Task.find params[:id]
|
71
|
+
# authorize @task
|
72
|
+
end
|
73
|
+
|
74
|
+
def new
|
75
|
+
# this happens automatically
|
76
|
+
# @task = Task.new
|
77
|
+
# authorize @task
|
78
|
+
end
|
79
|
+
|
80
|
+
def create
|
81
|
+
# this happens automatically
|
82
|
+
# @task = Task.new task_params
|
83
|
+
# authorize @task
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
## Credits
|
90
|
+
|
91
|
+
- [Jonas Nicklas](https://github.com/jnicklas) @ [Pundit][1]
|
92
|
+
- [Bryan Rite](https://github.com/bryanrite), [Ryan Bates](https://github.com/ryanb), [Richard Wilson](https://github.com/Senjai) @ [CanCanCan][2]
|
93
|
+
- [Tom Morgan](https://github.com/seven1m)
|
94
|
+
|
95
|
+
Thanks for building awesome stuff.
|
96
|
+
|
97
|
+
---
|
98
|
+
|
99
|
+
[1]: https://github.com/elabs/pundit
|
100
|
+
[2]: https://github.com/CanCanCommunity/cancancan
|
data/lib/pundit_extra.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'pundit_extra/helpers'
|
2
|
+
require 'pundit_extra/resource_autoload'
|
3
|
+
|
4
|
+
module PunditExtra
|
5
|
+
def self.included(base)
|
6
|
+
if defined? ActionController::Base
|
7
|
+
ActionController::Base.class_eval do
|
8
|
+
include PunditExtra::Helpers
|
9
|
+
include PunditExtra::ResourceAutoload
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module PunditExtra
|
2
|
+
module Helpers
|
3
|
+
def self.included(base)
|
4
|
+
base.helper_method :can?, :cannot? if base.respond_to? :helper_method
|
5
|
+
end
|
6
|
+
|
7
|
+
def can?(action, resource)
|
8
|
+
policy(resource).send "#{action}?"
|
9
|
+
end
|
10
|
+
|
11
|
+
def cannot?(*args)
|
12
|
+
!can? *args
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module PunditExtra
|
4
|
+
module ResourceAutoload
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def load_resource(options={})
|
9
|
+
before_filter :load_resource, options.dup
|
10
|
+
end
|
11
|
+
|
12
|
+
def authorize_resource(options={})
|
13
|
+
before_filter :authorize_resource, options.dup
|
14
|
+
end
|
15
|
+
|
16
|
+
def skip_authorization(options={})
|
17
|
+
before_filter :skip_authorization_and_scope, options.dup
|
18
|
+
end
|
19
|
+
|
20
|
+
def load_and_authorize_resource(options={})
|
21
|
+
load_resource options
|
22
|
+
authorize_resource options
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def load_resource
|
27
|
+
scope = resource_class
|
28
|
+
action = params[:action]
|
29
|
+
varname = resource_name
|
30
|
+
if action == 'index'
|
31
|
+
varname = controller_name
|
32
|
+
resource = policy_scope resource_class
|
33
|
+
elsif ['new', 'create'].include? action
|
34
|
+
resource = scope.new
|
35
|
+
resource.attributes = send("#{resource_name}_params") if action == 'create'
|
36
|
+
elsif params[:id]
|
37
|
+
resource = scope.find params[:id]
|
38
|
+
else
|
39
|
+
resource = nil
|
40
|
+
end
|
41
|
+
instance_variable_set "@#{varname}", resource
|
42
|
+
end
|
43
|
+
|
44
|
+
def authorize_resource
|
45
|
+
resource = resource_instance || resource_class
|
46
|
+
authorize resource
|
47
|
+
end
|
48
|
+
|
49
|
+
def skip_authorization_and_scope
|
50
|
+
action = params[:action]
|
51
|
+
if action == 'index'
|
52
|
+
skip_authorization
|
53
|
+
skip_policy_scope
|
54
|
+
else
|
55
|
+
skip_authorization
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def resource_name
|
60
|
+
controller_name.singularize
|
61
|
+
end
|
62
|
+
|
63
|
+
def resource_class
|
64
|
+
resource_name.classify.constantize
|
65
|
+
end
|
66
|
+
|
67
|
+
def resource_instance
|
68
|
+
instance_variable_get "@#{resource_name}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pundit_extra
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Danny Ben Shitrit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: combustion
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: runfile
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: run-gem-dev
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.2'
|
55
|
+
description: Add some helpers and additional functionality to Pundit.
|
56
|
+
email: db@dannyben.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- README.md
|
62
|
+
- lib/pundit_extra.rb
|
63
|
+
- lib/pundit_extra/controller_mixin.rb
|
64
|
+
- lib/pundit_extra/helpers.rb
|
65
|
+
- lib/pundit_extra/resource_autoload.rb
|
66
|
+
- lib/pundit_extra/version.rb
|
67
|
+
homepage: https://github.com/DannyBen/pundit_extra
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 2.0.0
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.4.6
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Additions for Pundit
|
91
|
+
test_files: []
|