Skip to content

Fix RailsModelMatcher to recognize slash notation in ActiveRecord attribute translations#703

Merged
glebm merged 2 commits intoglebm:mainfrom
wagner:fix-702
Jan 12, 2026
Merged

Fix RailsModelMatcher to recognize slash notation in ActiveRecord attribute translations#703
glebm merged 2 commits intoglebm:mainfrom
wagner:fix-702

Conversation

@wagner
Copy link
Contributor

@wagner wagner commented Jan 7, 2026

Fixes #702

Problem

The RailsModelMatcher incorrectly reported missing translations when using Model.human_attribute_name with dot notation that should resolve to slash notation in locale files.

Rails' human_attribute_name method has special logic that converts dot notation to slash notation when looking up nested attribute translations. This is commonly used for STI (Single Table Inheritance) models and enum-like nested attributes.

Example

Locale file (config/locales/en.yml):

en:
  activerecord:
    attributes:
      product/status:
        active: Active
        inactive: Inactive

View code:

<%= Product.human_attribute_name("status.active") %>
# => "Active"

Before this fix:

  • i18n-tasks looked for: activerecord.attributes.product.status.active
  • Incorrectly reported as missing

After this fix:

  • i18n-tasks correctly looks for: activerecord.attributes.product/status.active
  • No false positives

Solution

Updated both the AST-based (RailsModelMatcher) and Prism-based scanners to detect when an attribute name contains a dot and convert the separator between the model name and attribute to a slash, matching Rails' internal behavior.

Changes

  • lib/i18n/tasks/scanners/ast_matchers/rails_model_matcher.rb: Added logic to use slash separator when attribute contains a dot
  • lib/i18n/tasks/scanners/prism_scanners/visitor.rb: Applied the same conversion logic for the Prism scanner
  • Test coverage: Added comprehensive tests for both Ruby and ERB files to ensure the slash notation is correctly recognized

Testing

  • ✅ All existing tests pass (257 examples, 0 failures)
  • ✅ Added new test cases for slash notation pattern
  • ✅ Tested with both AST and Prism-based scanners
  • ✅ Verified backward compatibility: regular attributes without dots continue to work as before

Test Cases Added

  1. Ruby files: Product.human_attribute_name("status.active")
  2. ERB files: <%= Product.human_attribute_name("status.active") %>
  3. Both scanners (AST and Prism) recognize the pattern correctly

Backward Compatibility

This change is fully backward compatible. Attributes without dots continue to use the standard dot notation:

  • Archive.human_attribute_name(:name)activerecord.attributes.archive.name

References

Copy link
Collaborator

@davidwessman davidwessman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🙂 Thank you for fixing this 🙂

Please fix the rubocop errors, then I wonder if we need to skip the candidate key for the human_attribute_name if we have the dot-notation. Can you double check the behaviour in a Rails app?.

]
)
)
describe "key verification" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of changes here because I had to split the tests: Spec/ExampleLength: Example has too many lines. [158/140]

Comment on lines +174 to +176
candidate_keys: [
"activerecord.attributes.product/status.active"
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reorganization made it hard to see, but here's the only actual change in the test. 👆

@wagner wagner requested a review from davidwessman January 12, 2026 13:23
Copy link
Collaborator

@davidwessman davidwessman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🙂 thank you!

@glebm glebm merged commit 799cc33 into glebm:main Jan 12, 2026
8 checks passed
@wagner wagner deleted the fix-702 branch January 27, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RailsModelMatcher doesn't recognize slash notation in ActiveRecord attribute translations

3 participants