Class: Avo::BaseAction

Inherits:
Object
  • Object
show all
Includes:
Concerns::HasActionStimulusControllers, Concerns::HasItems, Concerns::Hydration
Defined in:
lib/avo/base_action.rb

Direct Known Subclasses

Divider

Instance Attribute Summary collapse

Attributes included from Concerns::HasItems

#items_holder

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Hydration

#hydrate

Methods included from Concerns::HasActionStimulusControllers

#get_stimulus_controllers

Methods included from Concerns::HasItems

#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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/avo/base_action.rb', line 106

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_streamsObject (readonly)

Returns the value of attribute appended_turbo_streams.



26
27
28
# File 'lib/avo/base_action.rb', line 26

def appended_turbo_streams
  @appended_turbo_streams
end

#argumentsObject (readonly)

Returns the value of attribute arguments.



24
25
26
# File 'lib/avo/base_action.rb', line 24

def arguments
  @arguments
end

#iconObject (readonly)

Returns the value of attribute icon.



25
26
27
# File 'lib/avo/base_action.rb', line 25

def icon
  @icon
end

#recordObject

Returns the value of attribute record.



21
22
23
# File 'lib/avo/base_action.rb', line 21

def record
  @record
end

#records_to_reloadObject (readonly)

Returns the value of attribute records_to_reload.



27
28
29
# File 'lib/avo/base_action.rb', line 27

def records_to_reload
  @records_to_reload
end

#resourceObject

Returns the value of attribute resource.



22
23
24
# File 'lib/avo/base_action.rb', line 22

def resource
  @resource
end

#responseObject

Returns the value of attribute response.



20
21
22
# File 'lib/avo/base_action.rb', line 20

def response
  @response
end

#userObject

Returns the value of attribute user.



23
24
25
# File 'lib/avo/base_action.rb', line 23

def user
  @user
end

#viewObject

Returns the value of attribute view.



19
20
21
# File 'lib/avo/base_action.rb', line 19

def view
  @view
end

Class Method Details

.decode_arguments(arguments) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/avo/base_action.rb', line 82

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.



73
74
75
76
77
78
79
80
# File 'lib/avo/base_action.rb', line 73

def encode_arguments(arguments)
  return if arguments.blank?

  Base64.encode64 Avo::Services::EncryptionService.encrypt(
    message: arguments,
    purpose: :action_arguments
  )
end

.form_data_attributesObject



44
45
46
47
48
49
# File 'lib/avo/base_action.rb', line 44

def form_data_attributes
  {
    turbo: turbo,
    turbo_frame: :_top
  }.compact
end


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/avo/base_action.rb', line 55

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_paramObject



51
52
53
# File 'lib/avo/base_action.rb', line 51

def to_param
  to_s
end

Instance Method Details

#action_nameObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/avo/base_action.rb', line 92

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



356
357
358
# File 'lib/avo/base_action.rb', line 356

def append_to_response(turbo_stream)
  @appended_turbo_streams = turbo_stream
end

#authorized?Boolean

Returns:

  • (Boolean)


346
347
348
349
350
351
352
353
354
# File 'lib/avo/base_action.rb', line 346

def authorized?
  Avo::ExecutionContext.new(
    target: authorize,
    action: self,
    resource: @resource,
    view: @view,
    arguments: arguments
  ).handle
end

#cancel_button_labelObject



146
147
148
149
150
151
152
153
154
# File 'lib/avo/base_action.rb', line 146

def cancel_button_label
  Avo::ExecutionContext.new(
    target: self.class.cancel_button_label,
    resource: @resource,
    record: @record,
    view: @view,
    arguments: @arguments
  ).handle
end

#close_modalObject Also known as: do_nothing



255
256
257
258
259
# File 'lib/avo/base_action.rb', line 255

def close_modal
  response[:type] = :close_modal

  self
end

#confirm_button_labelObject



156
157
158
159
160
161
162
163
164
# File 'lib/avo/base_action.rb', line 156

def confirm_button_label
  Avo::ExecutionContext.new(
    target: self.class.confirm_button_label,
    resource: @resource,
    record: @record,
    view: @view,
    arguments: @arguments
  ).handle
end

#current_userObject



31
32
33
# File 'lib/avo/base_action.rb', line 31

def current_user
  Avo::Current.user
end

#disabled?Boolean

Returns:

  • (Boolean)


364
365
366
# File 'lib/avo/base_action.rb', line 364

