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

Use native String#end_with?, not the ActiveSupport wrapper #1905

Merged
merged 1 commit into from
Nov 8, 2023
Merged

Use native String#end_with?, not the ActiveSupport wrapper #1905

merged 1 commit into from
Nov 8, 2023

Conversation

halo
Copy link
Contributor

@halo halo commented Nov 8, 2023

What are you trying to accomplish?

I noticed a call to String#ends_with?, which is an ActiveSupport core String extension introduced in 2005.

To reduce dependencies, I suggest using the ruby native String#end_with? (notice the missing "s") method instead. It's been in Ruby at least since 2.7.8.

What approach did you choose and why?

I'm trying to create a gem that has some custom view components. Yet, I found no definite way to define the dependencies for a gem like mine. Some don't require rails at all, some require all of rails (even activejob), some depend only on actionview and actionsupport, the view_component gem itself only requires activesupport and actionview implicitly (I'm not sure why).

Having that in mind, I tried to not depend on the entirety of Rails, and a minimal test setup code like this was sufficient to create a test suite for my components:

# test_helper.rb

# Create a minimal Rails app
require 'view_component/engine'
require 'action_controller'

class MyApplication < Rails::Application; end
Rails.application.routes.draw { resource :home }
class ApplicationController < ActionController::Base; end

# Make the view_component gem boot
require 'view_component/base'
require 'action_controller/test_case'
ViewComponent::Base.config.view_component_path = File.expand_path('../app/components', __dir__)

However, my gem tests didn't pass because of this one call to ends_with? – yet, when I add the following line, all my tests naturally pass:

require 'active_support/core_ext/string/starts_ends_with'

Final thoughts

I know this is just a minimal change and maybe I'm foolish for not requiring all of Rails in my test suite. I'm also fully aware that in the future, I might not be able to use my approach any more, if more dependencies on Rails-internals are implicitly used inside of the view_component gem.

But at the very least, I thought, I'd be able to press the question of a canonical way to create gems that contain view components. If you tell me to load an entire rails dummy app in my test suite instead, then you can disregard this pull request :)

Thank you for your time. It's very exciting to see your gem develop from generation to generation.

@halo halo changed the title Use native String#end_with?, not the Rails wrapper Use native String#end_with?, not the ActiveSupport wrapper Nov 8, 2023
* Replace usage of `String#ends_with?` with `String#end_with?` to reduce the dependency on ActiveSupport core extensions.

*halo*

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(I don't think this PR warrants an entry in the CHANGELOG, but it looked like one of the bot checks demanded it)

@Spone
Copy link
Collaborator

Spone commented Nov 8, 2023

👍 If the two methods are totally equivalent, I see no reason to keep relying on ActiveSupport.

Copy link
Contributor

@BlakeWilliams BlakeWilliams left a comment

Choose a reason for hiding this comment

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

We make no guarantees around the dependency on ActiveSupport, but if there's an easy way to limit that dependency I see no reason not to approve it.

Thanks for the contribution! :shipit:

@Spone Spone merged commit 0820ce8 into ViewComponent:main Nov 8, 2023
23 checks passed
@Spone
Copy link
Collaborator

Spone commented Nov 8, 2023

Thanks for the very thorough PR description by the way, this is much appreciated and helps a lot when reviewing!

leeky added a commit to citizensadvice/rails-form-builder that referenced this pull request Dec 11, 2023
When attempting to test integration between the gem and a real Rails
app, I received a weird error coming from the
`raise_if_slot_ends_with_question_mark` method inside the
`view_component` gem. This didn't happen when I last tested the
integration, which is very odd indeed.

Relevant PR: ViewComponent/view_component#1905
leeky added a commit to citizensadvice/rails-form-builder that referenced this pull request Jan 8, 2024
When attempting to test integration between the gem and a real Rails
app, I received a weird error coming from the
`raise_if_slot_ends_with_question_mark` method inside the
`view_component` gem. This didn't happen when I last tested the
integration, which is very odd indeed.

Relevant PR: ViewComponent/view_component#1905
leeky added a commit to citizensadvice/rails-form-builder that referenced this pull request Jan 12, 2024
When attempting to test integration between the gem and a real Rails
app, I received a weird error coming from the
`raise_if_slot_ends_with_question_mark` method inside the
`view_component` gem. This didn't happen when I last tested the
integration, which is very odd indeed.

Relevant PR: ViewComponent/view_component#1905
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.

3 participants