pundit-before 0.0.1
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/.editorconfig +12 -0
- data/.envrc +1 -0
- data/.pryrc +3 -0
- data/.rubocop.yml +57 -0
- data/.tool-versions +1 -0
- data/Appraisals +15 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +80 -0
- data/LICENSE +21 -0
- data/README.md +204 -0
- data/Rakefile +16 -0
- data/gemfiles/pundit_2.0.gemfile +14 -0
- data/gemfiles/pundit_2.1.gemfile +14 -0
- data/gemfiles/pundit_2.2.gemfile +14 -0
- data/gemfiles/pundit_2.3.gemfile +14 -0
- data/lib/pundit/before/version.rb +22 -0
- data/lib/pundit/before.rb +98 -0
- data/lib/pundit-before.rb +3 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 120b45a26b35d09c85f44f3eceac5557759770e94da78d8d8ac1538121a61c52
|
4
|
+
data.tar.gz: e52a3f89c3e7a07b613f64cb3b4dfd3c6825c106a804de6520508077ac44a777
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 10123635560be0123e65d2799b707727b2b88d60634e15e58c84570008454bf5c7a28503bd5fbf91871adb1e9bf3de6aa0de50e9815ac788399a925138c3ab74
|
7
|
+
data.tar.gz: 1c1afcd63dc07af3b76359e27f4af777fe86d5deb109e24901d263e47d8920a0df0e0e57a8ec4252590bb565fb1668079901cf28e2d7c1e97460f5880007ef91
|
data/.editorconfig
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# This file is for unifying the coding style for different editors and IDEs
|
2
|
+
# editorconfig.org
|
3
|
+
|
4
|
+
root = true
|
5
|
+
|
6
|
+
[*]
|
7
|
+
charset = utf-8
|
8
|
+
indent_size = 2
|
9
|
+
indent_style = space
|
10
|
+
insert_final_newline = true
|
11
|
+
max_line_length = 120
|
12
|
+
trim_trailing_whitespace = true
|
data/.envrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
PATH_add bin
|
data/.pryrc
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-minitest
|
3
|
+
- rubocop-rake
|
4
|
+
|
5
|
+
inherit_mode:
|
6
|
+
merge:
|
7
|
+
- Exclude
|
8
|
+
- Include
|
9
|
+
- Prefixes
|
10
|
+
|
11
|
+
AllCops:
|
12
|
+
DisplayCopNames: true
|
13
|
+
Exclude:
|
14
|
+
- "**/bin/**/*"
|
15
|
+
- "**/gemfiles/**/*"
|
16
|
+
- "**/Appraisals"
|
17
|
+
NewCops: enable
|
18
|
+
TargetRubyVersion: 2.7
|
19
|
+
|
20
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
21
|
+
EnforcedStyle: no_space
|
22
|
+
|
23
|
+
Metrics/ClassLength:
|
24
|
+
CountAsOne:
|
25
|
+
- array
|
26
|
+
- hash
|
27
|
+
- heredoc
|
28
|
+
|
29
|
+
Metrics/MethodLength:
|
30
|
+
CountAsOne:
|
31
|
+
- array
|
32
|
+
- hash
|
33
|
+
- heredoc
|
34
|
+
|
35
|
+
Metrics/ModuleLength:
|
36
|
+
CountAsOne:
|
37
|
+
- array
|
38
|
+
- hash
|
39
|
+
- heredoc
|
40
|
+
|
41
|
+
Naming/FileName:
|
42
|
+
Exclude:
|
43
|
+
- "**/lib/pundit-before.rb"
|
44
|
+
|
45
|
+
Style/Documentation:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Style/StringLiterals:
|
49
|
+
Enabled: true
|
50
|
+
EnforcedStyle: double_quotes
|
51
|
+
|
52
|
+
Style/StringLiteralsInInterpolation:
|
53
|
+
Enabled: true
|
54
|
+
EnforcedStyle: double_quotes
|
55
|
+
|
56
|
+
Layout/LineLength:
|
57
|
+
Max: 120
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 3.2.0
|
data/Appraisals
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem "appraisal", github: "excid3/appraisal", branch: "fix-bundle-env"
|
8
|
+
gem "minitest", "~> 5.0"
|
9
|
+
gem "pry", "~> 0.14"
|
10
|
+
gem "rake", "~> 13.0"
|
11
|
+
gem "rubocop", "~> 1.21"
|
12
|
+
gem "rubocop-minitest", "~> 0.19"
|
13
|
+
gem "rubocop-rake", "~> 0.6"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/excid3/appraisal.git
|
3
|
+
revision: 14855fc54ce88b42def871ce8bfd4259fbf06043
|
4
|
+
branch: fix-bundle-env
|
5
|
+
specs:
|
6
|
+
appraisal (2.4.1)
|
7
|
+
bundler
|
8
|
+
rake
|
9
|
+
thor (>= 0.14.0)
|
10
|
+
|
11
|
+
PATH
|
12
|
+
remote: .
|
13
|
+
specs:
|
14
|
+
pundit-before (0.0.1)
|
15
|
+
pundit (>= 2.0, < 3.0)
|
16
|
+
|
17
|
+
GEM
|
18
|
+
remote: https://rubygems.org/
|
19
|
+
specs:
|
20
|
+
activesupport (7.0.4)
|
21
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
22
|
+
i18n (>= 1.6, < 2)
|
23
|
+
minitest (>= 5.1)
|
24
|
+
tzinfo (~> 2.0)
|
25
|
+
ast (2.4.2)
|
26
|
+
coderay (1.1.3)
|
27
|
+
concurrent-ruby (1.1.10)
|
28
|
+
i18n (1.12.0)
|
29
|
+
concurrent-ruby (~> 1.0)
|
30
|
+
method_source (1.0.0)
|
31
|
+
minitest (5.15.0)
|
32
|
+
parallel (1.22.1)
|
33
|
+
parser (3.1.1.0)
|
34
|
+
ast (~> 2.4.1)
|
35
|
+
pry (0.14.1)
|
36
|
+
coderay (~> 1.1)
|
37
|
+
method_source (~> 1.0)
|
38
|
+
pundit (2.3.0)
|
39
|
+
activesupport (>= 3.0.0)
|
40
|
+
rainbow (3.1.1)
|
41
|
+
rake (13.0.6)
|
42
|
+
regexp_parser (2.2.1)
|
43
|
+
rexml (3.2.5)
|
44
|
+
rubocop (1.26.1)
|
45
|
+
parallel (~> 1.10)
|
46
|
+
parser (>= 3.1.0.0)
|
47
|
+
rainbow (>= 2.2.2, < 4.0)
|
48
|
+
regexp_parser (>= 1.8, < 3.0)
|
49
|
+
rexml
|
50
|
+
rubocop-ast (>= 1.16.0, < 2.0)
|
51
|
+
ruby-progressbar (~> 1.7)
|
52
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
53
|
+
rubocop-ast (1.16.0)
|
54
|
+
parser (>= 3.1.1.0)
|
55
|
+
rubocop-minitest (0.19.0)
|
56
|
+
rubocop (>= 0.90, < 2.0)
|
57
|
+
rubocop-rake (0.6.0)
|
58
|
+
rubocop (~> 1.0)
|
59
|
+
ruby-progressbar (1.11.0)
|
60
|
+
thor (1.2.1)
|
61
|
+
tzinfo (2.0.5)
|
62
|
+
concurrent-ruby (~> 1.0)
|
63
|
+
unicode-display_width (2.1.0)
|
64
|
+
|
65
|
+
PLATFORMS
|
66
|
+
arm64-darwin-21
|
67
|
+
x86_64-linux
|
68
|
+
|
69
|
+
DEPENDENCIES
|
70
|
+
appraisal!
|
71
|
+
minitest (~> 5.0)
|
72
|
+
pry (~> 0.14)
|
73
|
+
pundit-before!
|
74
|
+
rake (~> 13.0)
|
75
|
+
rubocop (~> 1.21)
|
76
|
+
rubocop-minitest (~> 0.19)
|
77
|
+
rubocop-rake (~> 0.6)
|
78
|
+
|
79
|
+
BUNDLED WITH
|
80
|
+
2.3.7
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Javier Aranda
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
# Pundit::Before
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
Adds `before` hook to pundit policy classes to resolve things like varvet/pundit#474. Inspired by action_policy
|
6
|
+
[pre-checks](https://actionpolicy.evilmartians.io/#/pre_checks).
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem "pundit-before"
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
```shell
|
19
|
+
bundle install
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Use `allow!` inside callback method or block to return `true` without evaluating `edit?` method defined in policy.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class UserPolicy < ApplicationPolicy
|
28
|
+
include Pundit::Before
|
29
|
+
|
30
|
+
before :check_admin
|
31
|
+
|
32
|
+
def edit?
|
33
|
+
false
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def check_admin
|
39
|
+
allow! if user.admin?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
UserPolicy.new(User.new(admin: true), record).edit? # => true
|
44
|
+
UserPolicy.new(User.new(admin: false), record).edit? # => false
|
45
|
+
```
|
46
|
+
|
47
|
+
Use `deny!` inside callback method or block to return `false` without evaluating `edit?` method defined in policy.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class UserPolicy < ApplicationPolicy
|
51
|
+
include Pundit::Before
|
52
|
+
|
53
|
+
before :check_admin
|
54
|
+
|
55
|
+
def edit?
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def check_admin
|
62
|
+
deny! unless user.admin?
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
UserPolicy.new(User.new(admin: true), record).edit? # => true
|
67
|
+
UserPolicy.new(User.new(admin: false), record).edit? # => false
|
68
|
+
```
|
69
|
+
|
70
|
+
Internally `before` hook is implemented as `ActiveSupport::Callbacks`, so the callback chain will halt if do any call to
|
71
|
+
`allow!` or `deny!` method. It's similar as Rails controller action filters works.
|
72
|
+
|
73
|
+
### block form
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class UserPolicy < ApplicationPolicy
|
77
|
+
include Pundit::Before
|
78
|
+
|
79
|
+
before do
|
80
|
+
allow! if user.admin?
|
81
|
+
end
|
82
|
+
|
83
|
+
def edit?
|
84
|
+
false
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
### skip before hook
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class UserPolicy < ApplicationPolicy
|
93
|
+
include Pundit::Before
|
94
|
+
|
95
|
+
before :check_admin
|
96
|
+
|
97
|
+
def edit?
|
98
|
+
false
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def check_admin
|
104
|
+
allow! if user.admin?
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class OperatorPolicy < UserPolicy
|
109
|
+
skip_before :check_admin
|
110
|
+
end
|
111
|
+
|
112
|
+
UserPolicy.new(User.new(admin: true), record).edit? # => true
|
113
|
+
OperatorPolicy.new(User.new(admin: true), record).edit? # => false
|
114
|
+
```
|
115
|
+
|
116
|
+
### using `only` modifier
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
class UserPolicy < ApplicationPolicy
|
120
|
+
include Pundit::Before
|
121
|
+
|
122
|
+
before :check_admin, only: :update?
|
123
|
+
|
124
|
+
def edit?
|
125
|
+
false
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
def check_admin
|
131
|
+
allow! if user.admin?
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
UserPolicy.new(User.new(admin: true), record).edit? # => false
|
136
|
+
```
|
137
|
+
|
138
|
+
### using `except` modifier
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
class UserPolicy < ApplicationPolicy
|
142
|
+
include Pundit::Before
|
143
|
+
|
144
|
+
before :check_admin, except: :edit?
|
145
|
+
|
146
|
+
def edit?
|
147
|
+
false
|
148
|
+
end
|
149
|
+
|
150
|
+
def destroy?
|
151
|
+
false
|
152
|
+
end
|
153
|
+
|
154
|
+
private
|
155
|
+
|
156
|
+
def check_admin
|
157
|
+
allow! if user.admin?
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
UserPolicy.new(User.new(admin: true), record).edit? # => false
|
162
|
+
UserPolicy.new(User.new(admin: true), record).destroy? # => true
|
163
|
+
```
|
164
|
+
|
165
|
+
### calling multiple methods
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
class UserPolicy < BasePolicy
|
169
|
+
before :check_presence, :check_admin
|
170
|
+
|
171
|
+
def edit?
|
172
|
+
false
|
173
|
+
end
|
174
|
+
|
175
|
+
private
|
176
|
+
|
177
|
+
def check_presence
|
178
|
+
deny! unless user.present?
|
179
|
+
end
|
180
|
+
|
181
|
+
def check_admin
|
182
|
+
allow! if user.admin?
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
UserPolicy.new(nil, record).edit? # => false
|
187
|
+
UserPolicy.new(User.new(admin: false), record).edit? # => false
|
188
|
+
UserPolicy.new(User.new(admin: true), record).edit? # => true
|
189
|
+
```
|
190
|
+
|
191
|
+
## Development
|
192
|
+
|
193
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
194
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
195
|
+
|
196
|
+
To install this gem onto your local machine, run `bin/rake install`.
|
197
|
+
|
198
|
+
To release a new version, update the version number in `version.rb`, and then run `bin/rake release`,
|
199
|
+
which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to
|
200
|
+
[rubygems.org](https://rubygems.org).
|
201
|
+
|
202
|
+
## License
|
203
|
+
|
204
|
+
Copyright © 2023 Javier Aranda. Released under the terms of the [MIT license](LICENSE).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal", github: "excid3/appraisal", branch: "fix-bundle-env"
|
6
|
+
gem "minitest", "~> 5.0"
|
7
|
+
gem "pry", "~> 0.14"
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
gem "rubocop", "~> 1.21"
|
10
|
+
gem "rubocop-minitest", "~> 0.19"
|
11
|
+
gem "rubocop-rake", "~> 0.6"
|
12
|
+
gem "pundit", "2.0"
|
13
|
+
|
14
|
+
gemspec path: "../"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal", github: "excid3/appraisal", branch: "fix-bundle-env"
|
6
|
+
gem "minitest", "~> 5.0"
|
7
|
+
gem "pry", "~> 0.14"
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
gem "rubocop", "~> 1.21"
|
10
|
+
gem "rubocop-minitest", "~> 0.19"
|
11
|
+
gem "rubocop-rake", "~> 0.6"
|
12
|
+
gem "pundit", "2.1"
|
13
|
+
|
14
|
+
gemspec path: "../"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal", github: "excid3/appraisal", branch: "fix-bundle-env"
|
6
|
+
gem "minitest", "~> 5.0"
|
7
|
+
gem "pry", "~> 0.14"
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
gem "rubocop", "~> 1.21"
|
10
|
+
gem "rubocop-minitest", "~> 0.19"
|
11
|
+
gem "rubocop-rake", "~> 0.6"
|
12
|
+
gem "pundit", "2.2"
|
13
|
+
|
14
|
+
gemspec path: "../"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal", github: "excid3/appraisal", branch: "fix-bundle-env"
|
6
|
+
gem "minitest", "~> 5.0"
|
7
|
+
gem "pry", "~> 0.14"
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
gem "rubocop", "~> 1.21"
|
10
|
+
gem "rubocop-minitest", "~> 0.19"
|
11
|
+
gem "rubocop-rake", "~> 0.6"
|
12
|
+
gem "pundit", "2.3"
|
13
|
+
|
14
|
+
gemspec path: "../"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pundit
|
4
|
+
module Before
|
5
|
+
module VERSION
|
6
|
+
MAJOR = 0
|
7
|
+
MINOR = 0
|
8
|
+
TINY = 1
|
9
|
+
PRE = nil
|
10
|
+
|
11
|
+
STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.version
|
15
|
+
VERSION::STRING
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.gem_version
|
19
|
+
Gem::Version.new VERSION::STRING
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/callbacks"
|
4
|
+
require "active_support/core_ext/array/wrap"
|
5
|
+
require "active_support/core_ext/module/redefine_method"
|
6
|
+
require "active_support/core_ext/object/blank"
|
7
|
+
|
8
|
+
require_relative "before/version"
|
9
|
+
|
10
|
+
module Pundit
|
11
|
+
module Before
|
12
|
+
# rubocop:disable Metrics/MethodLength
|
13
|
+
def self.included(base)
|
14
|
+
base.extend ClassMethods
|
15
|
+
base.include ActiveSupport::Callbacks
|
16
|
+
|
17
|
+
base.class_eval do
|
18
|
+
define_callbacks :_pundit_before, skip_after_callbacks_if_terminated: true
|
19
|
+
|
20
|
+
def self.method_added(method_name)
|
21
|
+
super
|
22
|
+
|
23
|
+
return unless method_name.to_s =~ /.*\?$/ && public_method_defined?(method_name)
|
24
|
+
return if @_pundit_before_running
|
25
|
+
|
26
|
+
@_pundit_before_running = true
|
27
|
+
|
28
|
+
old_method = instance_method(method_name)
|
29
|
+
|
30
|
+
redefine_method(method_name) do
|
31
|
+
@_pundit_before_result = nil
|
32
|
+
@_pundit_before_method = method_name
|
33
|
+
|
34
|
+
run_callbacks :_pundit_before
|
35
|
+
|
36
|
+
@_pundit_before_result.nil? ? old_method.bind(self).call : @_pundit_before_result
|
37
|
+
end
|
38
|
+
|
39
|
+
@_pundit_before_running = false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
# rubocop:enable Metrics/MethodLength
|
44
|
+
|
45
|
+
class CallbackFilter
|
46
|
+
def initialize(methods)
|
47
|
+
@methods = Array(methods).map(&:to_sym)
|
48
|
+
end
|
49
|
+
|
50
|
+
def match?(object)
|
51
|
+
@methods.include?(object.instance_variable_get(:@_pundit_before_method).to_sym)
|
52
|
+
end
|
53
|
+
|
54
|
+
alias after match?
|
55
|
+
alias before match?
|
56
|
+
alias around match?
|
57
|
+
end
|
58
|
+
|
59
|
+
module ClassMethods
|
60
|
+
def before(*method_names, **options, &block)
|
61
|
+
_normalize_callback_options(options)
|
62
|
+
|
63
|
+
if block_given?
|
64
|
+
set_callback :_pundit_before, :before, **options, &block
|
65
|
+
else
|
66
|
+
set_callback :_pundit_before, :before, *method_names, **options
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def skip_before(*method_names, **options)
|
71
|
+
_normalize_callback_options(options)
|
72
|
+
skip_callback :_pundit_before, :before, *method_names, **options
|
73
|
+
end
|
74
|
+
|
75
|
+
def _normalize_callback_options(options)
|
76
|
+
_normalize_callback_option(options, :only, :if)
|
77
|
+
_normalize_callback_option(options, :except, :unless)
|
78
|
+
end
|
79
|
+
|
80
|
+
def _normalize_callback_option(options, from, to)
|
81
|
+
return unless (from = options.delete(from))
|
82
|
+
|
83
|
+
from = CallbackFilter.new(from)
|
84
|
+
options[to] = Array(options[to]).unshift(from)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def allow!
|
89
|
+
@_pundit_before_result = true
|
90
|
+
throw :abort
|
91
|
+
end
|
92
|
+
|
93
|
+
def deny!
|
94
|
+
@_pundit_before_result = false
|
95
|
+
throw :abort
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pundit-before
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Javier Aranda
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pundit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
description:
|
34
|
+
email:
|
35
|
+
- javier.aranda.varo@gmail.com
|
36
|
+
executables: []
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files:
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
files:
|
42
|
+
- ".editorconfig"
|
43
|
+
- ".envrc"
|
44
|
+
- ".pryrc"
|
45
|
+
- ".rubocop.yml"
|
46
|
+
- ".tool-versions"
|
47
|
+
- Appraisals
|
48
|
+
- CHANGELOG.md
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- gemfiles/pundit_2.0.gemfile
|
55
|
+
- gemfiles/pundit_2.1.gemfile
|
56
|
+
- gemfiles/pundit_2.2.gemfile
|
57
|
+
- gemfiles/pundit_2.3.gemfile
|
58
|
+
- lib/pundit-before.rb
|
59
|
+
- lib/pundit/before.rb
|
60
|
+
- lib/pundit/before/version.rb
|
61
|
+
homepage: https://github.com/javierav/pundit-before
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata:
|
65
|
+
homepage_uri: https://github.com/javierav/pundit-before
|
66
|
+
source_code_uri: https://github.com/javierav/pundit-before/tree/v0.0.1
|
67
|
+
changelog_uri: https://github.com/javierav/pundit-before/blob/v0.0.1/CHANGELOG.md
|
68
|
+
rubygems_mfa_required: 'true'
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- "--charset=UTF-8"
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 2.7.0
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.4.1
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Adds before hook to pundit policy classes
|
89
|
+
test_files: []
|