Skip to content

Commit 7af820e

Browse files
committed
[Fix #997] Support methods and variables in NotNullColumn
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.
1 parent d71ac2e commit 7af820e

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#997](https://github.com/rubocop/rubocop-rails/issues/997): Fix to Allow `NotNullColumn` to work with method calls and variables. ([@fidalgo][])

lib/rubocop/cop/rails/not_null_column.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def on_send(node)
4545

4646
def check_add_column(node)
4747
add_not_null_column?(node) do |type, pairs|
48-
return if type.value == :virtual || type.value == 'virtual'
48+
return if type.respond_to?(:value) && (type.value == :virtual || type.value == 'virtual')
4949

5050
check_pairs(pairs)
5151
end

spec/rubocop/cop/rails/not_null_column_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
end
2222
end
2323

24+
context 'with the type argument is a variable' do
25+
it 'does not register an offense' do
26+
expect_no_offenses(<<~RUBY)
27+
add_column(:users, :name, type, default: 'default')
28+
RUBY
29+
end
30+
end
31+
2432
context 'with null: false and default: nil' do
2533
it 'reports an offense' do
2634
expect_offense(<<~RUBY)

0 commit comments

Comments
 (0)