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

[Fix #912] Enhance Rails/Delegate by adding delegation detection for self.class, constants, class variables, global variables, and instance variables #1438

Merged

Conversation

ydakuka
Copy link
Contributor

@ydakuka ydakuka commented Feb 9, 2025

Fix #912


  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@ydakuka ydakuka force-pushed the 912-add-more-delegation-targets-to-rails-delegate branch 2 times, most recently from 2d0f730 to f2e3803 Compare February 10, 2025 09:24
Comment on lines 287 to 292
@inst_obj.foo
end
RUBY

expect_correction(<<~RUBY)
delegate :foo, to: :@inst_obj
Copy link
Member

Choose a reason for hiding this comment

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

That's my two cents. Can you tweak the example? e.g.,

Suggested change
@inst_obj.foo
end
RUBY
expect_correction(<<~RUBY)
delegate :foo, to: :@inst_obj
@instance_variable.foo
end
RUBY
expect_correction(<<~RUBY)
delegate :foo, to: :@instance_variable

Comment on lines 300 to 305
@@cls_obj.foo
end
RUBY

expect_correction(<<~RUBY)
delegate :foo, to: :@@cls_obj
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@@cls_obj.foo
end
RUBY
expect_correction(<<~RUBY)
delegate :foo, to: :@@cls_obj
@@class_variable.foo
end
RUBY
expect_correction(<<~RUBY)
delegate :foo, to: :@@class_variable

@koic
Copy link
Member

koic commented Feb 13, 2025

It's a corner case, but wouldn't it be possible to apply the same handling to global variables as well?

@ydakuka
Copy link
Contributor Author

ydakuka commented Feb 13, 2025

It's a corner case, but wouldn't it be possible to apply the same handling to global variables as well?

I'm not sure if Rails delegate supports it because I couldn't find an example of it in the official documentation.
https://api.rubyonrails.org/classes/Module.html#method-i-delegate
Ok, I'll check.

@koic
Copy link
Member

koic commented Feb 13, 2025

This is the behavior in Rails 8.0.1. For your reference.

$ bin/rails -v
Rails 8.0.1

$ cat /tmp/x.rb
class X
  def foo
    puts 'hi'
  end
end

$global_variable = X.new

class Y
  delegate :foo, to: :$global_variable
end

Y.new.foo

$ bin/rails r /tmp/x.rb
hi

@ydakuka ydakuka force-pushed the 912-add-more-delegation-targets-to-rails-delegate branch from f2e3803 to a228e9c Compare February 13, 2025 17:01
@ydakuka
Copy link
Contributor Author

ydakuka commented Feb 13, 2025

Thank you, I've fixed it.

@@ -0,0 +1 @@
* [#912](https://github.com/rubocop/rubocop-rails/issues/912): Enhance `Rails/Delegate` by adding delegation detection for `self.class`, constants, instance variables, and class variables. ([@ydakuka][])
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a mention of global variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

P.S. I got a failed test that is not related to this PR.

Copy link
Member

Choose a reason for hiding this comment

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

The issue was fixed in f9adec0. There's no problem with this PR. Thank you!

@ydakuka ydakuka force-pushed the 912-add-more-delegation-targets-to-rails-delegate branch 2 times, most recently from 46cf5a2 to 3d2f8bf Compare February 13, 2025 17:25
…tion for `self.class`, constants, instance variables, and class variables
@ydakuka ydakuka force-pushed the 912-add-more-delegation-targets-to-rails-delegate branch from 3d2f8bf to 23e2098 Compare February 13, 2025 17:26
@koic koic merged commit 4d0e655 into rubocop:master Feb 13, 2025
15 of 16 checks passed
@koic
Copy link
Member

koic commented Feb 13, 2025

Thanks!

@ydakuka ydakuka deleted the 912-add-more-delegation-targets-to-rails-delegate branch February 13, 2025 17:33
@ydakuka ydakuka changed the title [Fix #912] Enhance Rails/Delegate by adding delegation detection for self.class, constants, instance variables, and class variables [Fix #912] Enhance Rails/Delegate by adding delegation detection for self.class, constants, class variables, global variables, and instance variables Feb 13, 2025
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.

Add more delegation targets to Rails/Delegate
2 participants