Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Rails/RedundantReceiverInWithOptions cop #5185

Conversation

koic
Copy link
Member

@koic koic commented Dec 5, 2017

Feature

This cop checks for redundant receiver in with_options.
Receiver is implicit from Rails 4.2 or higher.

Bad case

class Account < ApplicationRecord
  with_options dependent: :destroy do |assoc|
    assoc.has_many :customers
    assoc.has_many :products
    assoc.has_many :invoices
    assoc.has_many :expenses
  end
end

Good case

class Account < ApplicationRecord
  with_options dependent: :destroy do
    has_many :customers
    has_many :products
    has_many :invoices
    has_many :expenses
  end
end

How to use

% cat app/models/account.rb
# frozen_string_literal: true

class Account < ApplicationRecord
  with_options dependent: :destroy do |assoc|
    assoc.has_many :customers
    assoc.has_many :products
    assoc.has_many :invoices
    assoc.has_many :expenses
  end
end
% rubocop /tmp/app/models/account.rb --only Rails/RedundantReceiverInWithOptions
Inspecting 1 file
C

Offenses:

/tmp/app/models/account.rb:5:5: C: Rails/RedundantReceiverInWithOptions: Redundant receiver in with_options.
    assoc.has_many :customers
    ^^^^^
/tmp/app/models/account.rb:6:5: C: Rails/RedundantReceiverInWithOptions: Redundant receiver in with_options.
    assoc.has_many :products
    ^^^^^
/tmp/app/models/account.rb:7:5: C: Rails/RedundantReceiverInWithOptions: Redundant receiver in with_options.
    assoc.has_many :invoices
    ^^^^^
/tmp/app/models/account.rb:8:5: C: Rails/RedundantReceiverInWithOptions: Redundant receiver in with_options.
    assoc.has_many :expenses
    ^^^^^

1 file inspected, 4 offenses detected

Other Information

API documentation
http://api.rubyonrails.org/?q=with_options


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

module RuboCop
module Cop
module Rails
# This cop checks for unneeded receiver in `with_options`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually in English the word redundant would be used instead of unneeded.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. Code comment, cop name, offense message, commit message and so on in this PR were all replaced with redundant.

@koic koic force-pushed the add_new_rails_unneeded_receiver_in_with_options_cop branch from 61bc085 to 0a88805 Compare December 5, 2017 13:15
@koic koic changed the title Add new Rails/UnneededReceiverInWithOptions cop Add new Rails/RedundantReceiverInWithOptions cop Dec 5, 2017
@koic koic force-pushed the add_new_rails_unneeded_receiver_in_with_options_cop branch from 0a88805 to b898c09 Compare December 5, 2017 13:17
## Feature

This cop checks for redundant receiver in `with_options`.
Receiver is implicit from Rails 4.2 or higher.

### Bad case

```ruby
class Account < ApplicationRecord
  with_options dependent: :destroy do |assoc|
    assoc.has_many :customers
    assoc.has_many :products
    assoc.has_many :invoices
    assoc.has_many :expenses
  end
end
```

### Good case

```ruby
class Account < ApplicationRecord
  with_options dependent: :destroy do
    has_many :customers
    has_many :products
    has_many :invoices
    has_many :expenses
  end
end
```

### How to use

```console
% cat app/models/account.rb
# frozen_string_literal: true

class Account < ApplicationRecord
  with_options dependent: :destroy do |assoc|
    assoc.has_many :customers
    assoc.has_many :products
    assoc.has_many :invoices
    assoc.has_many :expenses
  end
end
```

```console
% rubocop /tmp/app/models/account.rb --only Rails/RedundantReceiverInWithOptions
Inspecting 1 file
C

Offenses:

/tmp/app/models/account.rb:5:5: C: Rails/RedundantReceiverInWithOptions: Redundant receiver in with_options.
    assoc.has_many :customers
    ^^^^^
/tmp/app/models/account.rb:6:5: C: Rails/RedundantReceiverInWithOptions: Redundant receiver in with_options.
    assoc.has_many :products
    ^^^^^
/tmp/app/models/account.rb:7:5: C: Rails/RedundantReceiverInWithOptions: Redundant receiver in with_options.
    assoc.has_many :invoices
    ^^^^^
/tmp/app/models/account.rb:8:5: C: Rails/RedundantReceiverInWithOptions: Redundant receiver in with_options.
    assoc.has_many :expenses
    ^^^^^

1 file inspected, 4 offenses detected
```

## Other Information

API documentation
http://api.rubyonrails.org/?q=with_options
@koic koic force-pushed the add_new_rails_unneeded_receiver_in_with_options_cop branch from b898c09 to 0beb6d2 Compare December 5, 2017 13:42
@bbatsov bbatsov merged commit 2014eec into rubocop:master Dec 5, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 5, 2017

👍

@koic koic deleted the add_new_rails_unneeded_receiver_in_with_options_cop branch December 5, 2017 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants