You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Every single file in autoload_paths is eagerly loaded by Rails admin. Below, I'll explain how this happens, why this shouldn't happen, and will propose a solution.
I came across this when trying to figure a bizarre issue in Thredded + rails_admin compatibility: thredded/thredded#280.
defall(adapter=nil)@@all ||= Config.models_pool.collect{ |m| new(m)}.compactadapter ? @@all.select{ |m| m.adapter == adapter} : @@allendalias_method:old_new,:newdefnew(m)m=m.constantizeunlessm.is_a?(Class)(am=old_new(m)).model && am.adapter ? am : nilrescueLoadError,NameErrorputs"[RailsAdmin] Could not load model #{m}, assuming model is non existing. (#{$ERROR_INFO})"unlessRails.env.test?nilend
Why this shouldn't happen?
If something is in autoload_paths, it doesn't mean it can or should always be loaded. Rails explicitly makes this distinction with eager_load.
E.g. if I'm developing a Rails engine, I might want to add lib to autoload paths (to ease the development of the engine), but that doesn't mean I want to eagerly load ruby files in lib/generators/templates!
Proposed solution
There are a few options here:
Remove app.config.autoload_paths from the files RailsAdmin tries to autoload. Only load things from app/models directories.
Replace app.config.autoload_paths with app.paths.eager_load.
Stop messing with the loading system! Just include a module into the base class!
3 is ideal but is also the most effort and I don't have the time. 1 breaks backwards compatibility.
So I'll send a PR for 2 for now.
Perhaps one day somebody can do 3.
If something is in `autoload_paths`, it doesn't mean it can or should *always* be loaded. Rails explicitly makes this distinction with `eager_load`.
E.g. if I'm developing a Rails engine, I might want to add `lib` to autoload paths (to ease the development of the engine), but that doesn't mean I want to eagerly load ruby files in `lib/generators/templates`!
Resolvesrailsadminteam#2770
If something is in `autoload_paths`, it doesn't mean it can or should *always* be loaded. Rails explicitly makes this distinction with `eager_load`.
E.g. if I'm developing a Rails engine, I might want to add `lib` to autoload paths (to ease the development of the engine), but that doesn't mean I want to eagerly load ruby files in `lib/generators/templates`!
Resolvesrailsadminteam#2770
Every single file in
autoload_paths
is eagerly loaded by Rails admin. Below, I'll explain how this happens, why this shouldn't happen, and will propose a solution.I came across this when trying to figure a bizarre issue in Thredded + rails_admin compatibility: thredded/thredded#280.
How does this happen?
Here is what RailsAdmin tries to load, lib/rails_admin/config.rb#L322:
It loads these by calling constantize in lib/rails_admin/abstract_model.rb#L13-L25:
Why this shouldn't happen?
If something is in
autoload_paths
, it doesn't mean it can or should always be loaded. Rails explicitly makes this distinction witheager_load
.E.g. if I'm developing a Rails engine, I might want to add
lib
to autoload paths (to ease the development of the engine), but that doesn't mean I want to eagerly load ruby files inlib/generators/templates
!Proposed solution
There are a few options here:
app.config.autoload_paths
from the files RailsAdmin tries to autoload. Only load things fromapp/models
directories.app.config.autoload_paths
withapp.paths.eager_load
.3 is ideal but is also the most effort and I don't have the time. 1 breaks backwards compatibility.
So I'll send a PR for 2 for now.
Perhaps one day somebody can do 3.
@mshibuya @bbenezech @sferik
The text was updated successfully, but these errors were encountered: