Skip to content

[Fix #1612] Add Rails/ActiveStorageAttachedDependent cop#1613

Open
melody-universe wants to merge 1 commit into
rubocop:masterfrom
melody-universe:add-active-storage-attached-dependent-cop
Open

[Fix #1612] Add Rails/ActiveStorageAttachedDependent cop#1613
melody-universe wants to merge 1 commit into
rubocop:masterfrom
melody-universe:add-active-storage-attached-dependent-cop

Conversation

@melody-universe
Copy link
Copy Markdown

@melody-universe melody-universe commented May 3, 2026

🤖 This PR and its preceding issue were written with some heavy assistance from Claude, but I have personally reviewed and edited both.

Adds Rails/ActiveStorageAttachedDependent, a new cop that flags has_one_attached / has_many_attached declarations whose dependent: value silently no-ops in ActiveStorage.

Rails stores the kwarg on the reflection, but only the consumer ActiveStorage::Attachment#purge_dependent_blob_later reads it back, and only with strict equality on :purge_later. Anything else (true, nil, :destroy, arbitrary symbols) leaves orphan blobs in storage when the parent record is destroyed.

The cop does not autocorrect: every offending value is runtime-equivalent (all silent no-ops against the strict == :purge_later check), so the right replacement depends on the user's intent. dependent: :purge is reported at warning severity per-offense, citing rails/rails#36423.

See #1612 for the full motivation, including a real-world example.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide. — deferred to a follow-up.

Flags `has_one_attached` / `has_many_attached` declarations whose
`dependent:` value silently no-ops in ActiveStorage. Rails stores
the kwarg on the reflection, but only the consumer
`ActiveStorage::Attachment#purge_dependent_blob_later` reads it,
and only with strict equality on `:purge_later`. Anything else
(`true`, `nil`, `:destroy`, arbitrary symbols) leaves orphan blobs
in storage when the parent record is destroyed.

The cop does not autocorrect — every value other than `:purge_later`
and `false` is runtime-equivalent (all silent no-ops), so the right
replacement depends on the user's intent. `dependent: :purge` is
reported at warning severity, citing rails/rails#36423.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@koic
Copy link
Copy Markdown
Member

koic commented May 9, 2026

Could you first propose this style to the style guide?
https://github.com/rubocop/rails-style-guide

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.

2 participants