Skip to content

Commit

Permalink
Merge pull request #862 from r7kamura/feature/dynamic-find-by-and
Browse files Browse the repository at this point in the history
Ignore if number of arguments does not match on `Rails/DynamicFindBy`
  • Loading branch information
koic authored Nov 13, 2022
2 parents 86e90c5 + 742cbf3 commit c9abc2a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#862](https://github.com/rubocop/rubocop-rails/pull/862): Ignore if number of arguments does not match on `Rails/DynamicFindBy`. ([@r7kamura][])
22 changes: 16 additions & 6 deletions lib/rubocop/cop/rails/dynamic_find_by.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def on_send(node)
method_name = node.method_name
static_name = static_method_name(method_name)
return unless static_name
return if node.arguments.any? { |argument| IGNORED_ARGUMENT_TYPES.include?(argument.type) }
return unless dynamic_find_by_arguments?(node)

message = format(MSG, static_name: static_name, method: method_name)
add_offense(node, message: message) do |corrector|
Expand All @@ -65,12 +65,8 @@ def on_send(node)
private

def autocorrect(corrector, node)
keywords = column_keywords(node.method_name)

return if keywords.size != node.arguments.size

autocorrect_method_name(corrector, node)
autocorrect_argument_keywords(corrector, node, keywords)
autocorrect_argument_keywords(corrector, node, column_keywords(node.method_name))
end

def allowed_invocation?(node)
Expand Down Expand Up @@ -120,6 +116,20 @@ def static_method_name(method_name)

match[2] ? 'find_by!' : 'find_by'
end

def dynamic_find_by_arguments?(node)
dynamic_find_by_arguments_count?(node) && dynamic_find_by_arguments_type?(node)
end

def dynamic_find_by_arguments_count?(node)
column_keywords(node.method_name).size == node.arguments.size
end

def dynamic_find_by_arguments_type?(node)
node.arguments.none? do |argument|
IGNORED_ARGUMENT_TYPES.include?(argument.type)
end
end
end
end
end
Expand Down
14 changes: 4 additions & 10 deletions spec/rubocop/cop/rails/dynamic_find_by_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,18 @@
end

context 'with too much arguments' do
it 'registers an offense and no corrects' do
expect_offense(<<~RUBY)
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
User.find_by_name_and_email(name, email, token)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email`.
RUBY

expect_no_corrections
end
end

context 'with too few arguments' do
it 'registers an offense and no corrects' do
expect_offense(<<~RUBY)
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
User.find_by_name_and_email(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email`.
RUBY

expect_no_corrections
end
end

Expand Down

0 comments on commit c9abc2a

Please sign in to comment.