# Actions

{% hint style="success" %}
This is the official documentation of the `agent_ruby` Ruby agent.
{% endhint %}

Sooner or later, you will need to perform actions on your data that are specific to your business.

Moderating comments, generating invoices, logging into a customer’s account, or banning users are exactly the kind of important tasks to unlock to manage your day-to-day operations.

![Custom Action displayed in a Table View](https://561307319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fn23xpsrpC8uJXljJ3an2%2Fuploads%2Fgit-blob-d43582d7cd00503688418f24ec3689808dc40730%2Factions-dropdown.png?alt=media)

### In your code

To create an Action, you will first need to declare it in your code for a specific collection. Here we declare a "Mark as Live" Action for the `companies` collection.

The action behavior is implemented in the `execute` function.

#### Traditional Syntax

Using full context methods with `customize_collection`:

```ruby
include ForestAdmin::Types

@create_agent.customize_collection('companies') do |collection|
  collection.add_action(
    'Mark as Live',
    BaseAction.new(
      scope: ActionScope::GLOBAL,
      description: "Mark the company as live",
      submit_button_label: "Validate 🫵",
      form: [
        {
            type: FieldType::LAYOUT,
            component: 'Page',
            next_button_label: "Go to address",
            elements:[
                {type: FieldType::DATE, label: "Live date",id: "LiveDate"},
                {type: FieldType::STRING, label: "Exist since", id: "CreationDate"},
                {type: FieldType::LAYOUT, component: "Separator"},
                {
                    type: FieldType::LAYOUT,
                    component: "Row",
                    fields: [
                        {type: FieldType::BOOLEAN, label: "CreditCard ?" id: "WithCreditCard"},
                        {
                            type: FieldType::STRING,
                            if_condition: ->(ctx) {ctx.form_values?.WithCreditCard == true},
                            label: "Number",
                            id: "CreditCardNumber",
                        },
                    ],
                },
            ],
        },
        {
            type: FieldType::LAYOUT,
            component: "Page",
            previous_button_label: "Go back to identity",
            elements: [
                {
                    type: FieldType::LAYOUT,
                    component: "Row",
                    fields: [
                        {type: FieldType::NUMBER, label: "StreetNumber"},
                        {type: FieldType::NUMBER, label: "StreetName"},
                    ],
                },
                {type: FieldType::LAYOUT, component: "Separator"},
                {type: FieldType::NUMBER, label: "PostalCode"},
                {type: FieldType::NUMBER, label: "City"},
                {type: FieldType::LAYOUT, component: "Separator"},
                {type: FieldType::NUMBER, label: "Country"},
            ],
        },
      ]
    ) do |context|
      # Implement your controller here.
    end
  )
end
```

#### DSL Syntax

Using simplified helper methods with `collection.action`:

```ruby
@create_agent.collection :companies do |collection|
  collection.action 'Mark as Live', scope: :global do
    description "Mark the company as live"
    submit_button_label "Validate 🫵"

    form do
      # Define form fields using the form DSL
      # See forms-static.md for more details
    end

    execute do
      # Implement your controller here.
      success 'Company marked as live!'
    end
  end
end
```

| Property              | Usage       | Description                                                                                                                                                                                                                                                                                                                                                                        |
| --------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| scope                 | **require** | `Single`, `Global` or `Bulk`. See [here](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/actions/scope-context) for more detail                                                                                                                                                                                                                       |
| execute               | **require** | The callable called when the action is executed, with `context` and `result builder` as parameters. See [context](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/scope-context#the-context-object) and [result builder](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/actions/result-builder) pages for more details. |
| form                  | *optional*  | A list of static fields to be input by the user or a function called with `context` as parameters which returns a list of fields. See [form](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/actions/forms-dynamic) page for more details.                                                                                                            |
| description           | *optional*  | An optional description of the action. *Default: null*                                                                                                                                                                                                                                                                                                                             |
| submit\_button\_label | *optional*  | A custom label for the submit button. *Default: the action name*                                                                                                                                                                                                                                                                                                                   |

### In the admin panel

After declaring it, the Action will appear in the Smart Actions tab within your Collection Settings.

{% hint style="info" %}
An Action is displayed in the UI only if:

* it is set as "visible" (see screenshot below) AND
* in non-development environments, the user's role must grant the "trigger" permission
  {% endhint %}

You must make the Action visible there if you wish users to be able to see it in this Team.

![Making the Action visible](https://561307319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fn23xpsrpC8uJXljJ3an2%2Fuploads%2Fgit-blob-a2c3d4ee72768a9fa63d091617829bbf9b026488%2Factions-visibility.png?alt=media)
