Class: RuboCop::Cop::Rails::Pluck
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::Pluck
- Extended by:
- AutoCorrector, TargetRailsVersion
- Defined in:
- lib/rubocop/cop/rails/pluck.rb
Overview
This cop enforces the use of `pluck` over `map`.
`pluck` can be used instead of `map` to extract a single key from each element in an enumerable. When called on an Active Record relation, it results in a more efficient query that only selects the necessary key.
Constant Summary collapse
- MSG =
'Prefer `pluck(:%<value>s)` over `%<method>s { |%<argument>s| %<element>s[:%<value>s] }`.'
Instance Method Summary collapse
Methods included from TargetRailsVersion
minimum_target_rails_version, support_target_rails_version?
Instance Method Details
#on_block(node) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rubocop/cop/rails/pluck.rb', line 32 def on_block(node) pluck_candidate?(node) do |method, argument, element, value| next unless argument == element = (method, argument, element, value) add_offense(offense_range(node), message: ) do |corrector| corrector.replace(offense_range(node), "pluck(:#{value})") end end end |