Class: BeltController::Base
Constant Summary
ImplicitResponse::FRAMEWORK_IVARS
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
#cors_headers, #error_response, #handle_error_and_respond, #head, #html_response, #response_status, #success_response
Constructor Details
#initialize(event:, body:) ⇒ Base
Returns a new instance of Base.
116
117
118
119
120
|
# File 'lib/belt_controller/base.rb', line 116
def initialize(event:, body:)
@event = event
@raw_body = deep_transform_keys_to_snake_case(body || {})
@params = nil
end
|
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
18
19
20
|
# File 'lib/belt_controller/base.rb', line 18
def body
@body
end
|
#event ⇒ Object
Returns the value of attribute event.
18
19
20
|
# File 'lib/belt_controller/base.rb', line 18
def event
@event
end
|
Class Method Details
.after_action(method_name, only: nil, except: nil) ⇒ Object
41
42
43
|
# File 'lib/belt_controller/base.rb', line 41
def after_action(method_name, only: nil, except: nil)
after_actions << { method: method_name, only: only&.map(&:to_sym), except: except&.map(&:to_sym) }
end
|
.after_actions ⇒ Object
37
38
39
|
# File 'lib/belt_controller/base.rb', line 37
def after_actions
@after_actions ||= []
end
|
.all_after_actions ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/belt_controller/base.rb', line 80
def all_after_actions
if superclass.respond_to?(:all_after_actions)
superclass.all_after_actions + after_actions
else
after_actions
end
end
|
.all_before_actions ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/belt_controller/base.rb', line 72
def all_before_actions
if superclass.respond_to?(:all_before_actions)
superclass.all_before_actions + before_actions
else
before_actions
end
end
|
.all_rescue_handlers ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/belt_controller/base.rb', line 96
def all_rescue_handlers
if superclass.respond_to?(:all_rescue_handlers)
superclass.all_rescue_handlers.merge(rescue_handlers)
else
rescue_handlers
end
end
|
.all_skipped_before_actions ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/belt_controller/base.rb', line 88
def all_skipped_before_actions
if superclass.respond_to?(:all_skipped_before_actions)
superclass.all_skipped_before_actions + skipped_before_actions
else
skipped_before_actions
end
end
|
.before_action(method_name, only: nil, except: nil) ⇒ Object
33
34
35
|
# File 'lib/belt_controller/base.rb', line 33
def before_action(method_name, only: nil, except: nil)
before_actions << { method: method_name, only: only&.map(&:to_sym), except: except&.map(&:to_sym) }
end
|
.before_actions ⇒ Object
29
30
31
|
# File 'lib/belt_controller/base.rb', line 29
def before_actions
@before_actions ||= []
end
|
Implicit response format when the action does not call success_response /
error_response / html_response / render / head.
Inherits up the controller chain; falls back to Belt.configuration.default_format.
56
57
58
59
60
61
|
# File 'lib/belt_controller/base.rb', line 56
def default_format
return @default_format if defined?(@default_format)
return superclass.default_format if superclass.respond_to?(:default_format)
Belt.configuration.default_format
end
|
63
64
65
66
67
68
69
70
|
# File 'lib/belt_controller/base.rb', line 63
def default_format=(value)
format = value.to_sym
unless Belt::Configuration::VALID_FORMATS.include?(format)
raise ArgumentError, "default_format must be :json or :html (got #{value.inspect})"
end
@default_format = format
end
|
.rescue_from(*exceptions, with:) ⇒ Object
25
26
27
|
# File 'lib/belt_controller/base.rb', line 25
def rescue_from(*exceptions, with:)
exceptions.each { |e| rescue_handlers[e] = with }
end
|
.rescue_handlers ⇒ Object
21
22
23
|
# File 'lib/belt_controller/base.rb', line 21
def rescue_handlers
@rescue_handlers ||= {}
end
|
.skip_before_action(method_name, only: nil, except: nil) ⇒ Object
49
50
51
|
# File 'lib/belt_controller/base.rb', line 49
def skip_before_action(method_name, only: nil, except: nil)
skipped_before_actions << { method: method_name, only: only&.map(&:to_sym), except: except&.map(&:to_sym) }
end
|
.skipped_before_actions ⇒ Object
45
46
47
|
# File 'lib/belt_controller/base.rb', line 45
def skipped_before_actions
@skipped_before_actions ||= []
end
|
Instance Method Details
#action_name ⇒ Object
171
172
173
|
# File 'lib/belt_controller/base.rb', line 171
def action_name
@current_action
end
|
#admin? ⇒ Boolean
163
164
165
|
# File 'lib/belt_controller/base.rb', line 163
def admin?
user_groups.include?('Admin')
end
|
#authenticate! ⇒ Object
148
149
150
151
152
153
|
# File 'lib/belt_controller/base.rb', line 148
def authenticate!
user_id = event.dig('requestContext', 'authorizer', 'claims', 'sub')
raise Belt::AuthenticationError, 'Authentication required' unless user_id
@current_user_id = user_id
end
|
#current_user_id ⇒ Object
155
156
157
|
# File 'lib/belt_controller/base.rb', line 155
def current_user_id
@current_user_id ||= event.dig('requestContext', 'authorizer', 'claims', 'sub')
end
|
#dispatch(action_name) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/belt_controller/base.rb', line 130
def dispatch(action_name)
action_sym = action_name.to_sym
@current_action = action_sym
unless respond_to?(action_sym)
raise Belt::ActionNotFound, "The action '#{action_sym}' could not be found for #{self.class.name}"
end
run_before_actions(action_sym)
@__assigns_before = instance_variables
result = send(action_sym)
run_after_actions(action_sym)
finalize_response(result)
rescue StandardError => e
handle_exception(e)
end
|
#employee? ⇒ Boolean
167
168
169
|
# File 'lib/belt_controller/base.rb', line 167
def employee?
user_groups.include?('Employee')
end
|
#params ⇒ Object
122
123
124
|
# File 'lib/belt_controller/base.rb', line 122
def params
@params ||= build_params
end
|
#user_groups ⇒ Object
159
160
161
|
# File 'lib/belt_controller/base.rb', line 159
def user_groups
@user_groups ||=
end
|