diff --git a/config/routes.rb b/config/routes.rb index e5480b211..02637333e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,11 +4,6 @@ get "resources", to: redirect(Avo.configuration.root_path) get "dashboards", to: redirect(Avo.configuration.root_path) - # Mount Avo engines routes by default but leave it configurable in case the user wants to nest these under a scope. - if Avo.configuration.mount_avo_engines - instance_exec(&Avo.mount_engines) - end - post "/rails/active_storage/direct_uploads", to: "/active_storage/direct_uploads#create" scope "avo_api", as: "avo_api" do diff --git a/lib/avo.rb b/lib/avo.rb index 58aa29743..7fcd04599 100644 --- a/lib/avo.rb +++ b/lib/avo.rb @@ -130,16 +130,6 @@ def has_profile_menu? true end - # Mount all Avo engines - def mount_engines - -> { - 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) - mount Avo::Kanban::Engine, at: "/boards" if defined?(Avo::Kanban::Engine) - } - end - def extra_gems [:pro, :advanced, :menu, :dynamic_filters, :dashboards, :enterprise, :audits] end diff --git a/lib/avo/configuration.rb b/lib/avo/configuration.rb index 6176103d8..b16a22a22 100644 --- a/lib/avo/configuration.rb +++ b/lib/avo/configuration.rb @@ -49,7 +49,6 @@ class Configuration attr_accessor :resources attr_accessor :prefix_path attr_accessor :resource_parent_controller - attr_accessor :mount_avo_engines attr_accessor :default_url_options attr_accessor :click_row_to_view_record attr_accessor :alert_dismiss_time @@ -111,7 +110,6 @@ def initialize @field_wrapper_layout = :inline @resources = nil @resource_parent_controller = "Avo::ResourcesController" - @mount_avo_engines = true @cache_store = computed_cache_store @logger = default_logger @turbo = default_turbo diff --git a/lib/avo/engine.rb b/lib/avo/engine.rb index 1a03ce096..eaa33acbd 100644 --- a/lib/avo/engine.rb +++ b/lib/avo/engine.rb @@ -21,6 +21,11 @@ class Engine < ::Rails::Engine isolate_namespace Avo config.after_initialize do + # This callback is triggered 2 times + # This flag check will avoid to re-execute the logic + next if @already_initialized + @already_initialized = true + # Reset before reloads in development ::Avo.asset_manager.reset @@ -39,12 +44,6 @@ class Engine < ::Rails::Engine end end - # Ensure we reboot the app when something changes - config.to_prepare do - # Boot Avo - ::Avo.boot - end - initializer "avo.autoload" do |app| # This undoes Rails' previous nested directories behavior in the `app` dir. # More on this: https://github.com/fxn/zeitwerk/issues/250 @@ -59,6 +58,17 @@ class Engine < ::Rails::Engine app.config.watchable_dirs[directory_path] = [:rb] end end + + # Add the mount_avo method to Rails + ActionDispatch::Routing::Mapper.include(Module.new { + def mount_avo(at: Avo.configuration.root_path, **options) + mount Avo::Engine, at:, **options + + Avo.plugin_manager.engines.each do |engine| + mount engine[:klass], **engine[:options] + end + end + }) end initializer "avo.reloader" do |app| diff --git a/lib/avo/plugin_manager.rb b/lib/avo/plugin_manager.rb index c35a43527..a7fde3bba 100644 --- a/lib/avo/plugin_manager.rb +++ b/lib/avo/plugin_manager.rb @@ -1,11 +1,13 @@ module Avo class PluginManager attr_reader :plugins + attr_accessor :engines alias_method :all, :plugins def initialize @plugins = [] + @engines = [] end def register(name, priority: 10) @@ -50,6 +52,10 @@ def installed?(name) plugin.name.to_s == name.to_s end end + + def mount_engine(klass, **options) + @engines << {klass:, options:} + end end def self.plugin_manager diff --git a/lib/generators/avo/templates/initializer/avo.tt b/lib/generators/avo/templates/initializer/avo.tt index dfe93766d..7920e98c2 100644 --- a/lib/generators/avo/templates/initializer/avo.tt +++ b/lib/generators/avo/templates/initializer/avo.tt @@ -6,10 +6,6 @@ Avo.configure do |config| # used only when you have custom `map` configuration in your config.ru # config.prefix_path = "/internal" - # Sometimes you might want to mount Avo's engines yourself. - # https://docs.avohq.io/3.0/routing.html - # config.mount_avo_engines = true - # Where should the user be redirected when visiting the `/<%= options[:path] %>` url # config.home_path = nil diff --git a/spec/dummy/config/initializers/avo.rb b/spec/dummy/config/initializers/avo.rb index 0823ef04b..29c99ec7a 100644 --- a/spec/dummy/config/initializers/avo.rb +++ b/spec/dummy/config/initializers/avo.rb @@ -2,9 +2,7 @@ ## == Base configs == config.root_path = "/admin" config.app_name = -> { "Avocadelicious #{params[:app_name_suffix]}" } - config.home_path = -> { "/admin/resources/projects" } config.home_path = -> { avo.resources_projects_path } - # config.mount_avo_engines = false # config.default_url_options = [:tenant_id] # Use this to test root_path_without_url helper # Also enable in config.ru & application.rb diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 1dad6d2d4..47b80e857 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -12,17 +12,18 @@ get "custom_tool", to: "avo/tools#custom_tool", as: :custom_tool end - mount Avo::Engine, at: Avo.configuration.root_path + mount_avo + # Uncomment to test constraints /123/en/admin # scope ":course", constraints: {course: /\w+(-\w+)*/} do # scope ":locale", constraints: {locale: /\w[-\w]*/} do - # mount Avo::Engine, at: Avo.configuration.root_path + # mount_avo # end # end # TODO: support locale based routes scope "(:locale)" do - # mount Avo::Engine, at: Avo.configuration.root_path + # mount_avo end end end