-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpundit.rb
More file actions
54 lines (38 loc) · 1.07 KB
/
pundit.rb
File metadata and controls
54 lines (38 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true
gem 'pundit'
file 'app/policies/application_policy.rb', <<~POLICY
# frozen_string_literal: true
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def index? = false
def show? = false
def create? = false
def new? = create?
def update? = false
def edit? = update?
def destroy? = false
class Scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve = raise NotImplementedError, "You must define #resolve in \#{self.class}"
private
attr_reader :user, :scope
end
end
POLICY
inject_into_class 'app/controllers/application_controller.rb', 'ApplicationController', <<~RUBY
include Pundit::Authorization
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
private
def user_not_authorized
flash[:alert] = "You are not authorized to perform this action."
redirect_back fallback_location: root_path
end
RUBY
say '✅ Pundit configured'