Result builder

Actions can be configured to achieve different results in the GUI.

Most actions will simply perform work and display the default notification, however, other behaviors are possible:

Default behavior

The default behavior, when no exception is thrown in the handler is to display a generic notification.

include ForestAdminDatasourceCustomizer::Decorators::Action::Types
include ForestAdminDatasourceCustomizer::Decorators::Action::Context

@create_agent.customize_collection('company') do |collection|
  collection.add_action(
    'Mark as live',
    BaseAction.new(scope: ActionScope::SINGLE) do |context|
      # Not using the resultBuilder here will display the generic success notification.
      # (as long as no exception is thrown)
    end
  )
end

Custom notifications

When customizing the notification message, you can use the ForestAdminDatasourceCustomizer::Decorators::Action::ResultBuilder to generate different types of responses.

include ForestAdminDatasourceCustomizer::Decorators::Action::Types
include ForestAdminDatasourceCustomizer::Decorators::Action::Context

@create_agent.customize_collection('company') do |collection|
  collection.add_action(
    'Mark as live',
    BaseAction.new(scope: ActionScope::SINGLE) do |_context, result_builder|
      if # Company is not live
        result_builder.success(message: 'Company is now live!')
      else
        result_builder.error(message: 'The company was already live!')
      end
    end
  )
end

HTML result

You can also return an HTML page to give more feedback to the user who triggered the Action.

include ForestAdminDatasourceCustomizer::Decorators::Action::Types
include ForestAdminDatasourceCustomizer::Decorators::Action::Context

@create_agent.customize_collection('company') do |collection|
  collection.add_action(
    'Charge credit card',
    BaseAction.new(scope: ActionScope::SINGLE) do |context, result_builder|
      # ... charge the credit card ...
      record = context.get_record(['amount', 'source:last4'])
      if credit_card_successfully_charged
        result_builder.success(
          message: 'Success',
          options: {
            html: "<p class='c-clr-1-4 l-mt l-mb'>#{record['amount'] / 100} USD has been successfully charged.</p>
              <strong class='c-form__label--read c-clr-1-2'>Credit card</strong>
              <p class='c-clr-1-4 l-mb'>**** **** **** #{record['source']['last4']}</p>"
          }
        )
      else
        result_builder.error(
          message: 'An error occured',
          options: {
            html: "<p class='c-clr-1-4 l-mt l-mb'>#{record['amount'] / 100} USD has not been charged.</p>
              <strong class='c-form__label--read c-clr-1-2'>Credit card</strong>
              <p class='c-clr-1-4 l-mb'>**** **** **** #{record['source']['last4']}</p>
              <strong class='c-form__label--read c-clr-1-2'>Reason</strong>
              <p class='c-clr-1-4 l-mb'>You can not charge this credit card. The card is marked as blocked</p>"
          }
        )
      end
    end
  )
end

File generation

Smart actions can be used to generate or download files.

The example code below will trigger a file download (with the file named filename.txt, containing StringThatWillBeInTheFile using text/plain mime-type).

include ForestAdminDatasourceCustomizer::Decorators::Action::Types
include ForestAdminDatasourceCustomizer::Decorators::Action::Context

@create_agent.customize_collection('company') do |collection|
  collection.add_action(
    'Download a file',
    BaseAction.new(scope: ActionScope::GLOBAL, is_generate_file: true) do |_context, result_builder|
      my_file = File.open('filename.txt', 'w')
      my_file.write('StringThatWillBeInTheFile')
      my_file.close

      result_builder.file(content: 'filename.txt', name: 'filename.txt', mime_type: 'text/plain')
    end
  )
end

Redirections

To streamline your operation workflow, it could make sense to redirect to another page after an Action has successfully been executed.

It is possible using the redirectTo function.

The redirection works both for internal (\*.forestadmin.com pages) and external links.

Webhooks

After an action you can set up an HTTP (or HTTPS) callback - a webhook - to forward information to other applications.

Note that the webhook will be triggered from the user's browser, so it will be subject to CORS restrictions.

Its intended use is often to perform a login on a third-party application or to trigger a background job on the current user's behalf.

include ForestAdminDatasourceCustomizer::Decorators::Action::Types
include ForestAdminDatasourceCustomizer::Decorators::Action::Context

@create_agent.customize_collection('company') do |collection|
  collection.add_action(
    'Mark as live',
    BaseAction.new(scope: ActionScope::SINGLE) do |_context, result_builder|
      result_builder.webhook(
        url: 'http://my-company-name', # The url of the company providing the service.
        method: 'POST', # The method you would like to use (typically a POST).
        headers: {}, # You can add some headers if needed.
        body: { adminToken: 'your-admin-token' } # A body to send to the url.
      )
    end
  )
end

Last updated

Was this helpful?