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 12 commits into
base: main
Choose a base branch
from
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

resources :media_library, only: [:index, :show, :update, :destroy], path: "media-library"
get "attach-media", to: "media_library#attach"

Expand Down
11 changes: 0 additions & 11 deletions lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,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)
mount Avo::Collaborate::Engine, at: "/collaborate" if defined?(Avo::Collaborate::Engine)
}
end

def extra_gems
[:pro, :advanced, :menu, :dynamic_filters, :dashboards, :enterprise, :audits]
end
Expand Down
2 changes: 0 additions & 2 deletions lib/avo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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
Expand Down Expand Up @@ -112,7 +111,6 @@ def initialize
@field_wrapper_layout = :inline
@resources = nil
@resource_parent_controller = "Avo::ResourcesController"
@mount_avo_engines = true
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 add a setter method to raise an error with docs link.

@cache_store = computed_cache_store
@logger = default_logger
@turbo = default_turbo
Expand Down
22 changes: 16 additions & 6 deletions lib/avo/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't need this in production.
Development will always run it twice.

@already_initialized = true

# Reset before reloads in development
::Avo.asset_manager.reset

Expand All @@ -39,12 +44,6 @@ class Engine < ::Rails::Engine
end
end

# 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
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
Expand All @@ -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)
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
mount Avo::Engine, at:, **options
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved

Avo.plugin_manager.engines.each do |engine|
mount engine[:klass], at: "#{at}/#{engine[:options].delete(:at)}", **engine[:options]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We need comments with examples.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And maybe use scope at do

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
4 changes: 0 additions & 4 deletions lib/generators/avo/templates/initializer/avo.tt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This should still be here with a warning that it's not working anymore.
Deprecation warning.

Maybe


# Where should the user be redirected when visiting the `/<%= options[:path] %>` url
# config.home_path = nil

Expand Down
2 changes: 0 additions & 2 deletions spec/dummy/config/initializers/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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