@@ -206,6 +206,7 @@ def time_ago_in_words(from_time, options = {})
206
206
# you are creating new record. While editing existing record, <tt>:end_year</tt> defaults to
207
207
# the current selected year plus 5.
208
208
# * <tt>:year_format</tt> - Set format of years for year select. Lambda should be passed.
209
+ # * <tt>:day_format</tt> - Set format of days for day select. Lambda should be passed.
209
210
# * <tt>:discard_day</tt> - Set to true if you don't want to show a day select. This includes the day
210
211
# as a hidden field instead of showing a select field. Also note that this implicitly sets the day to be the
211
212
# first of the given month in order to not create invalid dates like 31 February.
@@ -279,6 +280,9 @@ def time_ago_in_words(from_time, options = {})
279
280
# # Generates a date select with custom year format.
280
281
# date_select("article", "written_on", year_format: ->(year) { "Heisei #{year - 1988}" })
281
282
#
283
+ # # Generates a date select with custom day format.
284
+ # date_select("article", "written_on", day_format: ->(day) { day.ordinalize })
285
+ #
282
286
# The selects are prepared for multi-parameter assignment to an Active Record object.
283
287
#
284
288
# Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that
@@ -811,7 +815,7 @@ def select_day
811
815
if @options [ :use_hidden ] || @options [ :discard_day ]
812
816
build_hidden ( :day , day || 1 )
813
817
else
814
- build_options_and_select ( :day , day , start : 1 , end : 31 , leading_zeros : false , use_two_digit_numbers : @options [ :use_two_digit_numbers ] )
818
+ build_select ( :day , build_day_options ( day ) )
815
819
end
816
820
end
817
821
@@ -899,6 +903,27 @@ def translated_month_names
899
903
I18n . translate ( key , locale : @options [ :locale ] )
900
904
end
901
905
906
+ # Looks up day names by number.
907
+ #
908
+ # day_name(1) # => 1
909
+ #
910
+ # If the <tt>use_two_digit_numbers: true</tt> option is passed to DateTimeSelector:
911
+ #
912
+ # day_name(1) # => "01"
913
+ #
914
+ # If the <tt>day_format: ->(day) { day.ordinalize }</tt> option is passed to DateTimeSelector:
915
+ #
916
+ # day_name(1) # => "1st"
917
+ def day_name ( number )
918
+ if day_format_lambda = @options [ :day_format ]
919
+ day_format_lambda . call ( number )
920
+ elsif @options [ :use_two_digit_numbers ]
921
+ "%02d" % number
922
+ else
923
+ number
924
+ end
925
+ end
926
+
902
927
# Looks up month names by number (1-based):
903
928
#
904
929
# month_name(1) # => "January"
@@ -1011,6 +1036,35 @@ def build_options(selected, options = {})
1011
1036
( select_options . join ( "\n " ) + "\n " ) . html_safe
1012
1037
end
1013
1038
1039
+ # Build select option HTML for day.
1040
+ # build_day_options(2)
1041
+ # => "<option value="1">1</option>
1042
+ # <option value="2" selected="selected">2</option>
1043
+ # <option value="3">3</option>..."
1044
+ #
1045
+ # If <tt>day_format: ->(day) { day.ordinalize }</tt> option is passed to DateTimeSelector
1046
+ # build_day_options(2)
1047
+ # => "<option value="1">1st</option>
1048
+ # <option value="2" selected="selected">2nd</option>
1049
+ # <option value="3">3rd</option>..."
1050
+ #
1051
+ # If <tt>use_two_digit_numbers: true</tt> option is passed to DateTimeSelector
1052
+ # build_day_options(2)
1053
+ # => "<option value="1">01</option>
1054
+ # <option value="2" selected="selected">02</option>
1055
+ # <option value="3">03</option>..."
1056
+ def build_day_options ( selected )
1057
+ select_options = [ ]
1058
+ ( 1 ..31 ) . each do |value |
1059
+ tag_options = { value : value }
1060
+ tag_options [ :selected ] = "selected" if selected == value
1061
+ text = day_name ( value )
1062
+ select_options << content_tag ( "option" , text , tag_options )
1063
+ end
1064
+
1065
+ ( select_options . join ( "\n " ) + "\n " ) . html_safe
1066
+ end
1067
+
1014
1068
# Build select option HTML for year.
1015
1069
# If <tt>year_format</tt> option is not passed
1016
1070
# build_year_options(1998, start: 1998, end: 2000)
0 commit comments