Class: Avo::BaseAction
Constant Summary
collapse
- DATA_ATTRIBUTES =
{turbo_frame: Avo::MODAL_FRAME_ID}
Instance Attribute Summary collapse
#items_holder
Class Method Summary
collapse
Instance Method Summary
collapse
-
#action_name ⇒ Object
-
#append_to_response(turbo_stream) ⇒ Object
-
#authorized? ⇒ Boolean
-
#cancel_button_label ⇒ Object
-
#close_modal ⇒ Object
(also: #do_nothing)
-
#confirm_button_label ⇒ Object
-
#confirmation? ⇒ Boolean
-
#current_user ⇒ Object
-
#disabled? ⇒ Boolean
-
#download(path, filename) ⇒ Object
-
#enabled? ⇒ Boolean
-
#error(text, timeout: nil) ⇒ Object
-
#fields ⇒ Object
-
#get_description ⇒ Object
-
#get_message ⇒ Object
-
#handle_action(**args) ⇒ Object
-
#inform(text, timeout: nil) ⇒ Object
-
#initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: "tabler/outline/player-play", query: nil, index_query: nil) ⇒ BaseAction
constructor
A new instance of BaseAction.
-
#keep_modal_open ⇒ Object
-
#navigate_to_action(action, **kwargs) ⇒ Object
-
#redirect_to(path = nil, **args, &block) ⇒ Object
-
#reload ⇒ Object
-
#reload_record(records) ⇒ Object
(also: #reload_records)
-
#silent ⇒ Object
Add a placeholder silent message from when a user wants to do a redirect action or something similar.
-
#succeed(text, timeout: nil) ⇒ Object
-
#visible_in_view(parent_resource: nil) ⇒ Object
-
#warn(text, timeout: nil) ⇒ Object
#hydrate
#get_stimulus_controllers
#get_field, #get_field_definitions, #get_fields, #get_items, #get_preview_fields, #invalid_fields, #is_empty?, #items, #items_with_standalone_fields_wrapped_in_cards, #only_fields, #tab_groups, #visible_items
Constructor Details
#initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: "tabler/outline/player-play", query: nil, index_query: nil) ⇒ BaseAction
Returns a new instance of BaseAction.
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/avo/base_action.rb', line 122
def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: "tabler/outline/player-play", query: nil, index_query: nil)
@record = record
@resource = resource
@user = user
@view = Avo::ViewInquirer.new(view)
@icon = icon
@arguments = Avo::ExecutionContext.new(
target: arguments,
resource: resource,
record: record
).handle.with_indifferent_access
@query = query
@index_query = index_query
self.class.message ||= I18n.t("avo.are_you_sure_you_want_to_run_this_option")
self.class.confirm_button_label ||= I18n.t("avo.run")
self.class.cancel_button_label ||= I18n.t("avo.cancel")
self.items_holder = Avo::Resources::Items::Holder.new
@response ||= {}
@response[:messages] = []
end
|
Instance Attribute Details
#appended_turbo_streams ⇒ Object
Returns the value of attribute appended_turbo_streams.
36
37
38
|
# File 'lib/avo/base_action.rb', line 36
def appended_turbo_streams
@appended_turbo_streams
end
|
#arguments ⇒ Object
Returns the value of attribute arguments.
34
35
36
|
# File 'lib/avo/base_action.rb', line 34
def arguments
@arguments
end
|
#icon ⇒ Object
Returns the value of attribute icon.
35
36
37
|
# File 'lib/avo/base_action.rb', line 35
def icon
@icon
end
|
#query ⇒ Object
Returns the value of attribute query.
38
39
40
|
# File 'lib/avo/base_action.rb', line 38
def query
@query
end
|
#record ⇒ Object
Returns the value of attribute record.
31
32
33
|
# File 'lib/avo/base_action.rb', line 31
def record
@record
end
|
#records_to_reload ⇒ Object
Returns the value of attribute records_to_reload.
37
38
39
|
# File 'lib/avo/base_action.rb', line 37
def records_to_reload
@records_to_reload
end
|
#resource ⇒ Object
Returns the value of attribute resource.
32
33
34
|
# File 'lib/avo/base_action.rb', line 32
def resource
@resource
end
|
#response ⇒ Object
Returns the value of attribute response.
30
31
32
|
# File 'lib/avo/base_action.rb', line 30
def response
@response
end
|
#user ⇒ Object
Returns the value of attribute user.
33
34
35
|
# File 'lib/avo/base_action.rb', line 33
def user
@user
end
|
#view ⇒ Object
Returns the value of attribute view.
29
30
31
|
# File 'lib/avo/base_action.rb', line 29
def view
@view
end
|
Class Method Details
.decode_arguments(arguments) ⇒ Object
97
98
99
100
101
102
103
104
|
# File 'lib/avo/base_action.rb', line 97
def decode_arguments(arguments)
return if arguments.blank?
Avo::Services::EncryptionService.decrypt(
message: Base64.decode64(arguments),
purpose: :action_arguments
)
end
|
.encode_arguments(arguments) ⇒ Object
Encrypt the arguments so we can pass sensible data as a query param. EncryptionService can generate special characters that can break the URL. We use Base64 to encode the encrypted string so we can safely pass it as a query param and don’t break the URL.
88
89
90
91
92
93
94
95
|
# File 'lib/avo/base_action.rb', line 88
def encode_arguments(arguments)
return if arguments.blank?
Base64.encode64 Avo::Services::EncryptionService.encrypt(
message: arguments,
purpose: :action_arguments
)
end
|
55
56
57
58
59
60
|
# File 'lib/avo/base_action.rb', line 55
def form_data_attributes
{
turbo: turbo,
turbo_frame: :_top
}.compact
end
|
.link_arguments(resource:, arguments: {}, **args) ⇒ Object
81
82
83
|
# File 'lib/avo/base_action.rb', line 81
def link_arguments(resource:, arguments: {}, **args)
[path(resource:, arguments:, **args), DATA_ATTRIBUTES]
end
|
.path(resource:, arguments: {}, **args) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/avo/base_action.rb', line 66
def path(resource:, arguments: {}, **args)
Avo::Services::URIService.parse(resource.record&.to_param.present? ? resource.record_path : resource.records_path)
.append_paths("actions")
.append_query(
**{
action_id: to_param,
arguments: encode_arguments(arguments),
resource_view: resource.view,
view_type: resource.view_type,
**args
}.compact
)
.to_s
end
|
.to_param ⇒ Object
62
63
64
|
# File 'lib/avo/base_action.rb', line 62
def to_param
to_s
end
|
Instance Method Details
#action_name ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/avo/base_action.rb', line 107
def action_name
if name.present?
return Avo::ExecutionContext.new(
target: name,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments,
query: @query
).handle
end
self.class.to_s.demodulize.underscore.humanize(keep_id_suffix: true)
end
|
#append_to_response(turbo_stream) ⇒ Object
349
350
351
|
# File 'lib/avo/base_action.rb', line 349
def append_to_response(turbo_stream)
@appended_turbo_streams = turbo_stream
end
|
#authorized? ⇒ Boolean
339
340
341
342
343
344
345
346
347
|
# File 'lib/avo/base_action.rb', line 339
def authorized?
Avo::ExecutionContext.new(
target: authorize,
action: self,
resource: @resource,
view: @view,
arguments: arguments
).handle
end
|
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/avo/base_action.rb', line 171
def cancel_button_label
Avo::ExecutionContext.new(
target: self.class.cancel_button_label,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments,
query: @query
).handle
end
|
#close_modal ⇒ Object
Also known as:
do_nothing
271
272
273
274
275
|
# File 'lib/avo/base_action.rb', line 271
def close_modal
response[:type] = :close_modal
self
end
|
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/avo/base_action.rb', line 182
def confirm_button_label
Avo::ExecutionContext.new(
target: self.class.confirm_button_label,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments,
query: @query
).handle
end
|
#confirmation? ⇒ Boolean
361
362
363
364
365
366
367
368
369
|
# File 'lib/avo/base_action.rb', line 361
def confirmation?
Avo::ExecutionContext.new(
target: confirmation,
action: self,
resource: @resource,
view: @view,
arguments:
).handle
end
|
#current_user ⇒ Object
42
43
44
|
# File 'lib/avo/base_action.rb', line 42
def current_user
Avo::Current.user
end
|
#disabled? ⇒ Boolean
357
358
359
|
# File 'lib/avo/base_action.rb', line 357
def disabled?
!enabled?
end
|
#download(path, filename) ⇒ Object
331
332
333
334
335
336
337
|
# File 'lib/avo/base_action.rb', line 331
def download(path, filename)
response[:type] = :download
response[:path] = path
response[:filename] = filename
self
end
|
#enabled? ⇒ Boolean
353
354
355
|
# File 'lib/avo/base_action.rb', line 353
def enabled?
self.class.standalone || @record&.to_param.present?
end
|
#error(text, timeout: nil) ⇒ Object
247
248
249
250
251
|
# File 'lib/avo/base_action.rb', line 247
def error(text, timeout: nil)
add_message text, :error, timeout: timeout
self
end
|
#fields ⇒ Object
146
147
|
# File 'lib/avo/base_action.rb', line 146
def fields
end
|
#get_description ⇒ Object
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/avo/base_action.rb', line 149
def get_description
Avo::ExecutionContext.new(
target: self.class.description,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments,
query: @query
).handle
end
|
#get_message ⇒ Object
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/avo/base_action.rb', line 160
def get_message
Avo::ExecutionContext.new(
target: self.class.message,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments,
query: @query
).handle
end
|
#handle_action(**args) ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/avo/base_action.rb', line 193
def handle_action(**args)
processed_fields = if args[:fields].present?
action_fields = get_field_definitions.map do |field|
field.hydrate(resource: @resource)
[field.id, field]
end.to_h
action_fields_by_database_id = action_fields.map do |id, value|
[value.database_id.to_sym, value]
end.to_h
args[:fields].to_unsafe_h.map do |name, value|
field = action_fields_by_database_id[name.to_sym]
next if field.blank?
[name, field.resolve_attribute(value)]
end.reject(&:blank?).to_h
else
{}
end
handle(
fields: processed_fields.with_indifferent_access,
current_user: args[:current_user],
resource: args[:resource],
records: args[:query],
query: args[:query]
)
self
end
|
253
254
255
256
257
|
# File 'lib/avo/base_action.rb', line 253
def inform(text, timeout: nil)
add_message text, :info, timeout: timeout
self
end
|
#keep_modal_open ⇒ Object
265
266
267
268
269
|
# File 'lib/avo/base_action.rb', line 265
def keep_modal_open
response[:type] = :keep_modal_open
self
end
|
#navigate_to_action(action, **kwargs) ⇒ Object
323
324
325
326
327
328
329
|
# File 'lib/avo/base_action.rb', line 323
def navigate_to_action(action, **kwargs)
response[:type] = :navigate_to_action
response[:action] = action
response[:navigate_to_action_args] = kwargs
self
end
|
#redirect_to(path = nil, **args, &block) ⇒ Object
287
288
289
290
291
292
293
294
295
296
297
|
# File 'lib/avo/base_action.rb', line 287
def redirect_to(path = nil, **args, &block)
response[:type] = :redirect
response[:redirect_args] = args
response[:path] = if block.present?
block
else
path
end
self
end
|
#reload ⇒ Object
299
300
301
302
303
|
# File 'lib/avo/base_action.rb', line 299
def reload
response[:type] = :reload
self
end
|
#reload_record(records) ⇒ Object
Also known as:
reload_records
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'lib/avo/base_action.rb', line 305
def reload_record(records)
close_modal
@records_to_reload = Array(records)
case @resource.view_type.to_sym
when :table, :map
reload_row_items
when :grid
reload_grid_items
end
end
|
#silent ⇒ Object
Add a placeholder silent message from when a user wants to do a redirect action or something similar
281
282
283
284
285
|
# File 'lib/avo/base_action.rb', line 281
def silent
add_message nil, :silent
self
end
|
#succeed(text, timeout: nil) ⇒ Object
241
242
243
244
245
|
# File 'lib/avo/base_action.rb', line 241
def succeed(text, timeout: nil)
add_message text, :success, timeout: timeout
self
end
|
#visible_in_view(parent_resource: nil) ⇒ Object
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/avo/base_action.rb', line 230
def visible_in_view(parent_resource: nil)
Avo::ExecutionContext.new(
target: visible,
params: params,
parent_resource: parent_resource,
resource: @resource,
view: @view,
arguments: arguments
).handle && authorized?
end
|
#warn(text, timeout: nil) ⇒ Object
259
260
261
262
263
|
# File 'lib/avo/base_action.rb', line 259
def warn(text, timeout: nil)
add_message text, :warning, timeout: timeout
self
end
|