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

Rails/HasManyOrHasOneDependent false positive when has_one called on a class instance #687

Closed
estraph opened this issue Mar 31, 2022 · 2 comments · Fixed by #926
Closed
Labels
bug Something isn't working

Comments

@estraph
Copy link

estraph commented Mar 31, 2022

When defining a decorator of a model we sometimes call has_one or has_many on the base class we are decorating:

module Foo
  def self.prepended(base)
    base.has_one :bar, dependent: :destroy
  end
end

Bar.prepend(Foo)

The Rails/HasManyOrHasOneDependent cop still triggers, which seems like a false positive:

app/models/foo.rb:3:10: C: Rails/HasManyOrHasOneDependent: Specify a :dependent option.
      base.has_one :bar, dependent: :destroy
           ^^^^^^^

Expected behavior

The cop should detect that dependent: :destroy is already defined.

Actual behavior

The cop fails suggesting dependent: :destroy is not present.

Steps to reproduce the problem

Call has_one or has_many on a class instance rather than as part of a class definition.

module Foo
  def self.prepended(base)
    base.has_one :bar, dependent: :destroy
  end
end

Bar.prepend(Foo)

RuboCop version

% bundle exec rubocop -V
1.26.1 (using Parser 3.1.1.0, rubocop-ast 1.16.0, running on ruby 3.0.3 x86_64-darwin21)
  - rubocop-performance 1.13.3
  - rubocop-rails 2.14.2
jcsanti added a commit to jcsanti/rubocop-rails that referenced this issue Jun 25, 2022
Ensure the cop does not ignore `has_many`, `has_one` or even
`with_options` when called on an explicit receiver.

An example where this can happen is a decorator implemented using a
module that prepends itself to the target class:

```RUBY
  class Person < ApplicationRecord; end

  module PersonDecorator
    extend ActiveSupport::Concern

    def self.prepended(base)
      base.has_one :foo, dependent: nil
    end

    Person.prepend self
  end
```
jcsanti added a commit to jcsanti/rubocop-rails that referenced this issue Jun 25, 2022
Ensure the cop does not ignore `has_many`, `has_one` or even
`with_options` when called on an explicit receiver.

An example where this can happen is a decorator implemented using a
module that prepends itself to the target class:

```RUBY
  class Person < ApplicationRecord; end

  module PersonDecorator
    extend ActiveSupport::Concern

    def self.prepended(base)
      base.has_one :foo, dependent: nil
    end

    Person.prepend self
  end
```
jcsanti added a commit to jcsanti/rubocop-rails that referenced this issue Jun 26, 2022
Ensure the cop does not ignore `has_many`, `has_one` or even
`with_options` when called on an explicit receiver.

An example where this can happen is a decorator implemented using a
module that prepends itself to the target class:

```RUBY
class Person < ApplicationRecord; end

module PersonDecorator
  extend ActiveSupport::Concern

  def self.prepended(base)
    base.has_one :foo, dependent: nil
  end

  Person.prepend self
end
```
jcsanti added a commit to jcsanti/rubocop-rails that referenced this issue Jun 26, 2022
Ensure the cop does not ignore `has_many`, `has_one` or even
`with_options` when called on an explicit receiver.

An example where this can happen is a decorator implemented using a
module that prepends itself to the target class:

```RUBY
class Person < ApplicationRecord; end

module PersonDecorator
  extend ActiveSupport::Concern

  def self.prepended(base)
    base.has_one :foo, dependent: nil
  end

  Person.prepend self
end
```
@Darhazer Darhazer added the bug Something isn't working label Jun 28, 2022
@fatkodima
Copy link
Contributor

@jcsanti would you like to open a PR with your changes?

@estraph
Copy link
Author

estraph commented Jan 27, 2023

Thank you @fatkodima 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants