pundit_extra 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +8 -7
- data/lib/pundit_extra/controller_mixin.rb +5 -5
- data/lib/pundit_extra/helpers.rb +1 -1
- data/lib/pundit_extra/resource_autoload.rb +101 -69
- data/lib/pundit_extra/version.rb +1 -1
- data/lib/pundit_extra.rb +4 -4
- metadata +10 -52
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 77376d6ce79786e54a8a0be60fc52fc45e643f578dcd248df97cbf6a2fdd5232
|
4
|
+
data.tar.gz: e3fa899de77740a36d984fcf7e8e50ed78fb70d3f5d8fa2b6038ac688aef87cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 000b5790efde79b36c76c3c02207019ee88b827948c1786ece090f07b2789b13f67a857ddb2b6def903cc6a22502eedb74d4dfc741bfceb8f581260c00762e25
|
7
|
+
data.tar.gz: e5d5fb8b5342c0167423f80ef2b554ab1a71fb1910061837f3ace599e0f2eda715df41514cbb2b982cc37c748c89f98301d4a4c17ed7d78da8678a57caf5b4a4
|
data/README.md
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# PunditExtra
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/pundit_extra.svg)](
|
4
|
-
[![Build Status](https://
|
5
|
-
[![
|
6
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/pundit_extra.svg)](https://badge.fury.io/rb/pundit_extra)
|
4
|
+
[![Build Status](https://github.com/DannyBen/pundit_extra/workflows/Test/badge.svg)](https://github.com/DannyBen/pundit_extra/actions?query=workflow%3ATest)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/61990b2b88d45ea6c89d/maintainability)](https://codeclimate.com/github/DannyBen/pundit_extra/maintainability)
|
6
|
+
|
7
|
+
---
|
7
8
|
|
8
9
|
This library borrows functionality from [CanCan(Can)][2] and adds it to [Pundit][1].
|
9
10
|
|
@@ -32,7 +33,7 @@ Add to your `ApplicationController`:
|
|
32
33
|
|
33
34
|
```ruby
|
34
35
|
class ApplicationController < ActionController::Base
|
35
|
-
include Pundit
|
36
|
+
include Pundit::Authorization
|
36
37
|
include PunditExtra
|
37
38
|
end
|
38
39
|
```
|
@@ -62,7 +63,7 @@ end
|
|
62
63
|
```
|
63
64
|
|
64
65
|
The `load_resource` filter will create the appropriate instance variable
|
65
|
-
based
|
66
|
+
based on the current action.
|
66
67
|
|
67
68
|
The `authorize_resource` filter will call Pundit's `authorize @model` in each
|
68
69
|
action.
|
@@ -96,7 +97,7 @@ class TasksController < ApplicationController
|
|
96
97
|
end
|
97
98
|
|
98
99
|
def create
|
99
|
-
|
100
|
+
# this happens automatically
|
100
101
|
# @task = Task.new task_params
|
101
102
|
# authorize @task
|
102
103
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module PunditExtra
|
2
2
|
def self.included(_base)
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
return unless defined? ActionController::Base
|
4
|
+
|
5
|
+
ActionController::Base.class_eval do
|
6
|
+
include PunditExtra::Helpers
|
7
|
+
include PunditExtra::ResourceAutoload
|
8
8
|
end
|
9
9
|
end
|
10
10
|
end
|
data/lib/pundit_extra/helpers.rb
CHANGED
@@ -1,69 +1,101 @@
|
|
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_action :load_resource, options.dup
|
10
|
-
end
|
11
|
-
|
12
|
-
def authorize_resource(options={})
|
13
|
-
before_action :authorize_resource, options.dup
|
14
|
-
end
|
15
|
-
|
16
|
-
def skip_authorization(options={})
|
17
|
-
before_action :skip_authorization_and_scope, options.dup
|
18
|
-
end
|
19
|
-
|
20
|
-
def load_and_authorize_resource(options={})
|
21
|
-
# :nocov:
|
22
|
-
load_resource options
|
23
|
-
authorize_resource options
|
24
|
-
# :nocov:
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def load_resource
|
29
|
-
scope = resource_class
|
30
|
-
action = params[:action]
|
31
|
-
varname = resource_name
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
resource =
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
|
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_action :load_resource, options.dup
|
10
|
+
end
|
11
|
+
|
12
|
+
def authorize_resource(options = {})
|
13
|
+
before_action :authorize_resource, options.dup
|
14
|
+
end
|
15
|
+
|
16
|
+
def skip_authorization(options = {})
|
17
|
+
before_action :skip_authorization_and_scope, options.dup
|
18
|
+
end
|
19
|
+
|
20
|
+
def load_and_authorize_resource(options = {})
|
21
|
+
# :nocov:
|
22
|
+
load_resource options
|
23
|
+
authorize_resource options
|
24
|
+
# :nocov:
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_resource
|
29
|
+
scope = resource_class
|
30
|
+
action = params[:action]
|
31
|
+
varname = resource_name
|
32
|
+
|
33
|
+
if action == 'index'
|
34
|
+
varname = controller_name
|
35
|
+
resource = policy_scope resource_class
|
36
|
+
|
37
|
+
elsif action == 'new'
|
38
|
+
resource = scope.new
|
39
|
+
|
40
|
+
elsif action == 'create'
|
41
|
+
resource = scope.new
|
42
|
+
resource.attributes = resource_attributes resource, action
|
43
|
+
|
44
|
+
elsif action == 'update'
|
45
|
+
resource = scope.find params[:id]
|
46
|
+
resource.attributes = resource_attributes resource, action
|
47
|
+
|
48
|
+
elsif params[:id]
|
49
|
+
resource = scope.find params[:id]
|
50
|
+
|
51
|
+
else
|
52
|
+
resource = nil
|
53
|
+
end
|
54
|
+
|
55
|
+
instance_variable_set :"@#{varname}", resource
|
56
|
+
end
|
57
|
+
|
58
|
+
def authorize_resource
|
59
|
+
resource = resource_instance || resource_class
|
60
|
+
authorize resource
|
61
|
+
end
|
62
|
+
|
63
|
+
def skip_authorization_and_scope
|
64
|
+
action = params[:action]
|
65
|
+
skip_policy_scope if action == 'index'
|
66
|
+
skip_authorization
|
67
|
+
end
|
68
|
+
|
69
|
+
def resource_name
|
70
|
+
controller_name.singularize
|
71
|
+
end
|
72
|
+
|
73
|
+
def resource_class
|
74
|
+
resource_name.classify.constantize
|
75
|
+
end
|
76
|
+
|
77
|
+
def resource_instance
|
78
|
+
instance_variable_get :"@#{resource_name}"
|
79
|
+
end
|
80
|
+
|
81
|
+
def resource_attributes(resource, action)
|
82
|
+
if has_permitted_attributes? resource, action
|
83
|
+
return permitted_attributes resource
|
84
|
+
end
|
85
|
+
|
86
|
+
candidates = ["#{action}_params", "#{resource_name}_params"]
|
87
|
+
candidates.each do |candidate|
|
88
|
+
return send(candidate) if respond_to? candidate, true
|
89
|
+
end
|
90
|
+
|
91
|
+
{}
|
92
|
+
end
|
93
|
+
|
94
|
+
def has_permitted_attributes?(resource, action)
|
95
|
+
return true if policy(resource).respond_to? :"permitted_attributes_for_#{action}"
|
96
|
+
return true if policy(resource).respond_to? :permitted_attributes
|
97
|
+
|
98
|
+
false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/lib/pundit_extra/version.rb
CHANGED
data/lib/pundit_extra.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'pundit_extra/controller_mixin'
|
2
|
+
require 'pundit_extra/helpers'
|
3
|
+
require 'pundit_extra/resource_autoload'
|
4
|
+
require 'pundit_extra/version'
|
metadata
CHANGED
@@ -1,57 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pundit_extra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
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.7'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0.7'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: runfile-tasks
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0.4'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0.4'
|
11
|
+
date: 2024-07-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
55
13
|
description: Add some helpers and additional functionality to Pundit.
|
56
14
|
email: db@dannyben.com
|
57
15
|
executables: []
|
@@ -67,8 +25,9 @@ files:
|
|
67
25
|
homepage: https://github.com/DannyBen/pundit_extra
|
68
26
|
licenses:
|
69
27
|
- MIT
|
70
|
-
metadata:
|
71
|
-
|
28
|
+
metadata:
|
29
|
+
rubygems_mfa_required: 'true'
|
30
|
+
post_install_message:
|
72
31
|
rdoc_options: []
|
73
32
|
require_paths:
|
74
33
|
- lib
|
@@ -76,16 +35,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
35
|
requirements:
|
77
36
|
- - ">="
|
78
37
|
- !ruby/object:Gem::Version
|
79
|
-
version:
|
38
|
+
version: 3.0.0
|
80
39
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
40
|
requirements:
|
82
41
|
- - ">="
|
83
42
|
- !ruby/object:Gem::Version
|
84
43
|
version: '0'
|
85
44
|
requirements: []
|
86
|
-
|
87
|
-
|
88
|
-
signing_key:
|
45
|
+
rubygems_version: 3.5.14
|
46
|
+
signing_key:
|
89
47
|
specification_version: 4
|
90
48
|
summary: Additions for Pundit
|
91
49
|
test_files: []
|