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

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
10 changes: 0 additions & 10 deletions lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,6 @@ def has_profile_menu?
true
end

# Mount all Avo engines
def mount_engines
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same. Raise and error with docs links.

-> {
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
Expand Down
14 changes: 13 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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

don't we still need this for reloading the code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let's run this only in development

# Boot Avo
::Avo.boot
# TODO: I think we can remove this
# ::Avo.boot
end

initializer "avo.autoload" do |app|
Expand All @@ -59,6 +60,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|
Expand Down
6 changes: 6 additions & 0 deletions lib/avo/plugin_manager.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading