Skip to content

Commit 1df1df9

Browse files
authored
Reserved model names and routes (#344)
1 parent ab25090 commit 1df1df9

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

docs/.vitepress/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ const config = {
269269
{text: "<code>Avo::Services::EncryptionService</code>", link: "/3.0/encryption-service.html"},
270270
{text: "Select All", link: "/3.0/select-all.html"},
271271
{text: "Icons", link: "/3.0/icons.html"},
272+
{text: "Reserved model names and routes", link: "/3.0/internal-model-names.html"},
272273
],
273274
},
274275
{

docs/3.0/internal-model-names.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Reserved model names and routes
2+
3+
When defining models in an Avo-powered application, certain names should be avoided as they are used by Avo’s internal controllers. Using these names may lead to conflicts, routing issues, or unexpected behavior.
4+
5+
## Model names to avoid
6+
7+
Avo uses the following names for its internal controllers:
8+
9+
- `action`
10+
- `application`
11+
- `association`
12+
- `attachment`
13+
- `base_application`
14+
- `base`
15+
- `chart`
16+
- `debug`
17+
- `home`
18+
- `private`
19+
- `resource`
20+
- `search`
21+
22+
Using these names for models may override built-in functionality, cause routing mismatches, or introduce other conflicts.
23+
24+
## Why these names are reserved
25+
26+
Avo relies on these names for its controller and routing system. For example:
27+
- `resource` is essential for managing Avo resources.
28+
- `chart` is used for analytics and visualizations.
29+
- `search` handles search functionality.
30+
31+
Since Avo dynamically maps models and controllers, using these names may interfere with how Avo processes requests and displays resources.
32+
33+
## Alternative approaches
34+
35+
If your application requires one of these names, consider the following alternatives:
36+
- **Use a prefix or suffix**
37+
- `user_resource` instead of `resource`
38+
- `advanced_search` instead of `search`
39+
- **Choose a synonym**
40+
- `graph` instead of `chart`
41+
42+
### Using Avo with existing models
43+
44+
If your application already has models with these names, you can generate an Avo resource with a different name while keeping the same model class.
45+
46+
For example for `Resource` run the following command:
47+
48+
```sh
49+
bin/rails generate avo:resource user_resource --model-class resource
50+
```
51+
52+
This will generate:
53+
54+
- `Avo::Resources::UserResource`
55+
- `Avo::UserResourcesController`
56+
57+
However, it will still use the existing `Resource` model, ensuring no conflicts arise.
58+
59+
## Route Conflicts with `resources :resources`
60+
61+
If your application has a route definition like:
62+
63+
```ruby
64+
resources :resources
65+
```
66+
67+
This will create path helpers such as `resources_path`, which **conflicts with [Avo’s internal routing helpers](https://github.com/avo-hq/avo/blob/main/app/helpers/avo/url_helpers.rb#L3)**. Avo uses `resources_path` internally, and having this route in your application **will override Avo’s default helpers**, potentially breaking parts of the admin panel.
68+
69+
### How to Fix It
70+
71+
To prevent conflicts, rename the route helpers to something more specific:
72+
73+
```ruby
74+
resources :resources, as: 'articles'
75+
```
76+
77+
This allows you to maintain the desired URL structure (`/resources`) without interfering with Avo’s internals.

0 commit comments

Comments
 (0)