Rails 6 ships with two autoloading modes:
classic. In that pull request https://github.com/discourse/discourse/pull/8083 I upgraded Rails to version 6.0.0 with classic autoloader as a transitional phase. It would be interesting to try to switch to Zeitwerk.
Zeitwerk is an efficient and thread-safe code loader for Ruby. As long as the project is following naming conventions, Zeitwerk can find correct files and load them on demand or upfront without the need for any
require_dependency. Also, it may give a small performance boost to the application according to that article Zeitwerk integration in Rails 6 (Beta 2) | Riding Rails
There are a few steps I need to do to make it work
Change the name of few classes to follow Rails naming convention. For example file,
CanonicalUrlclass instead of
CanonicalURL. Similarly file
Onpdiffclass instead of
ONPDiff. An alternative approach would be to hook custom inflector to the project, however, I think that following convention might be a better choice - GitHub - fxn/zeitwerk: Efficient and thread-safe code loader for Ruby
Similar to the previous point, by convention custom validations living in
validationsdirectory should be wrapped with
Validationsmodule. Besides, some validations are inheriting from EachValidator and should be accessible without namespace. I am planning to move them to separate directory and add to autoload paths.
require_dependencyand ensure that the project is working
requireand ensure that Discourse is working
Ensure that all plugins can access the required dependencies. I don’t know how to achieve it yet. I want to firstly make Discourse running without any plugins.
Of course, there are still plenty of unknowns to solve. I will keep you updated about progress however if you are keen to look, I started playing with that here Commits · lis2/discourse · GitHub
Please let me know if you see any drawbacks of implementing Zeitwerk or you think I missed something.