From 0b4235c90926f797dca5cffcda84d002fa3237f9 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sat, 9 Mar 2024 00:13:37 +0700 Subject: [PATCH] remove `extend_controllers_with` API --- docs/3.0/avo-application-controller.md | 41 ++++++++++++++++++++++---- docs/3.0/avo-current.md | 1 - docs/3.0/customization.md | 32 -------------------- docs/3.0/multitenancy.md | 16 +++++++--- 4 files changed, 48 insertions(+), 42 deletions(-) diff --git a/docs/3.0/avo-application-controller.md b/docs/3.0/avo-application-controller.md index fdc017b2..2a3597c4 100644 --- a/docs/3.0/avo-application-controller.md +++ b/docs/3.0/avo-application-controller.md @@ -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. @@ -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) diff --git a/docs/3.0/avo-current.md b/docs/3.0/avo-current.md index aebbcd23..d10be166 100644 --- a/docs/3.0/avo-current.md +++ b/docs/3.0/avo-current.md @@ -42,5 +42,4 @@ You can set the `tenant` for the current request. **Related:** - [Multitenancy](./multitenancy) - - [`extend_controllers_with`](./customization#extend_controllers_with) diff --git a/docs/3.0/customization.md b/docs/3.0/customization.md index fe42c1ea..aaa71eeb 100644 --- a/docs/3.0/customization.md +++ b/docs/3.0/customization.md @@ -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. diff --git a/docs/3.0/multitenancy.md b/docs/3.0/multitenancy.md index fd21f1b1..0cf4c7ab 100644 --- a/docs/3.0/multitenancy.md +++ b/docs/3.0/multitenancy.md @@ -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] @@ -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]