Skip to content

Commit

Permalink
Merge pull request #438 from koic/fix_false_negative_for_rails_belong…
Browse files Browse the repository at this point in the history
…s_to

[Fix #435] Fix a false negative for `Rails/BelongsTo`
  • Loading branch information
koic authored Feb 15, 2021
2 parents e71791a + ea62fe5 commit 78b70fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#421](https://github.com/rubocop-hq/rubocop-rails/issues/421): Fix incorrect auto-correct for `Rails/LinkToBlank` when using `target: '_blank'` with hash brackets for the option. ([@koic][])
* [#436](https://github.com/rubocop-hq/rubocop-rails/issues/436): Fix a false positive for `Rails/ContentTag` when the first argument is a splat argument. ([@koic][])
* [#435](https://github.com/rubocop-hq/rubocop-rails/issues/435): Fix a false negative for `Rails/BelongsTo` when using `belongs_to` lambda block with `required` option. ([@koic][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class BelongsTo < Base
RESTRICT_ON_SEND = %i[belongs_to].freeze

def_node_matcher :match_belongs_to_with_options, <<~PATTERN
(send _ :belongs_to _
(send _ :belongs_to ...
(hash <$(pair (sym :required) ${true false}) ...>)
)
PATTERN
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rails/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
RUBY
end

it 'registers an offense and corrects when using `belongs_to` lambda block with `required: false`' do
expect_offense(<<~RUBY)
belongs_to :foo, -> { bar }, required: false
^^^^^^^^^^ You specified `required: false`, in Rails > 5.0 the required option is deprecated and you want to use `optional: true`.
RUBY

expect_correction(<<~RUBY)
belongs_to :foo, -> { bar }, optional: true
RUBY
end

it 'registers no offense when setting `optional: true`' do
expect_no_offenses('belongs_to :foo, optional: true')
end
Expand Down

0 comments on commit 78b70fa

Please sign in to comment.