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

Add String#downcase_first method #45827

Merged
merged 1 commit into from Sep 8, 2022

Conversation

markaschneider
Copy link
Contributor

Rails 5 added String#upcase_first, which converts the first letter of a
string to uppercase (returning a duplicate). This commit adds the
corollary method, converting the first letter to lowercase.

Summary

This adds String#downcasecase_first method via ActiveSupport::Inflector, which
is how String#upcase_first was added.

@markaschneider
Copy link
Contributor Author

I didn't see any mention of #upcase_first in the Inflections section of the Active Support Core Extentions page. I'm happy to add documentation of both #upcase_first and #downcase_first if desired.

@fatkodima
Copy link
Member

Can you share a real-world use case for this method?

@markaschneider
Copy link
Contributor Author

We wanted to prepend verbiage to a standard message in some cases but not others. Our standard message includes several interpolations, and we wanted to keep things DRY while not introducing a variable in haml and (for better or worse) not creating a helper method.

For example:

en.yml

en:
  exceptions:
    common: Please contact %{support_rep_name} at %{support_email} or %{support_number} if you need immediate assistance.
    403:
      message_prefix: "If you feel you have reached this page in error, "

500 page:

 = t('.message')

403 page:

 = t('message_prefix') + t('.message').downcase_first	

While there are other, arguably better, ways of implementing something like this, #downcase_first would have been convenient.

The comment examples for #upcase_first indicate that the method was introduced for convenience in adjusting verbiage, and in our case, we wanted the same convenience in the opposite direction.

@natematykiewicz
Copy link
Contributor

For what it's worth, it's generally not a good idea to concatenate strings for I18n. In lot of languages = t('message_prefix') + t('.message').downcase_first doesn't actually make grammatical sense. With I18n, it's best to have a single key for the entire message.

@rafaelfranca
Copy link
Member

I didn't see any mention of #upcase_first in the Inflections section of the Active Support Core Extentions page. I'm happy to add documentation of both #upcase_first and #downcase_first if desired.

Let's add that.

Rails 5 added String#upcase_first, which converts the first letter of a
string to uppercase (returning a duplicate).  This commit adds the
corollary method, converting the first letter to lowercase.
@markaschneider
Copy link
Contributor Author

I didn't see any mention of #upcase_first in the Inflections section of the Active Support Core Extentions page. I'm happy to add documentation of both #upcase_first and #downcase_first if desired.

Let's add that.

Added.

@rafaelfranca rafaelfranca merged commit fcac1c4 into rails:main Sep 8, 2022
@markaschneider markaschneider deleted the string_downcase_first branch October 3, 2022 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants