rails: Problem trying to upgrade Rails application to 6.1.4.1 and autoloading with zeitwerk

I’m trying to upgrade Rails to version 6.1.4.1 with Ruby 2.7.0 and I’m finding some problems with the new zeitwerk autoloading.

Specifically I have a file that is executing some code using class_eval for a class that is defined in a different file:

MyApp::Menu.class_eval do
  # Some code to be evaluated at class level
end

This is some kind of “extension” over MyApp::Menu class (defined in a different file). My problem is that I don’t know how to fit this with how zeitwerk autoload works. This file doesn’t defines any class/constant/module and zeitwerk is expecting something to be defined raising an error like:

NameError: uninitialized constant MyApp::Menu

How should I fix this allowing zeitwerk to work with these kind of files where I’m not defining anything but instead reopening or modifying an already created class?

According to Rails guides I should ping @fxn 😃

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@torukMnk awesome 😃.

I see this use case is not included in the Classic to Zeitwerk HOWTO guide, I’ll add it!