From 783ac66a108500001cc0421ae6fe4546ab56fa73 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Wed, 11 Dec 2024 15:04:50 +0100 Subject: [PATCH] Support Flickwerk-style patches This enables using patches (i.e. "decorators"/"overrides") in Solidus engines, but using the style described in the Flickwerk README[^1] In short, this allows autoloading patches whenever the file being patches is loaded by Zeitwerk. The new directory to put your patches into would be `app/patches`, and the files inside should be named `my_extension/spree_user_patch.rb` and define a constant `MyExtension::SpreeUserPatch`. The convention is that the module prepends itself to the class being patched, in this example `Spree::User`. [^1] https://github.com/friendlycart/flickwerk/blob/main/README.md --- lib/solidus_support/engine_extensions.rb | 13 +++++++++++++ solidus_support.gemspec | 3 +++ 2 files changed, 16 insertions(+) diff --git a/lib/solidus_support/engine_extensions.rb b/lib/solidus_support/engine_extensions.rb index cca58b4..65e52e5 100644 --- a/lib/solidus_support/engine_extensions.rb +++ b/lib/solidus_support/engine_extensions.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "active_support/deprecation" +require "flickwerk" module SolidusSupport module EngineExtensions @@ -9,6 +10,7 @@ module EngineExtensions def self.included(engine) engine.extend ClassMethods + engine.include Flickwerk engine.class_eval do solidus_decorators_root.glob('*') do |decorators_folder| @@ -106,6 +108,17 @@ def enable_solidus_engine_support(engine) end end end + + initializer "#{name}_#{engine}_patch_paths", after: "flickwerk.add_paths" do + patch_paths = root.join("lib/patches/#{engine}").glob("*") + Flickwerk.patch_paths += patch_paths + end + + initializer "#{name}_#{engine}_user_patches", before: "flickwerk.add_patches" do + Flickwerk.patches.transform_keys! do |key| + key.gsub("Spree.user_class", Spree.user_class_name) + end + end end end end diff --git a/solidus_support.gemspec b/solidus_support.gemspec index 563248d..0c97da3 100644 --- a/solidus_support.gemspec +++ b/solidus_support.gemspec @@ -28,6 +28,9 @@ Gem::Specification.new do |spec| spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] + spec.add_dependency 'flickwerk', '~> 0.2.0' + spec.add_dependency 'solidus_core', '~> 4.1' + spec.add_development_dependency 'rails' spec.add_development_dependency 'bundler' spec.add_development_dependency 'rake'