You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
document UnreliableSubclasses in styleguide and changelog
include a 'good' example showing an explicit registry, plus pointers to eager loading and ActiveSupport::DescendantsTracker for cases where reflection is unavoidable.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,9 @@
1
1
# rubocop-github
2
2
3
+
## Unreleased
4
+
5
+
- Added `GitHub/UnreliableSubclasses` cop. Flags `Class#descendants` and `Class#subclasses` when the receiver is a constant. Both happily skip classes that haven't been autoloaded yet. Both also depend on GC timing for dynamically-defined classes, which is great fun in tests.
6
+
3
7
## v0.26.0
4
8
5
9
- Read the automatic release notes on [the /releases page for this gem](https://github.com/github/rubocop-github/releases).
Person.descendants # => maybe [Employee], maybe [], who knows? Not me! I never lost control.
1123
+
Person.subclasses # => same problem
1124
+
1125
+
# good. Keep an explicit registry
1126
+
classPerson < ApplicationRecord
1127
+
TYPES= []
1128
+
1129
+
defself.inherited(subclass)
1130
+
super
1131
+
TYPES<< subclass
1132
+
end
1133
+
end
1134
+
1135
+
Person::TYPES# => [Employee, ...]
1136
+
```
1137
+
1138
+
Other alternatives: eager load the dependency tree ([`Rails.application.eager_load!`](https://api.rubyonrails.org/classes/Rails/Application.html#method-i-eager_load-21) in tests, `config.eager_load = true` in prod) or use [`ActiveSupport::DescendantsTracker`](https://api.rubyonrails.org/classes/ActiveSupport/DescendantsTracker.html) directly if you really need the reflection.
0 commit comments