diff --git a/lib/flickwerk.rb b/lib/flickwerk.rb index bcdf750..b42a901 100644 --- a/lib/flickwerk.rb +++ b/lib/flickwerk.rb @@ -9,6 +9,7 @@ class Error < StandardError; end mattr_accessor :patch_paths, default: [] mattr_accessor :patches, default: Hash.new([]) + mattr_accessor :aliases, default: {} def self.included(engine) engine.root.glob("app/patches/*").each do |path| @@ -17,6 +18,7 @@ def self.included(engine) end def self.patch(class_name, with:) - patches[class_name] += [with] + klass_name = aliases[class_name] || class_name + patches[klass_name] += [with] end end diff --git a/lib/flickwerk/railtie.rb b/lib/flickwerk/railtie.rb index 9005e77..9a3a7ea 100644 --- a/lib/flickwerk/railtie.rb +++ b/lib/flickwerk/railtie.rb @@ -10,13 +10,17 @@ class Flickwerk::Railtie < Rails::Railtie end end - initializer "flickwerk.find_patches", after: :setup_main_autoloader do - Flickwerk.patch_paths.each do |path| - Flickwerk::PatchFinder.new(path).call + initializer "flickwerk.find_patches" do |app| + app.config.to_prepare do + Flickwerk.patch_paths.each do |path| + Flickwerk::PatchFinder.new(path).call + end end end - initializer "flickwerk.add_patches", after: "flickwerk.find_patches" do - Flickwerk::PatchLoader.call + initializer "flickwerk.add_patches", after: "flickwerk.find_patches" do |app| + app.config.to_prepare do + Flickwerk::PatchLoader.call + end end end diff --git a/test/dummy_app/app/models/ui/button.rb b/test/dummy_app/app/models/ui/button.rb new file mode 100644 index 0000000..703735f --- /dev/null +++ b/test/dummy_app/app/models/ui/button.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module UI + class Button + def click + "Button clicked!" + end + end +end diff --git a/test/dummy_app/app/patches/models/user_patch.rb b/test/dummy_app/app/patches/models/user_patch.rb index 71380ca..8492c73 100644 --- a/test/dummy_app/app/patches/models/user_patch.rb +++ b/test/dummy_app/app/patches/models/user_patch.rb @@ -7,5 +7,5 @@ def age 26 end - User.prepend(self) + DummyApp.user_class.prepend(self) end diff --git a/test/dummy_app/config/application.rb b/test/dummy_app/config/application.rb index 704fc73..fdaf3d7 100644 --- a/test/dummy_app/config/application.rb +++ b/test/dummy_app/config/application.rb @@ -2,9 +2,14 @@ require "flickwerk/railtie" module DummyApp + def self.user_class + User + end + class Application < ::Rails::Application config.root = File.expand_path("../", __dir__) include Flickwerk + Flickwerk.aliases["DummyApp.user_class"] = "User" config.autoload_paths << File.expand_path("../app/models", __dir__) config.load_defaults("#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}") diff --git a/test/dummy_app/config/initializers/inflections.rb b/test/dummy_app/config/initializers/inflections.rb new file mode 100644 index 0000000..852db56 --- /dev/null +++ b/test/dummy_app/config/initializers/inflections.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "UI" } diff --git a/test/test_flickwerk.rb b/test/test_flickwerk.rb index e12c310..f547c72 100644 --- a/test/test_flickwerk.rb +++ b/test/test_flickwerk.rb @@ -61,4 +61,12 @@ def boot Flickwerk.patch("User", with: "UserPatch") assert_equal ["UserPatch"], Flickwerk.patches["User"] end + + test "acronyms still work" do + boot + + assert UI::Button + assert UI::Button.new.respond_to?(:click) + assert_equal "Button clicked!", UI::Button.new.click + end end