-
Notifications
You must be signed in to change notification settings - Fork 126
Address Zeitwerk reloading issue #3500
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
base: main
Are you sure you want to change the base?
Conversation
|
I have observed when calling `reload!` in a Rails console (after the gem is loaded) results in an error: ``` Uncaught exception: TypeError - superclass mismatch for class CustomCitiesForm ``` I suspect this occurs because Zeitwerk expects a [single constant per file](https://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#one-file-one-constant-at-the-same-top-level). Calling `reload!` will remove the constant `ApplicationForm`, but NOT `CustomCitiesForm` - which is now the subclass of a [stale object](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#reloading-and-stale-objects). The next time file is required, ruby assumes `CustomCitiesForm` is being redefined with a new superclass. Steps to reproduce: ``` Zeitwerk::Loader.eager_load_all reload! Zeitwerk::Loader.eager_load_all ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR nests form subclasses under their parent form classes to satisfy Zeitwerk’s one-constant-per-file requirement and prevent stale-constant reload errors.
- Move the outer
RadioButtonWithNestedForm
andCheckBoxWithNestedForm
definitions to wrap their nested form classes. - Remove duplicate top-level class definitions.
- Convert the multi-line
nested_form
block to a single-line inline block in the checkbox form.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
app/forms/radio_button_with_nested_form.rb | Define RadioButtonWithNestedForm at the top, nest FriendForm , remove duplicate class def |
app/forms/check_box_with_nested_form.rb | Define CheckBoxWithNestedForm at the top, nest CustomCitiesForm , inline nested_form block |
Comments suppressed due to low confidence (3)
app/forms/radio_button_with_nested_form.rb:22
- FriendTextAreaForm remains at the top level rather than nested under RadioButtonWithNestedForm. Nest this class inside RadioButtonWithNestedForm to ensure it reloads together with its parent and avoid superclass mismatch on
reload!
.
class FriendTextAreaForm < ApplicationForm
app/forms/radio_button_with_nested_form.rb:4
- Add a regression test that calls
Zeitwerk::Loader.eager_load_all
andreload!
in a Rails console context to verify the TypeError no longer occurs after nesting.
class RadioButtonWithNestedForm < ApplicationForm
app/forms/radio_button_with_nested_form.rb:1
- [nitpick] Include a brief comment explaining that the nested class structure is required to satisfy Zeitwerk’s one-file-one-constant reload semantics.
# frozen_string_literal: true
I observed when calling
reload!
in a Rails console (after the gem is eager loaded) results in an error:I suspect this occurs because Zeitwerk expects a single constant per file. Calling
reload!
will remove the constantApplicationForm
but notCustomCitiesForm
- which makes it a subclass of a stale object. The next time the file is required, ruby assumesCustomCitiesForm
is being redefined with a new superclass.Steps to reproduce: