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 use_year_names
option to date_select tag
#32190
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
I also patched
|
I agree with @yskkin. But if you need to mix different names it feels like you would be better served with an helper in your application. |
Ah, Western calendar and Japanese calendar are different so we can't handle with |
Yeah, this is actually going to be tricky for Japanese year 😅 With Thai Buddhist calendar, we got a easy way out since at the end of the day it's just a numeric value:
For Japanese year, I feel like having to pass I dug around and actually found a wareki gem which helps conversion from Gregorian date. I have a feeling that there must be a market in need of, say, |
@yskkin @rafaelfranca @sikachu <option value="2008">2551</option>
<option value="2009">2552</option>
<option value="2010">2553</option> But now |
Actually, one good way that we can achieve this is maybe letting user pass in a proc? date_select('user_birthday', '', start_year: 1998, end_year: 2000, labels_for_year_options: ->{ |year| convert_year_to_wareki(year) }) |
use_year_name
option to date_select taguse_year_names
option to date_select tag
It seems much better for me. I try to revise my codes in this way. |
Adopting @sikachu 's suggestion, I changed my codes and now the option takes Lambda as the argument. If the change is approved, I will squash the commits and change my comment on the commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There're just a few issues with the code style. I've left comments in-line.
After reading though it, I feel like labels_for_year_options
actually a bit mouthful though. I think we can get away with label_for_year: -> {...}
for brevity.
Nothing else has stood out for me, but if someone who's native English speaker can double check the documentation that'd be superb. I think this is a nice addition! 👍
@@ -846,12 +846,13 @@ def select_year | |||
options[:step] = options[:start] < options[:end] ? 1 : -1 | |||
options[:leading_zeros] = false | |||
options[:max_years_allowed] = @options[:max_years_allowed] || 1000 | |||
options[:labels_for_year_options] = @options[:labels_for_year_options] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this line was tagged by code climate. I think we stopped aligning operators, so you can just leave 1 space before =
.
@@ -996,6 +997,40 @@ def build_options(selected, options = {}) | |||
(select_options.join("\n") + "\n").html_safe | |||
end | |||
|
|||
# Build select option HTML for year. | |||
# If <tt>use_year_names</tt> option is not passed, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind updating this row to reflect the new setting name?
end | ||
|
||
(select_options.join("\n") + "\n").html_safe | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code style: Do you mind removing this empty blank line?
|
||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code style: Do you mind removing this empty blank line too?
@sikachu |
I think there's room to more closely mirror the month handling:
However, I'm a bit unsure whether it's worthwhile for us to support this... it's absolutely true that there are a number of other calendar systems, but this only helps the (small?) subset of those that still use Gregorian day/month numbering, and [at least approximately] agree that the year starts on Jan 1. On balance I think it's probably okay -- we do already provide a lot of formatting options on this helper. |
@matthewd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for updating the patch!
@matthewd do you mind taking care of this forward? I think this is good to go.
I found needles |
CI failure has been addressed at 691addb. |
@@ -933,6 +933,21 @@ def month_name(number) | |||
end | |||
end | |||
|
|||
# Looks up year names by number. | |||
# | |||
# month_name(1998) # => 1998 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
year_name(1998)
# | ||
# If the <tt>:year_format</tt> option is passed: | ||
# | ||
# month_name(1998) # => "Heisei 10" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
year_name(1998)
…sible to customize year names. Lambda should be passed to use this option. Example: date_select('user_birthday', '', start_year: 1998, end_year: 2000, year_format: ->year { "Heisei #{year - 1988}" }) The HTML produced: <select id="user_birthday__1i" name="user_birthday[(1i)]"> <option value="1998">Heisei 10</option> <option value="1999">Heisei 11</option> <option value="2000">Heisei 12</option> </select> /* The rest is omitted */
@kamipo |
@kamipo do you have any further feedback? Is this ready to be merged in? |
Yeah, I think this is ready to be merged in. |
@kamipo would you mind merging this in? (I'm in issues team so I don't have permission to merge 🙇) |
Add `use_year_names` option to date_select tag
Follow up rails#32190 [ci skip]
Summary
Add
use_year_names
option to date_select tag.This option makes it possible to customize year names. Example:
The HTML produced:
P.S.(on March 8)
Changed option name to
labels_for_year_options
. The argument is also changed from Array to Lambda. Example:The same HTML as written above is produced.
P.S.(on March 9)
Changed option name again to
label_for_year
. Nothing has changed except it and code style since March 8.P.S.(on March 15)
Changed option name again to
year_format
.Other Information
In Japan, Wareki, the Japanese calender, is used as often as Western Calender. For example, 2000 A.D. is Heisei 12 in Wareki. It is even used in official documents. Therefore, there is a need for date select box with Wareki years. Some other countries like Israel and Thailand also have their own calenders, so I think the number of people who need this
use_year_names
option is not small.