|
17 | 17 | RUBY
|
18 | 18 | end
|
19 | 19 |
|
| 20 | + it 'registers an offense and corrects when using `ids` in `where` for constant' do |
| 21 | + expect_offense(<<~RUBY) |
| 22 | + Post.where(user_id: User.active.ids) |
| 23 | + ^^^ Use `select(:id)` instead of `ids` within `where` query method. |
| 24 | + RUBY |
| 25 | + |
| 26 | + expect_correction(<<~RUBY) |
| 27 | + Post.where(user_id: User.active.select(:id)) |
| 28 | + RUBY |
| 29 | + end |
| 30 | + |
20 | 31 | it 'registers an offense and corrects when using `pluck` in `where.not` for constant' do
|
21 | 32 | expect_offense(<<~RUBY)
|
22 | 33 | Post.where.not(user_id: User.active.pluck(:id))
|
|
28 | 39 | RUBY
|
29 | 40 | end
|
30 | 41 |
|
| 42 | + it 'registers an offense and corrects when using `ids` in `where.not` for constant' do |
| 43 | + expect_offense(<<~RUBY) |
| 44 | + Post.where.not(user_id: User.active.ids) |
| 45 | + ^^^ Use `select(:id)` instead of `ids` within `where` query method. |
| 46 | + RUBY |
| 47 | + |
| 48 | + expect_correction(<<~RUBY) |
| 49 | + Post.where.not(user_id: User.active.select(:id)) |
| 50 | + RUBY |
| 51 | + end |
| 52 | + |
31 | 53 | it 'registers an offense and corrects when using `pluck` in `rewhere` for constant' do
|
32 | 54 | expect_offense(<<~RUBY)
|
33 | 55 | Post.rewhere('user_id IN (?)', User.active.pluck(:id))
|
|
39 | 61 | RUBY
|
40 | 62 | end
|
41 | 63 |
|
| 64 | + it 'registers an offense and corrects when using `ids` in `rewhere` for constant' do |
| 65 | + expect_offense(<<~RUBY) |
| 66 | + Post.rewhere('user_id IN (?)', User.active.ids) |
| 67 | + ^^^ Use `select(:id)` instead of `ids` within `where` query method. |
| 68 | + RUBY |
| 69 | + |
| 70 | + expect_correction(<<~RUBY) |
| 71 | + Post.rewhere('user_id IN (?)', User.active.select(:id)) |
| 72 | + RUBY |
| 73 | + end |
| 74 | + |
42 | 75 | it 'does not register an offense when using `select` in `where`' do
|
43 | 76 | expect_no_offenses(<<~RUBY)
|
44 | 77 | Post.where(user_id: User.active.select(:id))
|
|
51 | 84 | RUBY
|
52 | 85 | end
|
53 | 86 |
|
| 87 | + it 'does not register an offense when using `ids` chained with other method calls in `where`' do |
| 88 | + expect_no_offenses(<<~RUBY) |
| 89 | + Post.where(user_id: User.ids.map(&:to_i)) |
| 90 | + RUBY |
| 91 | + end |
| 92 | + |
54 | 93 | it 'does not register an offense when using `select` in query methods other than `where`' do
|
55 | 94 | expect_no_offenses(<<~RUBY)
|
56 | 95 | Post.order(columns.pluck(:name))
|
57 | 96 | RUBY
|
58 | 97 | end
|
| 98 | + |
| 99 | + it 'does not register an offense when using `ids` in query methods other than `where`' do |
| 100 | + expect_no_offenses(<<~RUBY) |
| 101 | + Post.order(columns.ids) |
| 102 | + RUBY |
| 103 | + end |
59 | 104 | end
|
60 | 105 |
|
61 | 106 | context 'EnforcedStyle: conservative' do
|
|
69 | 114 | Post.where(user_id: users.active.pluck(:id))
|
70 | 115 | RUBY
|
71 | 116 | end
|
| 117 | + |
| 118 | + it 'does not register an offense when using `ids` in `where`' do |
| 119 | + expect_no_offenses(<<~RUBY) |
| 120 | + Post.where(user_id: users.active.ids) |
| 121 | + RUBY |
| 122 | + end |
72 | 123 | end
|
73 | 124 | end
|
74 | 125 |
|
|
88 | 139 | Post.where(user_id: users.active.select(:id))
|
89 | 140 | RUBY
|
90 | 141 | end
|
| 142 | + |
| 143 | + it 'registers and corrects an offense when using `ids` in `where`' do |
| 144 | + expect_offense(<<~RUBY) |
| 145 | + Post.where(user_id: users.active.ids) |
| 146 | + ^^^ Use `select(:id)` instead of `ids` within `where` query method. |
| 147 | + RUBY |
| 148 | + |
| 149 | + expect_correction(<<~RUBY) |
| 150 | + Post.where(user_id: users.active.select(:id)) |
| 151 | + RUBY |
| 152 | + end |
91 | 153 | end
|
92 | 154 | end
|
93 | 155 | end
|
0 commit comments