Support autoloading for custom Arbre components #716
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We recently added this patch to our Rails app, so I figured I'd open a PR to see if you'd want to consider upstreaming it into Arbre.
Motivation
Our dilemma is that we have a bunch of custom Arbre components that we use in our admin app. Since you use Arbre components by their declared "builder_method" name, and not by referencing the class, they don't play well with zeitwerk/autoloading. This means you either need to:
requirethe right arbre component manuallyWe did the latter, and it was starting to become costly toward boot time since these components inevitably wound up pulling more things into memory. It's also wasteful since these components are only ever used in our admin app, but then they'd load on any app boot (e.g. rspec, rails console, background jobs, etc). You can obviously work around these things, but the beauty of Rails autoloading means you don't have to.
Solution
This PR makes it possible to autoload these components by introducing a simple convention: a component named
foo_bar, is defined by a class namedArbre::Components::FooBarthat's on some autoload path. This approach intentionally doesn't support deeper namespacing (e.g.Arbre::Components::Foo::Bar) since:I've updated the test suite to configure autoloading and added a test that fails before this change and passes after. Happy to help update documentation or whatever else, but first wanted to just put up this PR to see if it's even something you'd consider.