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
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 `%<replacement>s` over `%<current>s`.'
Constants included from TargetRailsVersion
TargetRailsVersion::TARGET_GEM_NAME, TargetRailsVersion::USES_REQUIRES_GEM_API
Instance Method Summary collapse
- #on_block(node) ⇒ Object (also: #on_numblock)
Methods included from TargetRailsVersion
minimum_target_rails_version, support_target_rails_version?
Instance Method Details
#on_block(node) ⇒ Object Also known as: on_numblock
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rubocop/cop/rails/pluck.rb', line 44 def on_block(node) pluck_candidate?(node) do |argument, key| next if key.regexp_type? || !use_one_block_argument?(argument) match = if node.block_type? block_argument = argument.children.first.source use_block_argument_in_key?(block_argument, key) else # numblock argument == 1 && use_block_argument_in_key?('_1', key) end next unless match register_offense(node, key) end end |