Customization
Actions can be customized in several ways to enhance the user experience. You can modify the action's display name, confirmation message, button labels, and confirmation behavior between other things.
There are 2 types of customization, visual and behavioral.
Visual customization
Visual customization is the process of modifying the action's appearance. This includes changing the action's name, message and button labels.
All visual customization options can be set as a string or a block.
The blocks are executed using Avo::ExecutionContext
. Within these blocks, you gain access to:
- All attributes of
Avo::ExecutionContext
resource
- The current resource instancerecord
- The current recordview
- The current viewarguments
- Any passed argumentsquery
- The current query parameters
-> name
The name
option is used to change the action's display name.
# app/avo/actions/release_fish.rb
class Avo::Actions::ReleaseFish < Avo::BaseAction
self.name = "Release fish"
# Or as a block
self.name = -> {
record.present? ? "Release #{record.name}?" : "Release fish"
}
end
-> message
The message
option is used to change the action's confirmation message.
# app/avo/actions/release_fish.rb
class Avo::Actions::ReleaseFish < Avo::BaseAction
self.message = "Are you sure you want to release the fish?"
# Or as a block
self.message = -> {
if resource.record.present?
"Are you sure you want to release the #{resource.record.name}?"
else
"Are you sure you want to release the fish?"
end
}
end
-> confirm_button_label
The confirm_button_label
option is used to change the action's confirmation button label.
# app/avo/actions/release_fish.rb
class Avo::Actions::ReleaseFish < Avo::BaseAction
self.confirm_button_label = "Release fish"
# Or as a block
self.confirm_button_label = -> {
if resource.record.present?
"Release #{resource.record.name}"
else
"Release fish"
end
}
end
-> cancel_button_label
The cancel_button_label
option is used to change the action's cancel button label.
# app/avo/actions/release_fish.rb
class Avo::Actions::ReleaseFish < Avo::BaseAction
self.cancel_button_label = "Cancel release"
# Or as a block
self.cancel_button_label = -> {
if resource.record.present?
"Cancel release on #{resource.record.name}"
else
"Cancel release"
end
}
end
Behavioral customization
Behavioral customization is the process of modifying the action's behavior. This includes changing the action's confirmation behavior and authorization.
-> no_confirmation
By default, actions display a confirmation modal before execution. You can bypass this modal by setting self.no_confirmation = true
, which will execute the action immediately upon triggering.
# app/avo/actions/release_fish.rb
class Avo::Actions::ReleaseFish < Avo::BaseAction
self.no_confirmation = true
end
This is particularly useful for actions that:
- Are safe to execute without confirmation
- Need to provide immediate feedback
- Are part of a multi-step workflow where confirmation is handled elsewhere
-> standalone
Standalone actions allow you to execute operations that aren't tied to specific model records. These are useful for global operations like:
- Generating system-wide reports
- Running maintenance tasks
- Triggering background jobs
You can create a standalone action in two ways:
- Using the generator with the
--standalone
flag:
bin/rails generate avo:action global_action --standalone
- Adding
self.standalone = true
to an existing action:
# app/avo/actions/global_report.rb
class Avo::Actions::GlobalReport < Avo::BaseAction
self.name = "Generate Global Report"
self.standalone = true
end
Standalone actions will be active in the Actions dropdown even when no records are selected. They can be used alongside regular record-based actions in the same resource.
TIP
Standalone actions work well with the fields
feature to collect additional input needed for the operation.
-> visible
You may want to hide specific actions on some views, like a standalone action on the Show
and Edit
views, and show it only on the Index
view. You can do that using the self.visible
attribute.
# app/avo/actions/global_report.rb
class Avo::Actions::GlobalReport < Avo::BaseAction
self.name = "Generate Global Report"
self.standalone = true
self.visible = true
# Or as a block
self.visible = -> { view.index? }
end
The visible
attribute accepts a boolean or a block.
The block will be executed within the Avo::ExecutionContext
environment, giving you access to important contextual attributes like:
view
- The current view type (index, show, edit)resource
- The current resource instanceparent_resource
- The parent resource (if applicable).- You can access the
parent_record
byparent_resource.record
- You can access the
- Plus all other
Avo::ExecutionContext
default attributes
-> authorize
The authorize
attribute is used to restrict access to actions based on custom logic.
If an action is unauthorized, it will be hidden. If a bad actor attempts to proceed with the action, the controller will re-evaluate the authorization and block unauthorized requests.
class Avo::Actions::GlobalReport < Avo::BaseAction
self.authorize = false
# Or as a block
self.authorize = -> {
current_user.is_admin?
}
end
The authorize
attribute accepts a boolean or a proc.
The block will be executed within the Avo::ExecutionContext
environment, giving you access to important contextual attributes like:
action
- The current action instanceresource
- The current resource instanceview
- The current view type (index, show, edit)- All other
Avo::ExecutionContext
attributes
-> close_modal_on_backdrop_click
By default, action modals use a dynamic backdrop.
Add self.close_modal_on_backdrop_click = false
in case you want to prevent the user from closing the modal when clicking on the backdrop.
# app/avo/actions/toggle_inactive.rb
class Avo::Actions::ToggleInactive < Avo::BaseAction
self.close_modal_on_backdrop_click = false
end
-> turbo
The turbo
attribute is used to control the Turbo behavior of actions.
There are times when you don't want to perform the actions with Turbo. In such cases, turbo should be set to false.
# app/avo/actions/toggle_inactive.rb
class Avo::Actions::ToggleInactive < Avo::BaseAction
self.turbo = false
end
The turbo
attribute accepts a boolean.