Skip to content

Commit

Permalink
[Fix rubocop#997] Support methods and variables in NotNullColumn
Browse files Browse the repository at this point in the history
At the NotNullColumn cop if a method or variable is passed as arguments for `add_column`
an error is triggerred:
```
NoMethodError:
  undefined method `value' for s(:send, nil, :string):RuboCop::AST::SendNode
```

In this commit we an extra check to ensure the type has  the
`value` method implemented, before invoking it.
  • Loading branch information
fidalgo committed May 4, 2023
1 parent d71ac2e commit f0550c6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#997](https://github.com/rubocop/rubocop/issues/997): Fix to Allow `NotNullColumn` to work with method calls and variables. ([@fidalgo][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/not_null_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def on_send(node)

def check_add_column(node)
add_not_null_column?(node) do |type, pairs|
return if type.value == :virtual || type.value == 'virtual'
return if type.respond_to?(:value) && (type.value == :virtual || type.value == 'virtual')

check_pairs(pairs)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/not_null_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
end
end

context 'with the type argument is a variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
add_column(:users, :name, type, default: 'default')
RUBY
end
end

context 'with null: false and default: nil' do
it 'reports an offense' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit f0550c6

Please sign in to comment.