Created
November 4, 2011 18:41
-
-
Save dvgica/1340125 to your computer and use it in GitHub Desktop.
Initializer to add am/pm support to time_select helpers in Rails 3 (3.1 has an option, use it!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modified from Bruno Miranda's original found here: http://brunomiranda.com/past/2007/6/2/displaying_12_hour_style_time_select/ | |
# usage: <%= form.time_select :the_time, { :twelve_hour => true } %> | |
module ActionView | |
module Helpers | |
class DateTimeSelector | |
def select_hour_with_twelve_hour_time | |
datetime = @datetime | |
options = @options | |
return select_hour_without_twelve_hour_time unless options[:twelve_hour].eql? true | |
if options[:use_hidden] | |
build_hidden(:hour, hour) | |
else | |
val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : '' | |
hour_options = [] | |
0.upto(23) do |hr| | |
ampm = hr <= 11 ? ' AM' : ' PM' | |
ampm_hour = case hr | |
when 0, 12 then 12 | |
when 1..11 then hr | |
when 13..23 then hr - 12 | |
end | |
hour_options << ((val == hr) ? | |
%(<option value="#{hr}" selected="selected">#{ampm_hour}#{ampm}</option>\n) : | |
%(<option value="#{hr}">#{ampm_hour}#{ampm}</option>\n) | |
) | |
end | |
build_select(:hour, hour_options.join) | |
end | |
end | |
alias_method_chain :select_hour, :twelve_hour_time | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment