Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better engine mounting #350

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/3.0/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ You probably do not want to allow Avo access to everybody. If you're using [devi

```ruby
authenticate :user do
mount Avo::Engine => '/avo'
mount_avo at: '/avo'
end
```

You may also add custom user validation such as `user.admin?` to only permit a subset of users to your Avo instance.

```ruby
authenticate :user, -> user { user.admin? } do
mount Avo::Engine => '/avo'
mount_avo at: '/avo'
end
```

Expand Down
63 changes: 0 additions & 63 deletions docs/3.0/common/mount_avo_under_locale_scope_common.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/3.0/custom-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Rails.application.routes.draw do
end

authenticate :user, ->(user) { user.admin? } do
mount Avo::Engine => Avo.configuration.root_path
mount_avo
end
end
```
Expand Down
4 changes: 2 additions & 2 deletions docs/3.0/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ Rails.application.routes.draw do
get "dashboard", to: "avo/tools#dashboard" # custom tool added before engine
end

mount Avo::Engine, at: Avo.configuration.root_path # engine mounted last
mount_avo # engine mounted last
end
end
end
Expand Down Expand Up @@ -505,7 +505,7 @@ end
Rails.application.routes.draw do
# Use to test out route-based multitenancy
scope "/account/:account_id" do
mount Avo::Engine, at: Avo.configuration.root_path
mount_avo
end
end
```
Expand Down
2 changes: 1 addition & 1 deletion docs/3.0/fields/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ Rails.application.routes.draw do
# your routes

authenticate :user, ->(user) { user.is_admin? } do
mount Avo::Engine, at: Avo.configuration.root_path
mount_avo
end
end

Expand Down
11 changes: 10 additions & 1 deletion docs/3.0/guides/multi-language-urls.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ Implementing multi-language URLs is a common use-case. Using a route scope block

## 1. Mount Avo within a `:locale` scope

<!-- @include: ./../common/mount_avo_under_locale_scope_common.md -->
Using a locale scope is an effective way to set the locale for your users.

```ruby{3-5}
# config/routes.rb
Rails.application.routes.draw do
scope ":locale" do
mount_avo
end
end
```

## 2. Apply the `locale` Scope

Expand Down
4 changes: 2 additions & 2 deletions docs/3.0/guides/multitenancy.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Rails.application.routes.draw do
# Your routes

authenticate :user, -> user { user.admin? } do
mount Avo::Engine => Avo.configuration.root_path
mount_avo
end
end
```
Expand All @@ -93,7 +93,7 @@ Rails.application.routes.draw do
# Your routes

authenticate :user, -> user { user.admin? } do
mount Avo::Engine => Avo.configuration.root_path
mount_avo
end
end

Expand Down
2 changes: 1 addition & 1 deletion docs/3.0/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ You can use the regular `host` constraint in the `routes.rb` file.

```ruby
constraint host: 'avo' do
mount Avo::Engine, at: '/'
mount_avo at: '/'
end
```

Expand Down
39 changes: 9 additions & 30 deletions docs/3.0/multitenancy.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,20 @@ There are a couple of strategies here, but the a common one is to use route-base

We need to do a few things:

#### 1. Disable automatic Avo engine mounting
#### 1. Set the proper routing pattern

_Do this step only if you use other Avo gems (`avo-pro`, `avo-advanced`, etc.)_
Mount Avo under the `tenant_id` scope

Avo will automatically mount it's engines unless you tell it otherwise, which is what we'll do now.
:::code-group
```ruby [config/avo.rb]{3}
Avo.configure do |config|
# Disable automatic engine mounting
config.mount_avo_engines = false

# other configuration
end
```
:::

**Related:**
- [Avo's Engines](./routing#avo-s-engines)

#### 2. Set the proper routing pattern

:::code-group
```ruby [config/routes.rb]
# Mount Avo and it's engines under the `tenant_id` scope
scope "/:tenant_id" do
mount Avo::Engine, at: Avo.configuration.root_path

scope Avo.configuration.root_path do
instance_exec(&Avo.mount_engines)
```ruby
# config/routes.rb
Rails.application.routes.draw do
scope "/:tenant_id" do
mount_avo
end
end
```
:::


#### 3. Set the tenant for each request
#### 2. Set the tenant for each request

:::code-group
```ruby [config/initializers/avo.rb]{6}
Expand Down
7 changes: 4 additions & 3 deletions docs/3.0/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ This means we provide two hooks that you can use to extend the functionality of

The way we do it is through an initializer. We mostly use the `engine.rb` file to register the plugin.

```ruby{8}
```ruby{8-15}
# lib/avo/feed_view/engine.rb
module Avo
module FeedView
class Engine < ::Rails::Engine
Expand All @@ -36,8 +37,8 @@ module Avo
# Register the plugin
Avo.plugin_manager.register :feed_view

# You can pass a priority too but it's not used at the moment
Avo.plugin_manager.register :feed_view, 5
# Register the mounting point
Avo.plugin_manager.mount_engine Avo::FeedView::Engine, at: "/feed_view"
end
end
end
Expand Down
62 changes: 25 additions & 37 deletions docs/3.0/routing.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,44 @@
# Routing

We stick to Rails defaults in terms of routing just to make working with Avo as straighforward as possible.
We stick to Rails defaults in terms of routing just to make working with Avo as straightforward as possible.

## Avo's Engines
Avo's functionality is distributed across multiple gems, each encapsulating its own engine. By default, these engines are mounted under Avo's scope within your Rails application.

Avo's functionality is bundled in a few gems and most of them have their own engines. By default we mount the engines under Avo's routes using a configuration like this one.
Each engine registers itself with Avo.

```ruby
# Your app's routes.rb
Rails.application.routes.draw do
mount Avo::Engine, at: Avo.configuration.root_path

