Skip to content

Commit 2af46cd

Browse files
authored
feat(pundit): Add types for Pundit authorization (#974)
1 parent 1087491 commit 2af46cd

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

gems/pundit/.rubocop.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This configuration inherits from /.rubocop.yml.
2+
# You can configure RBS style of this gem.
3+
# This file is used on CI. It is configured to automatically
4+
# make rubocop suggestions on pull requests for this gem.
5+
# If you do not like the style enforcement, you should remove this file.
6+
inherit_from: ../../.rubocop.yml
7+
8+
##
9+
# If you want to customize the style, please consult with the gem reviewers.
10+
# You can see the list of cops at https://github.com/ksss/rubocop-on-rbs/blob/main/docs/modules/ROOT/pages/cops.adoc
11+
12+
RBS/Layout:
13+
Enabled: true
14+
15+
RBS/Lint:
16+
Enabled: true
17+
18+
RBS/Style:
19+
Enabled: true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
additional_gems:
2+
- actionpack

gems/pundit/2.5/_test/test.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require "pundit"
2+
require "action_controller"
3+
4+
class Post
5+
end
6+
7+
class PostPolicy
8+
def initialize(user, record)
9+
@user = user
10+
@record = record
11+
end
12+
13+
def update?
14+
true
15+
end
16+
17+
class Scope
18+
def initialize(user, scope)
19+
@user = user
20+
@scope = scope
21+
end
22+
23+
def resolve
24+
@scope
25+
end
26+
end
27+
end
28+
29+
class ApplicationController < ActionController::Base
30+
include Pundit::Authorization
31+
32+
def show
33+
post = Post.new
34+
authorize(post)
35+
authorize(post, :update?)
36+
policy(post)
37+
policy_scope(Post)
38+
39+
raise Pundit::NotAuthorizedError
40+
raise Pundit::NotDefinedError
41+
end
42+
end

gems/pundit/2.5/_test/test.rbs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Post
2+
end
3+
4+
class PostPolicy
5+
@user: untyped
6+
@record: untyped
7+
8+
def initialize: (untyped user, untyped record) -> void
9+
10+
def update?: () -> bool
11+
12+
class Scope
13+
@user: untyped
14+
@scope: untyped
15+
16+
def initialize: (untyped user, untyped scope) -> void
17+
18+
def resolve: () -> untyped
19+
end
20+
end
21+
22+
class ApplicationController < ActionController::Base
23+
include Pundit::Authorization
24+
25+
def show: () -> untyped
26+
end

gems/pundit/2.5/pundit.rbs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Pundit
2+
class NotAuthorizedError < StandardError
3+
end
4+
5+
class NotDefinedError < StandardError
6+
end
7+
8+
module Authorization
9+
def authorize: [T] (T record, ?Symbol? query, ?policy_class: untyped?) -> T
10+
11+
def policy: (untyped record) -> untyped
12+
13+
def policy_scope: [T] (T scope, ?policy_scope_class: untyped?) -> T?
14+
15+
def policy_scope!: [T] (T scope, ?policy_scope_class: untyped?) -> T
16+
17+
def verify_authorized: () -> void
18+
19+
def verify_policy_scoped: () -> void
20+
21+
def skip_authorization: () -> void
22+
23+
def skip_policy_scope: () -> void
24+
end
25+
end

0 commit comments

Comments
 (0)