Class: RuboCop::Cop::Rails::EnumPrefix
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::EnumPrefix
- Defined in:
- lib/rubocop/cop/rails/enum_prefix.rb
Overview
Enforces the use of ‘_prefix` with any enum. Using a prefix helps prevent overriding of existing methods Example: enum some_enum: { open: 0, each: 1 }
Will cause an error because of overlap with existing methods such as ‘open` and `each` For more information, see github.com/rails/rails/issues/13389#issue-24527737
# bad
enum my_enum: {some_value: 0, another: 1}
# good
enum my_enum: {some_value: 0, another: 1}, _prefix: true
# good
enum my_enum: {some_value: 0, another: 1}, _prefix: :my_prefix
# good
enum my_enum: {some_value: 0, another: 1}, _suffix: true
# good
enum my_enum: {some_value: 0, another: 1}, _suffix: 'another_suffix'
Constant Summary collapse
- MSG =
'Prefer using a `_prefix` or `_suffix` with `enum`.'
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
38 39 40 41 42 |
# File 'lib/rubocop/cop/rails/enum_prefix.rb', line 38 def on_send(node) return unless enum? node add_offense(node) unless use_prefix_or_suffix? node end |