Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails time_select plugins?

Does anybody know any Ruby on Rails time_select plugins that have only one select box? So the select box has entries like "9:00 AM", "9:15 AM", etc.?

I have seen a few plugins, but nothing like this.

Thanks!

like image 265
Tony Avatar asked Nov 29 '22 07:11

Tony


1 Answers

I use time_selects ALOT in my apps, and solve time select problems with two minor adjustments.

The first is minute_step:

f.time_select :start_of_shift, :minute_step => 15

This will cut that heinous minute select box into a very managable size (choose your own!)

Also, I found a ruby module that I place i my initializer folder of all my time based apps:

module ActionView
module Helpers
class DateTimeSelector
def select_hour_with_twelve_hour_time 
  datetime = @datetime
  options = @options
  return select_hour_without_twelve_hour_time(datetime, options) 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 = hr == 12 ? 12 : (hr / 12 == 1 ? hr % 12 : hr)              
      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)         
  end       
  end       
  alias_method_chain :select_hour, :twelve_hour_time     
  end   
end
end

I wish I could remember where I found it so I could credit the source, but in a nutshell, it allows me to specify 12-hour time breaking down my time_select fields to two easy selects that are very easy to manage.

f.time_select :start_of_shift, :twelve_hour => true, :minute_step => 15

EDIT: Found the source of the ruby file! Find it here: http://brunomiranda.com/past/2007/6/2/displaying_12_hour_style_time_select/

like image 79
BushyMark Avatar answered Dec 05 '22 22:12

BushyMark