Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
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
42
43
44
45
46
47
48
#
# SELECT pulldown from a start month to an end month. If the options[:name] is passed a 
# parameter in the URL, the SELECT will default to the matching OPTION entry. Otherwise, 
# it will default to the one with the end date. Date string must be parsable by Time.parse().
# 
# options: {
#   :name, 
#   :start,
#   :end (optional; defaults to current month)
# }
#
# Usage:
# <%= select_month :name => 'period', :start => '2008/11' %>
#
def select_month(options)
  output = ""
  output.concat "<select name=\"#{options[:name]}\">\n"

  dstart = Time.parse(options[:start])
  dcurrent = dstart

  if options[:end].nil?
    dend = Time.now
  else
    dend = Time.parse(options[:end])
  end

  while dcurrent.strftime('%Y%m').to_i <= dend.strftime('%Y%m').to_i do
    period = dend.strftime('%Y_%m')
    unless params[options[:name]].nil?
      period = params[options[:name]]
    end

    selected = ""
    if dcurrent.strftime('%Y_%m') == period
      selected = "selected=\"selected\""
    end

    output.concat "<option value=\"#{dcurrent.strftime('%Y_%m')}\" #{selected}>"
    output.concat dcurrent.strftime('%B %Y')
    output.concat "</option>\n"
    dcurrent += 1.month
  end

  output.concat "</select>\n"

    return output
end