Mise à niveau de Discourse vers Zeitwerk

In my pull requests for plugins, you will notice that a lot of fixes are around searching in the global namespace.

I changed code like

module ::Jobs
  class TranslatorMigrateToAzurePortal < Jobs::Onceoff

to

module ::Jobs
  class TranslatorMigrateToAzurePortal < ::Jobs::Onceoff

One thing bothered me about that solution, why was it working before Zeitwerk. That question when something works but shouldn’t is always tricky :slight_smile:

I think I found a potential answer in the description of the classic autoloader (Autoloading and Reloading Constants (Classic Mode) — Ruby on Rails Guides) - “If not found, then the algorithm walks up the ancestor chain of the cref”

Zeitwerk is more strict. Once I tried to load code before fix it was complaining that Jobs::Jobs::Onceoff can not be found.

Sam suggested under pull request https://github.com/discourse/discourse-prometheus-alert-receiver/commit/ef9c238a6fabc3a9ca238664adb3dc594dc709ee, that instead of using < ::Jobs::Onceoff we can just use < Onceoff and he is right. I checked that without namespace it works as well. I am thinking that giving :: is explicitly saying that we are inheriting from Discourse Core class, however, we can go either way.

4 « J'aime »