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

Fix startup issues #3

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/flickwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
14 changes: 9 additions & 5 deletions lib/flickwerk/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions test/dummy_app/app/models/ui/button.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module UI
class Button
def click
"Button clicked!"
end
end
end
2 changes: 1 addition & 1 deletion test/dummy_app/app/patches/models/user_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ def age
26
end

User.prepend(self)
DummyApp.user_class.prepend(self)
end
5 changes: 5 additions & 0 deletions test/dummy_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
3 changes: 3 additions & 0 deletions test/dummy_app/config/initializers/inflections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "UI" }
8 changes: 8 additions & 0 deletions test/test_flickwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading