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

Detect delegate to self #45

Closed
andyw8 opened this issue Mar 7, 2019 · 1 comment · Fixed by #974
Closed

Detect delegate to self #45

andyw8 opened this issue Mar 7, 2019 · 1 comment · Fixed by #974
Labels
enhancement New feature or request

Comments

@andyw8
Copy link
Contributor

andyw8 commented Mar 7, 2019

I discovered some code like this:

delegate :foo, to: self

foo is a class method. So calling foo in an instance method would be equivalent to self.class.foo.

I can't see how this confusing indirection would ever make sense. If a method needs to be available as both an instance and a class method, then there's probably a problem in the design.

Any thoughts? Should rubocop-rails detect this?

@andyw8
Copy link
Contributor Author

andyw8 commented Mar 7, 2019

To check how common this is, I grepped the set of apps from real-world-rails and only found a handful of occurrences:

discourse/app/models/category.rb
110:  delegate :post_template, to: 'self.class'

e-petitions/app/models/constituency.rb
17:  delegate :query, :example_postcodes, to: "self.class"

foreman/app/models/compute_resources/foreman/model/ec2.rb
7:    delegate :security_groups, :flavors, :zones, :to => :self, :prefix => 'available'

foreman/app/models/subnet.rb
104:  delegate :supports_ipam_mode?, :supported_ipam_modes, :show_mask?, to: 'self.class'

solidus/core/app/models/spree/promotion_code/batch_builder.rb
24:    delegate attr, to: self

@koic koic added the enhancement New feature or request label Apr 4, 2023
koic added a commit to koic/rubocop-rails that referenced this issue Apr 4, 2023
Fixes rubocop#45.

This PR makes `Rails/Delegate` aware of `self`.
koic added a commit to koic/rubocop-rails that referenced this issue Apr 4, 2023
Fixes rubocop#45.

This PR makes `Rails/Delegate` aware of `self`.
@koic koic closed this as completed in #974 Apr 5, 2023
koic added a commit that referenced this issue Apr 5, 2023
[Fix #45] Make `Rails/Delegate` aware of `self`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants