Skip to content

Commit

Permalink
Eager load patches for older solidus versions
Browse files Browse the repository at this point in the history
Solidus versions < 4.5 `require` some of their application code.
This leads to hard-to-debug problems with Flickwerk patches.
What this does is eager-load all the patches in a `to_prepare`
hook by constantizing them.
  • Loading branch information
mamhoff committed Jan 24, 2025
1 parent b140fb0 commit 3c30516
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/solidus_support/engine_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ def activate
# This allows to add event subscribers to extensions without explicitly subscribing them,
# similarly to what happens in Solidus core.
def load_solidus_subscribers_from(path)
if SolidusSupport::LegacyEventCompat.using_legacy?
path.glob("**/*_subscriber.rb") do |subscriber_path|
require_dependency(subscriber_path)
end
return unless SolidusSupport::LegacyEventCompat.using_legacy?

if Spree::Event.respond_to?(:activate_all_subscribers)
Spree::Event.activate_all_subscribers
else
Spree::Event.subscribers.each(&:subscribe!)
end
path.glob("**/*_subscriber.rb") do |subscriber_path|
require_dependency(subscriber_path)
end

if Spree::Event.respond_to?(:activate_all_subscribers)
Spree::Event.activate_all_subscribers
else
Spree::Event.subscribers.each(&:subscribe!)
end
end

Expand Down Expand Up @@ -139,6 +139,19 @@ def enable_solidus_engine_support(engine)
Flickwerk.aliases["Spree.user_class"] = Spree.user_class_name
end
end

initializer "eager_load_#{engine_name}_#{engine}_patches", after: "flickwerk.find_patches" do |app|
# Solidus versions < 4.5 `require` some of their application code.
# This leads to hard-to-debug problems with Flickwerk patches.
# What this does is eager-load all the patches in a `to_prepare`
# hook by constantizing them.
# You can override this behavior by setting the environment variable `SOLIDUS_LAZY_LOAD_PATCHES`.
if Spree.solidus_gem_version < Gem::Version.new("4.5.0.a") && !ENV["SOLIDUS_LAZY_LOAD_PATCHES"]
app.reloader.to_prepare do
Flickwerk.patches.each_value { _1.each(&:constantize) }
end
end
end
end
end
end
Expand Down

0 comments on commit 3c30516

Please sign in to comment.