# Route overrides

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

[Route overrides](https://docs.forestadmin.com/documentation/reference-guide/routes/override-a-route) allowed customizing the behavior of the routes exposed by the agent.

This very low-level feature was used to implement many use cases:

* Attach handlers to events in the UI
* Customize filtering, search and sort behaviors
* Other advanced use cases.

Because our new agent API is higher-level, the protocol used to communicate between the agent and the application can no longer be manipulated.

## Code cheatsheet

| What was the route override used for?   | How to migrate it?                                                                                                                                                                    |
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Add custom permissions                  | Use [.add\_hook](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/hooks) and throw `forbidden errors`                                                     |
| Add validation to fields                | Use [.add\_field\_validation](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/fields/validation) instead                                                 |
| Add validation to whole records         | Use [.add\_hook](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/hooks) and throw `validation errors`                                                    |
| Run code on UI events                   | Use [.add\_hook](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/hooks) to perform custom logic                                                          |
| Change the search behavior              | Use [.replace\_search](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/search) to implement your custom search logic                                     |
| Change the filtering behavior of fields | Use [.replace\_field\_operator](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/fields/filter)                                                           |
| Change the sort behavior of fields      | Use [.replace\_field\_sorting](https://docs.forestadmin.com/developer-guide-agents-ruby/agent-customization/fields/sort)                                                              |
| Other use case                          | If you are stuck or think that this guide can be improved, please [expose your use case in the community forums](https://community.forestadmin.com/) and we will be happy to help you |

## Examples

### Add custom permissions

{% hint style="warning" %}
Custom permissions would better be implemented by using the [Roles](https://docs.forestadmin.com/user-guide/project-settings/teams-and-users/manage-roles) feature from the UI.
{% endhint %}

{% tabs %}
{% tab title="Before" %}

* Define a new route into your `routes.rb` file:
* Add a new method in your controller.

```ruby
module ForestLiana
  class BooksController < ForestLiana::ApplicationController
    def destroy
      Book.find(params[:id]).destroy

      head :no_content
    end
  end
end
```

{% endtab %}

{% tab title="After" %}

```ruby
module ForestAdminRails
  class CreateAgent
    def self.customize
      @create_agent.customize_collection('Book') do |collection|
        collection.add_hook('Before', 'Delete') do |context|
          is_allowed = true # YOUR LOGIC HERE

          if !is_allowed
            context.raise_forbidden_error('This collection is protected, you cannot remove from it.')
          end
        end
      end
    end
  end
end
```

{% endtab %}
{% endtabs %}

### Add validation to fields

{% tabs %}
{% tab title="Before" %}

* Define a new route into your `routes.rb` file.
* Add a new method to your controller.

```ruby
module ForestLiana
  class CompaniesController < ForestLiana::ApplicationController
    def create
      if params[:name] && params[:name].match?(/^Forest/)
        head :bad_request, content_type: 'application/json'
      else
        super
      end
    end

    def update
      if params[:name] && params[:name].match?(/^Forest/)
        head :bad_request, content_type: 'application/json'
      else
        super
      end
    end
  end
end
```

{% endtab %}

{% tab title="After" %}

```ruby
module ForestAdminRails
  class CreateAgent
    include ForestAdmin::Types

    def self.customize
      @create_agent.customize_collection('Company') do |collection|
        collection.add_field_validation('name', Operators::MATCH, /^Forest/)
      end
    end
  end
end
```

{% endtab %}
{% endtabs %}

### Run code on UI events

{% tabs %}
{% tab title="Before" %}

```ruby
module ForestLiana
  class CustomersController < ForestLiana::ApplicationController
    def create
      begin
        superagent.post('https://my-company/create-card').set(
          'X-API-Key', '**********'
        ).end()
      ensure
        super
      end
    end
  end
end
```

{% endtab %}

{% tab title="After" %}

```ruby
module ForestAdminRails
  class CreateAgent
    def self.customize
      @create_agent.customize_collection('Customer') do |collection|
        collection.add_hook('Before', 'Create') do |context|
          superagent.post('https://my-company/create-card').set(
            'X-API-Key', '**********'
          )
        end
      end
    end
  end
end
```

{% endtab %}
{% endtabs %}
