-
Notifications
You must be signed in to change notification settings - Fork 15
/
banken.rb
89 lines (71 loc) · 2.17 KB
/
banken.rb
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require "active_support/concern"
require "active_support/core_ext/string/inflections"
require "active_support/core_ext/object/blank"
require "active_support/core_ext/module/introspection"
require "banken/version"
require "banken/error"
require "banken/loyalty_finder"
module Banken
extend ActiveSupport::Concern
included do
if respond_to?(:helper_method)
helper_method :loyalty
helper_method :banken_user
end
if respond_to?(:hide_action)
hide_action :permitted_attributes
hide_action :loyalty
hide_action :banken_user
hide_action :skip_authorization
hide_action :verify_authorized
end
end
class << self
def loyalty!(controller_name, user, record=nil)
LoyaltyFinder.new(controller_name).loyalty!.new(user, record)
end
end
def authorize!(record=nil)
@_banken_authorization_performed = true
loyalty = loyalty(record)
unless loyalty.public_send(banken_query_name)
raise NotAuthorizedError.new(controller: banken_controller_name, query: banken_query_name, loyalty: loyalty)
end
true
end
def permitted_attributes(record)
name = record.class.to_s.demodulize.underscore
params.require(name).permit(loyalty(record).permitted_attributes)
end
def loyalty(record=nil, controller_name=nil)
controller_name = banken_controller_name unless controller_name
Banken.loyalty!(controller_name, banken_user, record)
end
def banken_user
current_user
end
def skip_authorization
@_banken_authorization_performed = true
end
def verify_authorized
raise AuthorizationNotPerformedError unless banken_authorization_performed?
end
def banken_authorization_performed?
!!@_banken_authorization_performed
end
# @deprecated Use banken_authorization_performed? instead.
def banken_loyalty_authorized?
ActiveSupport::Deprecation.warn('banken_loyalty_authorized? is deprecated, use banken_authorization_performed? instead.')
banken_authorization_performed?
end
private
def banken_action_name
params[:action]
end
def banken_controller_name
params[:controller]
end
def banken_query_name
"#{banken_action_name}?"
end
end