Skip to content

Make reusable action configuration autoload-friendly #3518

Open
@Cardyard

Description

@Cardyard

Is your feature request related to a problem? Please describe.
It took me a while to get autoloading working with Zeitwerk and Rails 7 and Rails Admin 3.0. I've ended up with the opposite of what I had pre: Zeitwerk/Rails 7/Rails Admin 3.0..

  • Any code in a reusable action is not reloaded
  • Any code outside of a reusable action is reloaded

Describe proposed solution(s)
Add some comments to...
Configuration
2. Actions

These are my proposed comments...

Reloading with Zeitwerk

If you are defining reusable rails_admin actions in zeitwerk mode(the only mode post Rails 6), you can include your library of code in the main autoloader in config/application.rb, but must register your rails_admin actions only once. This can be achieved using the after_initialize block

# config/initializers/rails_admin.rb
# Define asset_source outside the `after_initialize' block so it is run before the gem initializer and avoid getting an error
RailsAdmin.config { |config| config.asset_source = :sprockets } # or :webpacker
Rails.application.config.after_initialize do
  # Require your reusable actions which will have the line RailsAdmin::Config::Actions.register(self) 
  Dir[Rails.root.join('lib', 'rails_admin','config','actions','*.rb')
    ].each do |file|
    require file
  end
  RailsAdmin.config do |config|
       dashboard
      index
      new
      export
      bulk_delete
      show
      edit
      <your actions here>
      delete
      show_in_app
  end
end

Avoid using to_prepare block as an alternative to after_initialze because the to_prepare block will be run again after unloading classes by the autoloader which results in rails_admin actions and models being removed from the router.

Note that require is discouraged as you should allow the autoloader to manage loading your code, and using require will exclude the required file from the autoloader. However, since rails_admin actions can't be reloaded, its fine/preferable to use require. Any code you wish to be reloadable can, of course, be placed in other files.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions