Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: better engine mounting #3533

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[rubocop] reported by reviewdog 🐶
[Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside { detected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[rubocop] reported by reviewdog 🐶
[Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside } detected.

end
end

def self.plugin_manager
Expand Down
Loading