Guard enum_for's pluralized accessor with respond_to? - #77
Merged
Merged
Conversation
`Generator#enum_for` decided whether to emit `Model.pluralized_name.keys` by checking only that the pluralized name matched a Ruby identifier regex, duplicating (but not fully mirroring) ColumnGuard's `enum_keys_expr`. A field whose plural form is a valid identifier but has no actual accessor on the model — renamed or otherwise excluded — made the generator emit a call that raises NoMethodError when the draft loads, a case ColumnGuard's sibling code already guards against via `klass.respond_to?(reader)`. Add the same respond_to? check before emitting the pluralized-accessor expression, falling back to the `defined_enums[...]` spelling exactly as enum_keys_expr does. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Generator#enum_for(lib/permittable/generator.rb, around line 517) decides how to reference an enum's keys in the drafted code — either the model's pluralized class accessor (Model.statuses.keys) or the always-safeModel.defined_enums["status"].keysfallback. It picks between them by checking only that the pluralized field name matches a Ruby identifier regex (METHOD_NAME).This duplicates
Permittable::ColumnGuard#enum_keys_expr(lib/permittable/column_guard.rb), which makes the identical choice for its own error messages — but that sibling copy also checksklass.respond_to?(reader), andenum_fordoes not.A field name whose plural form matches the identifier regex but has no actual accessor on the model — the enum accessor was renamed or otherwise excluded — made
enum_forstill emitModel.pluralized_name.keys. That code gets written straight into the generated draft and raisesNoMethodErrorthe moment the draft loads, even thoughColumnGuard's parallel logic already guards against exactly this case.The fix
Add the same
model.respond_to?(plural)checkenum_keys_expralready carries, right alongside the existing identifier check, before emitting the pluralized-accessor expression:When the accessor isn't callable,
enum_fornow falls back to the samedefined_enums[...]spellingenum_keys_expruses in its own not-respond_to? case, which is always safe to call.Verification
spec/generator_spec.rb): an enum whose pluralized name (statuses) is undefined on the model (simulating a renamed/excluded accessor) — confirmed to fail before the fix (draftedGenOrderNoReader.statuses.keys, which the loaded draft can't answer to) and pass after (draftsGenOrderNoReader.defined_enums["status"].keysinstead, and the loaded draft correctly accepts/rejects enum values)bundle exec rspec— 852 examples, 0 failuresbundle exec rubocop lib/permittable/generator.rb spec/generator_spec.rb— no offenses🤖 Generated with Claude Code