|
| 1 | +--- |
| 2 | +license: community |
| 3 | +feedbackId: 837 |
| 4 | +outline: deep |
| 5 | +--- |
| 6 | + |
| 7 | +# Action Generator |
| 8 | + |
| 9 | +Avo provides a powerful Rails generator to create action files quickly and efficiently. |
| 10 | + |
| 11 | +## Basic Generator Usage |
| 12 | + |
| 13 | +Generate a new action file using the Rails generator: |
| 14 | + |
| 15 | +```bash |
| 16 | +bin/rails generate avo:action toggle_inactive |
| 17 | +``` |
| 18 | + |
| 19 | +This command creates a new action file at `app/avo/actions/toggle_inactive.rb` with the following structure: |
| 20 | + |
| 21 | +```ruby |
| 22 | +# app/avo/actions/toggle_inactive.rb |
| 23 | +class Avo::Actions::ToggleInactive < Avo::BaseAction |
| 24 | + self.name = "Toggle Inactive" |
| 25 | + # self.visible = -> do |
| 26 | + # true |
| 27 | + # end |
| 28 | + |
| 29 | + # def fields |
| 30 | + # # Add Action fields here |
| 31 | + # end |
| 32 | + |
| 33 | + def handle(query:, fields:, current_user:, resource:, **args) |
| 34 | + query.each do |record| |
| 35 | + # Do something with your records. |
| 36 | + end |
| 37 | + end |
| 38 | +end |
| 39 | +``` |
| 40 | + |
| 41 | +## Generator Options |
| 42 | + |
| 43 | +### `--standalone` |
| 44 | + |
| 45 | +By default, actions require at least one record to be selected before they can be triggered, unless specifically configured as standalone actions. |
| 46 | + |
| 47 | +The `--standalone` option creates an action that doesn't require record selection. This is particularly useful for: |
| 48 | +- Generating reports |
| 49 | +- Exporting all records |
| 50 | +- Running global operations |
| 51 | + |
| 52 | +```bash |
| 53 | +bin/rails generate avo:action export_users --standalone |
| 54 | +``` |
| 55 | + |
| 56 | +You can also make an existing action standalone by manually setting `self.standalone = true` in the action class: |
| 57 | + |
| 58 | +```ruby{5} |
| 59 | +# app/avo/actions/export_users.rb |
| 60 | +
|
| 61 | +class Avo::Actions::ExportUsers < Avo::BaseAction |
| 62 | + self.name = "Export Users" |
| 63 | + self.standalone = true |
| 64 | +
|
| 65 | + # ... rest of the action code |
| 66 | +end |
| 67 | +``` |
| 68 | + |
| 69 | +## Best Practices |
| 70 | + |
| 71 | +When generating actions, consider the following: |
| 72 | + |
| 73 | +1. Use descriptive names that reflect the action's purpose (e.g., `toggle_published`, `send_newsletter`, `archive_records`) |
| 74 | +2. Follow Ruby naming conventions (snake_case for file names) |
| 75 | +3. Group related actions in namespaces using subdirectories |
| 76 | +4. Use the `--standalone` flag when the action doesn't operate on specific records |
| 77 | + |
| 78 | +## Examples |
| 79 | + |
| 80 | +```bash |
| 81 | +# Generate a regular action |
| 82 | +bin/rails generate avo:action mark_as_featured |
| 83 | + |
| 84 | +# Generate a standalone action |
| 85 | +bin/rails generate avo:action generate_monthly_report --standalone |
| 86 | + |
| 87 | +# Generate an action in a namespace |
| 88 | +bin/rails generate avo:action admin/approve_user |
| 89 | +``` |
0 commit comments