Address Zeitwerk reloading issue#3500
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.
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
RadioButtonWithNestedFormandCheckBoxWithNestedFormdefinitions to wrap their nested form classes. - Remove duplicate top-level class definitions.
- Convert the multi-line
nested_formblock 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_allandreload!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
Pull Request is not mergeable
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 constantApplicationFormbut notCustomCitiesForm- which makes it a subclass of a stale object. The next time the file is required, ruby assumesCustomCitiesFormis being redefined with a new superclass.Steps to reproduce: