Skip to content

Conversation

@tiagomenegaz
Copy link
Collaborator

@tiagomenegaz tiagomenegaz commented Feb 4, 2025

What are you trying to accomplish?

This PR removes respond_to? from the following files:

  • app/controllers/concerns/view_component/preview_actions.rb
  • lib/view_component/base.rb
  • lib/view_component/collection.rb
  • lib/view_component/slotable.rb
  • lib/view_component/slotable_default.rb
  • lib/view_component/translatable.rb
  • test/sandbox/test/rendering_test.rb
  • test/sandbox/test/slotable_test.rb
  • view_component.gemspec

Most of the changes were done by refactoring respond_to? to class.method_defined which is an alternative to that method. Please feel free to discuss these changes.

What approach did you choose and why?

Some checks aren't necessary because we don't support discontinued Ruby versions and the code can be written without respond_to?.

Anything you want to highlight for special attention from reviewers?

I noticed that some respond_to? are being used to support ruby2_keywords. I'm assuming it exists because we want to support old ruby versions. However, based on the gemfile - ruby_version = (ENV["RUBY_VERSION"] || "~> 3.4").to_s - we don't support Ruby 2 anymore. I'll address these keywords in a different PR.

@tiagomenegaz tiagomenegaz self-assigned this Feb 4, 2025
@tiagomenegaz tiagomenegaz changed the base branch from main to v4 February 4, 2025 22:27
@tiagomenegaz tiagomenegaz changed the title [WIP] Remove respond to [WIP] Remove respond_to? check Feb 4, 2025
@tiagomenegaz tiagomenegaz removed the request for review from joelhawksley February 8, 2025 03:26
@tiagomenegaz tiagomenegaz marked this pull request as ready for review February 10, 2025 05:33
@tiagomenegaz tiagomenegaz changed the title [WIP] Remove respond_to? check Remove respond_to? check Feb 10, 2025
def __vc_request
@__vc_request ||= controller.request if controller.respond_to?(:request)
@__vc_request ||= controller.request
rescue NoMethodError
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe this change goes against this project's code style? 🤔

@reeganviljoen
Copy link
Collaborator

@tiagomenegaz Firstly, thank you for this work 😄
I wonder what the performance difference is. Are you able to run the benchmarks with and without this and post the results here?

@tiagomenegaz
Copy link
Collaborator Author

@tiagomenegaz Firstly, thank you for this work 😄 I wonder what the performance difference is. Are you able to run the benchmarks with and without this and post the results here?

No problem. Any recommendations for that? @reeganviljoen

@BlakeWilliams
Copy link
Contributor

I feel like there's context I'm missing. What's the reasoning behind this change?

@joelhawksley
Copy link
Member

@BlakeWilliams I looked at some of these with @tiagomenegaz. We were looking to see if we could get rid of these checks wholesale as many were implicitly for older versions of Rails. I'll review this!

@joelhawksley
Copy link
Member

Closing in favor of #2219, added you as co-author!

@tiagomenegaz
Copy link
Collaborator Author

Closing in favor of #2219, added you as co-author!

Thanks for letting me know @joelhawksley

@joelhawksley joelhawksley deleted the remove-respond-to branch March 11, 2025 14:28
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.

5 participants