Class: CML::TimeSelect

Inherits:
Object
  • Object
show all
Defined in:
lib/cml/tags/hours.rb

Constant Summary collapse

HOURS =
[12, 1.upto(11).to_a]
MINUTES =
[0, 15, 30, 45]
SUFFIXES =
[:am, :pm]
BlankOption =
"<option value=\"\">Select one:</option>"
GoldBlankOption =
"<option value=\"\">(Any)</option>"
NotListedOption =
"<option{{selected_att}} value=\"not_listed\">(Not listed)</option>"

Class Method Summary collapse

Class Method Details

.build(name, gold = false, opts = {}) ⇒ Object

Builds a time <select> input that submits military time values.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cml/tags/hours.rb', line 13

def self.build( name, gold=false, opts={} )
  opts = {
    :selected => nil,
    :allow_unlisted => false
  }.merge( opts )
  
  time_options = SUFFIXES.map do |am_pm|
    HOURS.flatten.map do |h|
      h_mil = h
      h_mil = 0 if h_mil == 12 && am_pm == :am
      MINUTES.map do |m|
        time_label = "#{h}:#{self.zero_pad(m)}#{am_pm}"
        mil_time = self.to_military( h_mil, m, am_pm )
        selected_att = mil_time == opts[:selected] ? " selected=\"selected\"" : ""
        "<option#{selected_att} value=\"#{mil_time}\">#{time_label}</option>"
      end.join("")
    end.join("")
  end.join("")
  extras = gold ? GoldBlankOption : BlankOption

  # Add the not listed field and select it if needed
  if opts[:allow_unlisted]
    selected_att = "not_listed" == opts[:selected] ? " selected=\"selected\"" : ""
    not_listed_html = Liquid::Template.parse( NotListedOption ).render( "selected_att" => selected_att )
    extras += not_listed_html
  end
  
  "<select name=\"#{name}\" class=\"cml_hours_time\">#{extras}#{time_options}</select>"
end