Skip to content

Commit facb068

Browse files
authored
Merge pull request #1013 from fatkodima/fix-three_state_boolean-dynamic-table
Fix `Rails/ThreeStateBooleanColumn` for dynamic tables/columns
2 parents d3c268e + 8e9e5c3 commit facb068

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1010](https://github.com/rubocop/rubocop-rails/issues/1010): Fix `Rails/ThreeStateBooleanColumn` for dynamic tables/columns. ([@fatkodima][])

lib/rubocop/cop/rails/three_state_boolean_column.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ThreeStateBooleanColumn < Base
3535
PATTERN
3636

3737
def_node_search :change_column_null?, <<~PATTERN
38-
(send nil? :change_column_null {(sym %1) (str %1)} {(sym %2) (str %2)} false)
38+
(send nil? :change_column_null %1 %2 false)
3939
PATTERN
4040

4141
def on_send(node)
@@ -46,9 +46,7 @@ def on_send(node)
4646

4747
def_node = node.each_ancestor(:def, :defs).first
4848
table_node = table_node(node)
49-
if def_node && (table_node.nil? || change_column_null?(def_node, table_node.value, column_node.value))
50-
return
51-
end
49+
return if def_node && (table_node.nil? || change_column_null?(def_node, table_node, column_node))
5250

5351
add_offense(node)
5452
end

spec/rubocop/cop/rails/three_state_boolean_column_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def change
3434
RUBY
3535
end
3636

37+
it 'does not register an offense when using `#change_column_null` with dynamic table' do
38+
expect_no_offenses(<<~RUBY)
39+
def change
40+
add_column table, :active, :boolean
41+
change_column_null table, :active, false
42+
end
43+
RUBY
44+
end
45+
3746
it 'registers an offense when using `#change_column_null` for other table or column' do
3847
expect_offense(<<~RUBY)
3948
def change
@@ -82,6 +91,17 @@ def change
8291
RUBY
8392
end
8493

94+
it 'does not register an offense when using `#change_column_null` with dynamic table' do
95+
expect_no_offenses(<<~RUBY)
96+
def change
97+
create_table(table) do |t|
98+
t.column :active, :boolean
99+
end
100+
change_column_null table, :active, false
101+
end
102+
RUBY
103+
end
104+
85105
it 'registers an offense when using `#change_column_null` for other table or column' do
86106
expect_offense(<<~RUBY)
87107
def change
@@ -136,6 +156,17 @@ def change
136156
RUBY
137157
end
138158

159+
it 'does not register an offense when using `#change_column_null` with dynamic table' do
160+
expect_no_offenses(<<~RUBY)
161+
def change
162+
create_table(table) do |t|
163+
t.boolean :active
164+
end
165+
change_column_null table, :active, false
166+
end
167+
RUBY
168+
end
169+
139170
it 'registers an offense when using `#change_column_null` for other table or column' do
140171
expect_offense(<<~RUBY)
141172
def change

0 commit comments

Comments
 (0)