railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.9k stars 2.26k forks source link

Moving configuration outside of initializers/rails_admin.rb breaks the panel #3498

Open RocKhalil opened 2 years ago

RocKhalil commented 2 years ago

Describe the bug We use configuration files for Rails Admin instead of putting all the config in initializers/rails_admin.rb; After 3.0.0 update, this stopped working as it's overriding the other config.

Reproduction steps In initializer/rails_admin.rb add this line:

Dir["#{Rails.root}/lib/rails_admin/configuration/*.rb"].map { |f| [f, require(f)] }

Then create this file in lib/rails_admin/configuration/user.rb

navigation_label = 'Users'

RailsAdmin.config do |config|
  config.model User do
    navigation_label navigation_label
    weight 1
  end
end

You will get: No route matches [GET] "/admin/user"

Expected behavior The configuration should work and shouldn't affect the existing config.

Additional context

codealchemy commented 2 years ago

This looks to be a dupe of https://github.com/railsadminteam/rails_admin/issues/3474

Per the last comment there

You config happened to work in the past implementation, but since we introduced the dynamic config reload feature we have no way other than to reset it with every invocation of actions.

RocKhalil commented 2 years ago

@codealchemy makes sense.. I'll try to see how to re-do this with the latest changes. For me, it makes no sense to have all the config in one big file;

Example: 30 models with specific config for each one 10 custom actions etc...

codealchemy commented 2 years ago

👍🏼 Got it - just a heads up that there is an open issue (https://github.com/railsadminteam/rails_admin/issues/3490) regarding ordering of the config and any associations used (in case you're considering moving the model configs to the models themselves).

gabrieletassoni commented 1 year ago

@RocKhalil did you sorted this out? As I can understand, this does not work anymore: https://github.com/railsadminteam/rails_admin/wiki/Actions#use-existing-actions-and-customize-them

Also I have a lot of config.actions {} blocks spread among intializers files in different gems and consolidating all in one rails_admin.rb file makes adding features by different dev teams working on different gems a little pain to manage, since there always be a master intializer that must me maintained and informed of new root actions etc. instead of being provided dynamically by the initializer files from each included gem.

RocKhalil commented 1 year ago

@gabrieletassoni unfortunately, I didn't touch this as in newer projects I'm directly adding custom actions in the rails_admin file. it looks so big, but it was the fastest way for me to move forward in what I'm working on.

gabrieletassoni commented 1 year ago

Hi @RocKhalil I managed to find a way to do it and to set RailsAdmin Configurations from engines (gems), I put this code in an after_initialize block in config/initializers folder in the gem(s) like this (so actually configuring from another gem, without the need to touch the generated rails_admin.rb initializer in app):

Rails.application.configure do
    config.after_initialize do
        # Freeze more or fewer columns (col 1 = checkboxes, 2 = links/actions) for horizontal scrolling:
        RailsAdmin::Config.sidescroll = {num_frozen_columns: 2}

        RailsAdmin::Config.main_app_name = Proc.new { |controller| [ ((ENV["APP_NAME"].presence || Settings.app_name.presence) rescue "Thecore"), "" ] }
        # Link for background Job
        # (config.navigation_static_links ||= {}).merge! "Background Monitor" => "#{ENV["BACKEND_URL"].presence || "http://localhost:3000"}/sidekiq"

        ### Popular gems integration
        ## == Devise ==
        RailsAdmin::Config.authenticate_with do
            warden.authenticate! scope: :user
        end
        RailsAdmin::Config.current_user_method(&:current_user)

        ## == Cancan ==
        RailsAdmin::Config.authorize_with :cancancan

        ## == PaperTrail ==
        # config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0
        RailsAdmin::Config.show_gravatar = false
        ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration
        RailsAdmin::Config.label_methods.unshift(:display_name)

        RailsAdmin::Config::Actions.add_action "active_job_monitor", :base, :root do
            show_in_sidebar true
            show_in_navigation false
            breadcrumb_parent [nil]
            # This ensures the action only shows up for Users
            # visible? authorized?
            # Not a member action
            member false
            # Not a colleciton action
            collection false

            link_icon 'fas fa-eye'

            # You may or may not want pjax for your action
            # pjax? true

            http_methods [:get]
            # Adding the controller which is needed to compute calls from the ui
            controller do
                proc do # This is needed because we need that this code is re-evaluated each time is called
                    puts "Loading Active Job Monitor Controller"
                end
            end
        end
    end
end

And this inject as needed, dynamically, the configurations and also, as you can see, a root action using the RailsAdmin::Config::Actions.add_action method (actual controller code omitted for brevity).

For example, in another gem, I made a new after_initialize block and added the root action which i need to add from there and not in the main app or the other gem described above (so, adding just the needed root action):

Rails.application.configure do
    config.after_initialize do
        RailsAdmin::Config::Actions.add_action "thecore_spot_items_importer", :base, :root do
            show_in_sidebar true
            show_in_navigation false
            breadcrumb_parent [nil]
            # This ensures the action only shows up for Users
            # visible? authorized?
            # Not a member action
            member false
            # Not a colleciton action
            collection false

            link_icon 'fas fa-upload'

            # You may or may not want pjax for your action
            # pjax? true

            http_methods [:get, :post]
            # Adding the controller which is needed to compute calls from the ui
            controller do
                proc do # This is needed because we need that this code is re-evaluated each time is called
                    puts "Other Root action here"
                end
            end
        end
    end
end