Skip to content

Commit

Permalink
refactor: better engine mounting
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Dec 15, 2024
1 parent 42630ec commit 69e0f5a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
5 changes: 0 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,20 @@ def has_profile_menu?
end

# Mount all Avo engines
# TODO: We can remove the content of this method to keep back compatibility
# Add deprecation warning
def mount_engines
-> {
mount Avo::Engine, at: Avo.configuration.root_path

Avo.plugin_manager.engines.dup.each do |engine|
mount engine[:klass], **engine[:options]
end
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)
# mount Avo::Collaborate::Engine, at: "/collaborate" if defined?(Avo::Collaborate::Engine)
}
end

Expand Down
10 changes: 9 additions & 1 deletion lib/avo/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Engine < ::Rails::Engine
# Ensure we reboot the app when something changes
config.to_prepare do
# Boot Avo
::Avo.boot
# TODO: I think we can remove this
# ::Avo.boot
end

initializer "avo.autoload" do |app|
Expand All @@ -59,6 +60,13 @@ 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
instance_exec(&Avo.mount_engines)
end
})
end

initializer "avo.reloader" do |app|
Expand Down
18 changes: 18 additions & 0 deletions lib/avo/plugin_manager.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
module Avo
class PluginManager
attr_reader :plugins
# attr_reader :engines

alias_method :all, :plugins

def initialize
puts ["new PluginManager->"].inspect
@plugins = []
@engines = []
end

def engines=(value)
puts ["setter engines=->", value].inspect
@engines = value
end

def engines
puts ["getter engines->", @engines].inspect
@engines
end

def register(name, priority: 10)
Expand Down Expand Up @@ -50,6 +63,11 @@ def installed?(name)
plugin.name.to_s == name.to_s
end
end

def mount_engine(klass, **options)
puts ["mount_engine->", klass, options].inspect
@engines << { klass:, options: }
end
end

def self.plugin_manager
Expand Down

0 comments on commit 69e0f5a

Please sign in to comment.