def disabled?
  !enabled?
end

#download(path, filename) ⇒ Object



338
339
340
341
342
343
344
# File 'lib/avo/base_action.rb', line 338

def download(path, filename)
  response[:type] = :download
  response[:path] = path
  response[:filename] = filename

  self
end

#enabled?Boolean

Returns:

  • (Boolean)


360
361
362
# File 'lib/avo/base_action.rb', line 360

def enabled?
  self.class.standalone || @record&.persisted?
end

#error(text) ⇒ Object



231
232
233
234
235
# File 'lib/avo/base_action.rb', line 231

def error(text)
  add_message text, :error

  self
end

#fieldsObject

Blank method



133
134
# File 'lib/avo/base_action.rb', line 133

def fields
end

#get_messageObject



136
137
138
139
140
141
142
143
144
# File 'lib/avo/base_action.rb', line 136

def get_message
  Avo::ExecutionContext.new(
    target: self.class.message,
    resource: @resource,
    record: @record,
    view: @view,
    arguments: @arguments
  ).handle
end

#handle_action(**args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/avo/base_action.rb', line 166

def handle_action(**args)
  processed_fields = if args[:fields].present?
    # Fetching the field definitions and not the actual fields (get_fields) because they will break if the user uses a `visible` block and adds a condition using the `params` variable. The params are different in the show method and the handle method.
    action_fields = get_field_definitions.map do |field|
      field.hydrate(resource: @resource)

      [field.id, field]
    end.to_h

    # For some fields, like belongs_to, the id and database_id differ (user vs user_id).
    # That's why we need to fetch the database_id for when we process the action.
    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

#inform(text) ⇒ Object



237
238
239
240
241
# File 'lib/avo/base_action.rb', line 237

def inform(text)
  add_message text, :info

  self
end

#keep_modal_openObject



249
250
251
252
253
# File 'lib/avo/base_action.rb', line 249

def keep_modal_open
  response[:type] = :keep_modal_open

  self
end


330
331
332
333
334
335
336
# File 'lib/avo/base_action.rb', line 330

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



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/avo/base_action.rb', line 271

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

#reloadObject



283
284
285
286
287
# File 'lib/avo/base_action.rb', line 283

def reload
  response[:type] = :reload

  self
end

#reload_record(records) ⇒ Object Also known as: reload_records



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/avo/base_action.rb', line 289

def reload_record(records)
  # Force close modal to avoid default redirect to
  # Redirect is 100% not wanted when using reload_record
  close_modal

  @records_to_reload = Array(records)

  append_to_response -> {
    table_row_components = []
    header_fields = []

    @action.records_to_reload.each do |record|
      resource = @resource.dup
      resource.hydrate(record:, view: :index)
      resource.detect_fields
      row_fields = resource.get_fields(only_root: true)
      header_fields.concat row_fields
      table_row_components << resource.resolve_component(Avo::Index::TableRowComponent).new(
        resource: resource,
        header_fields: row_fields.map(&:table_header_label),
        fields: row_fields
      )
    end

    header_fields.uniq!(&:table_header_label)

    header_fields_ids = header_fields.map(&:table_header_label)

    table_row_components.map.with_index do |table_row_component, index|
      table_row_component.header_fields = header_fields_ids
      turbo_stream.replace(
        "avo/index/table_row_component_#{@action.records_to_reload[index].to_param}",
        table_row_component
      )
    end
  }
end

#silentObject

Add a placeholder silent message from when a user wants to do a redirect action or something similar



265
266
267
268
269
# File 'lib/avo/base_action.rb', line 265

def silent
  add_message nil, :silent

  self
end

#succeed(text) ⇒ Object



225
226
227
228
229
# File 'lib/avo/base_action.rb', line 225

def succeed(text)
  add_message text, :success

  self
end

#visible_in_view(parent_resource: nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/avo/base_action.rb', line 203

def visible_in_view(parent_resource: nil)
  return false unless authorized?

  if visible.blank?
    # Hide on the :new view by default
    return false if view.new?

    # Show on all other views
    return true
  end

  # Run the visible block if available
  Avo::ExecutionContext.new(
    target: visible,
    params: params,
    parent_resource: parent_resource,
    resource: @resource,
    view: @view,
    arguments: arguments
  ).handle
end

#warn(text) ⇒ Object



243
244
245
246
247
# File 'lib/avo/base_action.rb', line 243

def warn(text)
  add_message text, :warning

  self
end