# other routes
end

# Avo's routes.rb
Avo::Engine.routes.draw do
mount Avo::DynamicFilters::Engine, at: "/avo-dynamic_filters" if defined?(Avo::DynamicFilters::Engine)
mount Avo::Dashboards::Engine, at: "/dashboards" if defined?(Avo::Dashboards::Engine)
mount Avo::Pro::Engine, at: "/avo-pro" if defined?(Avo::Pro::Engine)
### Default Mounting Behavior

# other routes
end
```

<Option name="`Avo.mount_engines` helper">
When the `mount_avo` method is invoked, Avo and all the associated engines are mounted at a common entry point. By default, this mounting point corresponds to `Avo.configuration.root_path`, but you can customize it using the `at` argument:

In order to make mounting the engines easier we added the `Avo.mount_engines` helper which returns a block that can be run in any routing context.

```ruby
# The configuration above turns into
Avo::Engine.routes.draw do
instance_exec(&Avo.mount_engines)
```ruby{4,7}
# config/routes.rb
Rails.application.routes.draw do
# Mounts Avo at Avo.configuration.root_path
mount_avo

# other routes
# Mounts Avo at `/custom_path` instead of the default
mount_avo at: "custom_path"
end
```
</Option>

Sometimes you might have more exotic use-cases so you'd like to customize those paths accordingly.
If no custom path is specified, Avo is mounted at the default configuration root path.

## Mount Avo under a scope

:::info
The `:locale` scope provided is just an example. If your objective is to implement a route scope for localization within Avo, there's a detailed recipe available (including this step). Check out [this guide](guides/multi-language-urls) for comprehensive instructions.
In this example, we'll demonstrate how to add a `:locale` scope to your routes.

If your goal is adding another scope unrelated to localization, you're in the right place. This approach works for other types of scoped routing as well.
:::
The `:locale` scope is just an example. If your objective is to implement a route scope for localization within Avo, there's a detailed recipe available. Check out [this guide](guides/multi-language-urls) for comprehensive instructions.

In this example, we'll demonstrate how to add a `:locale` scope to your routes.
```ruby{4-6}
# config/routes.rb

Rails.application.routes.draw do
scope ":locale" do
mount_avo
end
end
```

<!-- @include: ./common/mount_avo_under_locale_scope_common.md-->

:::info
To guarantee that the `locale` scope is included in the `default_url_options`, you must explicitly add it to the Avo configuration.
Expand All @@ -67,7 +55,7 @@ You can do that in your app's `routes.rb` file by opening up the Avo routes bloc
```ruby
# routes.rb
Rails.application.routes.draw do
mount Avo::Engine, at: Avo.configuration.root_path
mount_avo

# your other app routes
end
Expand Down
38 changes: 38 additions & 0 deletions docs/3.0/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ If you're looking for the Avo 2 to Avo 3 upgrade guide, please visit [the dedica

## Upgrade to 3.18.0

<Option name="Avo's mounting point update">

We have refactored how Avo and its engines are mounted, eliminating the need for options like `mount_avo_engines` and custom engine mounting via `Avo.mount_engines`.

As a result, both `mount_avo_engines` and `Avo.mount_engines` are now obsolete. The new mounting approach simplifies integration and removes the need for custom logic.

### Breaking Change
- `mount_avo_engines` and `Avo.mount_engines` are obsolete and should no longer be used.
- Replace Avo mounting point with the new `mount_avo` approach.

### Steps to Update
1. **Remove all references** to `mount_avo_engines` and `Avo.mount_engines` from your application.
2. **Update your `routes.rb` file** by replacing:
- `mount Avo::Engine, at: Avo.configuration.root_path` → with → `mount_avo`
- If using a custom path (`mount Avo::Engine, at: "custom/path"`), replace it with `mount_avo at: "custom/path"`

By following these steps, your application will be fully compatible with the new mounting strategy.


```ruby
# config/routes.rb
Rails.application.routes.draw do
mount Avo::Engine, at: Avo.configuration.root_path # [!code --]
mount_avo # [!code ++]
end
```


```ruby
# config/routes.rb
Rails.application.routes.draw do
mount Avo::Engine, at: "custom/path" # [!code --]
mount_avo at: "custom/path" # [!code ++]
end
```

</Option>

<Option name="Preview Policy">

As of <Version version="3.18.0" />, a new policy method is available: `preview?`.
Expand Down