Skip to content

Commit

Permalink
[Fix rubocop#707] Fix an error when a variable is passed to has_many …
Browse files Browse the repository at this point in the history
…or has_one with double splat
  • Loading branch information
nobuyo committed May 31, 2022
1 parent 3a93706 commit c9c6cef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_when_a_variable_is_passed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#707](https://github.com/rubocop/rubocop-rails/issues/707): Fix an error when a variable is passed to has_many or has_one with double splat. ([@nobuyo][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ def contain_valid_options_in_with_options_block?(node)
def valid_options?(options)
return false if options.nil?

options = options.first.children.first.pairs if options.first.kwsplat_type?
if options.first.kwsplat_type? && options.first.children.first.hash_type?
options = options.first.children.first.pairs
end

return true unless options
return true if options.any? do |o|
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class Person < ApplicationRecord
RUBY
end

it 'registers an offense when a variable passed with double splat' do
expect_offense(<<~RUBY)
class Person < ApplicationRecord
has_one :foo, **bar
^^^^^^^ Specify a `:dependent` option.
end
RUBY
end

it 'does not register an offense when specifying default `dependent: nil` strategy' do
expect_no_offenses(<<~RUBY)
class Person < ApplicationRecord
Expand Down

0 comments on commit c9c6cef

Please sign in to comment.