Class: Avo::BaseAction
Instance Attribute Summary collapse
#items_holder
Class Method Summary
collapse
Instance Method Summary
collapse
#get_stimulus_controllers
#get_field, #get_field_definitions, #get_fields, #get_items, #get_preview_fields, #invalid_fields, #is_empty?, #items, #only_fields, #tab_groups, #visible_items
Constructor Details
#initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: :play) ⇒ BaseAction
Returns a new instance of BaseAction.
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/avo/base_action.rb', line 103
def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: :play)
@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
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] = []
if may_download_file.present?
puts "[Avo->] WARNING! Since version 3.2.2 'may_download_file' is unecessary and deprecated on actions. Can be safely removed from #{self.class.name}"
end
end
|
Instance Attribute Details
#appended_turbo_streams ⇒ Object
Returns the value of attribute appended_turbo_streams.
24
25
26
|
# File 'lib/avo/base_action.rb', line 24
def appended_turbo_streams
@appended_turbo_streams
end
|
#arguments ⇒ Object
Returns the value of attribute arguments.
22
23
24
|
# File 'lib/avo/base_action.rb', line 22
def arguments
@arguments
end
|
#icon ⇒ Object
Returns the value of attribute icon.
23
24
25
|
# File 'lib/avo/base_action.rb', line 23
def icon
@icon
end
|
#record ⇒ Object
Returns the value of attribute record.
19
20
21
|
# File 'lib/avo/base_action.rb', line 19
def record
@record
end
|
#resource ⇒ Object
Returns the value of attribute resource.
20
21
22
|
# File 'lib/avo/base_action.rb', line 20
def resource
@resource
end
|
#response ⇒ Object
Returns the value of attribute response.
18
19
20
|
# File 'lib/avo/base_action.rb', line 18
def response
@response
end
|
#user ⇒ Object
Returns the value of attribute user.
21
22
23
|
# File 'lib/avo/base_action.rb', line 21
def user
@user
end
|
#view ⇒ Object
Returns the value of attribute view.
17
18
19
|
# File 'lib/avo/base_action.rb', line 17
def view
@view
end
|
Class Method Details
.decode_arguments(arguments) ⇒ Object
79
80
81
82
83
84
85
86
|
# File 'lib/avo/base_action.rb', line 79
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.
70
71
72
73
74
75
76
77
|
# File 'lib/avo/base_action.rb', line 70
def encode_arguments(arguments)
return if arguments.blank?
Base64.encode64 Avo::Services::EncryptionService.encrypt(
message: arguments,
purpose: :action_arguments
)
end
|
41
42
43
44
45
46
|
# File 'lib/avo/base_action.rb', line 41
def form_data_attributes
{
turbo: turbo,
turbo_frame: :_top
}.compact
end
|
.link_arguments(resource:, arguments: {}, **args) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/avo/base_action.rb', line 52
def link_arguments(resource:, arguments: {}, **args)
path = Avo::Services::URIService.parse(resource.record&.persisted? ? resource.record_path : resource.records_path)
.append_paths("actions")
.append_query(
**{
action_id: to_param,
arguments: encode_arguments(arguments),
**args
}.compact
)
.to_s
[path, {turbo_frame: Avo::MODAL_FRAME_ID}]
end
|
.to_param ⇒ Object
48
49
50
|
# File 'lib/avo/base_action.rb', line 48
def to_param
to_s
end
|
Instance Method Details
#action_name ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/avo/base_action.rb', line 89
def action_name
if name.present?
return Avo::ExecutionContext.new(
target: name,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
).handle
end
self.class.to_s.demodulize.underscore.humanize(keep_id_suffix: true)
end
|
#append_to_response(turbo_stream) ⇒ Object
291
292
293
|
# File 'lib/avo/base_action.rb', line 291
def append_to_response(turbo_stream)
@appended_turbo_streams = turbo_stream
end
|
#authorized? ⇒ Boolean
281
282
283
284
285
286
287
288
289
|
# File 'lib/avo/base_action.rb', line 281
def authorized?
Avo::ExecutionContext.new(
target: authorize,
action: self,
resource: @resource,
view: @view,
arguments: arguments
).handle
end
|
#close_modal ⇒ Object
Also known as:
do_nothing
232
233
234
235
236
|
# File 'lib/avo/base_action.rb', line 232
def close_modal
response[:type] = :close_modal
self
end
|
#current_user ⇒ Object
28
29
30
|
# File 'lib/avo/base_action.rb', line 28
def current_user
Avo::Current.user
end
|
#download(path, filename) ⇒ Object
273
274
275
276
277
278
279
|
# File 'lib/avo/base_action.rb', line 273
def download(path, filename)
response[:type] = :download
response[:path] = path
response[:filename] = filename
self
end
|
#error(text) ⇒ Object
208
209
210
211
212
|
# File 'lib/avo/base_action.rb', line 208
def error(text)
add_message text, :error
self
end
|
#fields ⇒ Object
130
131
|
# File 'lib/avo/base_action.rb', line 130
def fields
end
|
#get_message ⇒ Object
133
134
135
136
137
138
139
140
141
|
# File 'lib/avo/base_action.rb', line 133
def get_message
Avo::ExecutionContext.new(
target: self.class.message,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
).handle
end
|
#handle_action(**args) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/avo/base_action.rb', line 143
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
|
214
215
216
217
218
|
# File 'lib/avo/base_action.rb', line 214
def inform(text)
add_message text, :info
self
end
|
#keep_modal_open ⇒ Object
226
227
228
229
230
|
# File 'lib/avo/base_action.rb', line 226
def keep_modal_open
response[:type] = :keep_modal_open
self
end
|
#navigate_to_action(action, **kwargs) ⇒ Object
265
266
267
268
269
270
271
|
# File 'lib/avo/base_action.rb', line 265
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
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/avo/base_action.rb', line 247
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
259
260
261
262
263
|
# File 'lib/avo/base_action.rb', line 259
def reload
response[:type] = :reload
self
end
|
#silent ⇒ Object
Add a placeholder silent message from when a user wants to do a redirect action or something similar
241
242
243
244
245
|
# File 'lib/avo/base_action.rb', line 241
def silent
add_message nil, :silent
self
end
|
#succeed(text) ⇒ Object
202
203
204
205
206
|
# File 'lib/avo/base_action.rb', line 202
def succeed(text)
add_message text, :success
self
end
|
#visible_in_view(parent_resource: nil) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/avo/base_action.rb', line 180
def visible_in_view(parent_resource: nil)
return false unless authorized?
if visible.blank?
return false if view.new?
return true
end
Avo::ExecutionContext.new(
target: visible,
params: params,
parent_resource: parent_resource,
resource: @resource,
view: @view,
arguments: arguments
).handle
end
|
#warn(text) ⇒ Object
220
221
222
223
224
|
# File 'lib/avo/base_action.rb', line 220
def warn(text)
add_message text, :warning
self
end
|