Skip to content

Commit

Permalink
remove extend_controllers_with API
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Mar 8, 2024
1 parent 3e18824 commit 0b4235c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 42 deletions.
41 changes: 36 additions & 5 deletions docs/3.0/avo-application-controller.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# `Avo::ApplicationController`

:::tip
To safely extend Avo's `ApplicationController` please use the [`extend_controllers_with`](./customization#extend_controllers_with) configuration option.
:::

## On extending the `ApplicationController`

You may sometimes want to add functionality to Avo's `ApplicationController`. That functionality may be setting attributes to `Current` or multi-tenancy scenarios.
Expand Down Expand Up @@ -68,6 +64,41 @@ With this technique, the `multitenancy_detector` method and its `before_action`
If you'd like to add a `before_action` before all of Avo's before actions, use `prepend_before_action` instead. That will run that code first and enable you to set an account or do something early on.
:::

## Override `ApplicationController` methods

Sometimes you don't want to add methods but want to override the current ones.

For example, you might want to take control of the `Avo::ApplicationController.fill_record` method and add your own behavior.

TO do that you should change a few things in the approach we mentioned above. First we want to `prepend` the concern instead of `include` it and next, if we want to run a class method, we used `prepended` instead of `included`.


```ruby{5-8,10-12,14-17,23}
# app/controllers/concerns/application_controller_overrides.rb
module ApplicationControllerOverrides
extend ActiveSupport::Concern
# we use the `prepended` block instead of `included`
prepended do
before_action :some_hook
end
def some_hook
# your logic here
end
def fill_record
# do some logic here
super
end
end
# configuration/initializers/avo.rb
Rails.configuration.to_prepare do
# we will prepend instead of include
Avo::ApplicationController.prepend ApplicationControllerOverrides
end
```

**Related:**
- [Multitenancy](./multitenancy)
- [`extend_controllers_with`](./customization#extend_controllers_with)
1 change: 0 additions & 1 deletion docs/3.0/avo-current.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ You can set the `tenant` for the current request.

**Related:**
- [Multitenancy](./multitenancy)
- [`extend_controllers_with`](./customization#extend_controllers_with)

32 changes: 0 additions & 32 deletions docs/3.0/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,38 +468,6 @@ config.logger = -> {
}
```

::::option `extend_controllers_with`
The `extend_controllers_with` configuration option enables you to add methods or hooks to Avo's `ApplicationController` in the main `avo` gem and all the other ones (`avo-pro`, `avo-dashboards`, etc.).

All you need to do is to pass an array with the concerns you need to have included. The concerns should be strings as we later constantize them for you.

::: code-group
```ruby [config/initializers/avo.rb]{2}
Avo.configure do |config|
config.extend_controllers_with = ["Multitenancy"]
end
```
```ruby [app/controllers/concerns/multitenancy.rb]
module Multitenancy
extend ActiveSupport::Concern

included do
prepend_before_action :set_tenant
end

def set_tenant
Avo::Current.tenant_id = params[:tenant_id]
Avo::Current.tenant = Account.find params[:tenant_id]
end
end
```
:::
::::

Related:
- [Multitenancy](./multitenancy)
- [`Avo::Current`](./avo-current#tenant_id)

::::option `default_url_options`
`default_url_options` is a Rails [controller method](https://apidock.com/rails/ActionController/Base/default_url_options) that will append params automatically to the paths you generate through path helpers.

Expand Down
16 changes: 12 additions & 4 deletions docs/3.0/multitenancy.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ scope "/:tenant_id" do
#### 3. Set the tenant for each request

:::code-group
```ruby [config/initializers/avo.rb]{2}
```ruby [config/initializers/avo.rb]{6}
Avo.configure do |config|
config.extend_controllers_with = ["Multitenancy"]
# configuration values
end

Rails.configuration.to_prepare do
Avo::ApplicationController.include Multitenancy
end
```
```ruby [app/controllers/concerns/multitenancy.rb]
Expand Down Expand Up @@ -90,9 +94,13 @@ We need to do a few things:

#### 1. Set the tenant for each request
:::code-group
```ruby [config/initializers/avo.rb]{2}
```ruby [config/initializers/avo.rb]{6}
Avo.configure do |config|
config.extend_controllers_with = ["Multitenancy"]
# configuration values
end

Rails.configuration.to_prepare do
Avo::ApplicationController.include Multitenancy
end
```
```ruby [app/controllers/concerns/multitenancy.rb]
Expand Down

0 comments on commit 0b4235c

Please sign in to comment.