Skip to content

Commit

Permalink
Do not load ActiveJobs if ActiveJob is not loaded (#602)
Browse files Browse the repository at this point in the history
* Do not load ActiveJobs if ActiveJob is not loaded

* Do not include Broadcastable in ActiveRecord::Base if ActiveJob is not loaded
  • Loading branch information
aaronjensen authored Sep 15, 2024
1 parent 1636615 commit dadf8b5
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 223 deletions.
2 changes: 1 addition & 1 deletion app/models/concerns/turbo/broadcastable.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Turbo streams can be broadcasted directly from models that include this module (this is automatically done for Active Records).
# Turbo streams can be broadcasted directly from models that include this module (this is automatically done for Active Records if ActiveJob is loaded).
# This makes it convenient to execute both synchronous and asynchronous updates, and render directly from callbacks in models
# or from controllers or jobs that act on those models. Here's an example:
#
Expand Down
37 changes: 31 additions & 6 deletions lib/turbo/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ class Engine < Rails::Engine
#{root}/app/jobs
)

# If the parent application does not use Active Job, app/jobs cannot
# be eager loaded, because it references the ActiveJob constant.
#
# When turbo-rails depends on Rails 7 or above, the entire block can be
# reduced to
#
# unless defined?(ActiveJob)
# Rails.autoloaders.once.do_not_eager_load("#{root}/app/jobs")
# end
#
initializer "turbo.no_active_job", before: :set_eager_load_paths do
unless defined?(ActiveJob)
if Rails.autoloaders.zeitwerk_enabled?
Rails.autoloaders.once.do_not_eager_load("#{root}/app/jobs")
else
# This else branch only runs in Rails 6.x + classic mode.
config.eager_load_paths.delete("#{root}/app/jobs")
end
end
end

# If the parent application does not use Action Cable, app/channels cannot
# be eager loaded, because it references the ActionCable constant.
#
Expand Down Expand Up @@ -70,8 +91,10 @@ class Engine < Rails::Engine
end

initializer "turbo.broadcastable" do
ActiveSupport.on_load(:active_record) do
include Turbo::Broadcastable
ActiveSupport.on_load(:active_job) do
ActiveSupport.on_load(:active_record) do
include Turbo::Broadcastable
end
end
end

Expand Down Expand Up @@ -99,10 +122,12 @@ class Engine < Rails::Engine
include Turbo::TestAssertions
end

ActiveSupport.on_load(:action_cable) do
ActiveSupport.on_load(:active_support_test_case) do
require "turbo/broadcastable/test_helper"
include Turbo::Broadcastable::TestHelper
ActiveSupport.on_load(:active_job) do
ActiveSupport.on_load(:action_cable) do
ActiveSupport.on_load(:active_support_test_case) do
require "turbo/broadcastable/test_helper"
include Turbo::Broadcastable::TestHelper
end
end
end

Expand Down
Loading

0 comments on commit dadf8b5

Please sign in to comment.