geetarista (owner)

Fork Of

gist: 55584 by bentlegen Date & DateTime form helper...

Revisions

  • df9c63 Sat Jan 31 08:20:45 -0800 2009
gist: 55791 Download_button fork
public
Public Clone URL: git://gist.github.com/55791.git
Embed All Files: show embed
Text only #
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module Merb::Helpers::Form
  def date_field(*args)
    if bound?(*args)
      current_form_context.bound_date_field(*args)
    else
      current_form_context.unbound_date_field(*args)
    end
  end
  
  def datetime_field(*args)
    if bound?(*args)
      current_form_context.bound_datetime_field(*args)
    else
      current_form_context.unbound_datetime_field(*args)
    end
  end
end
 
module Merb::Helpers::Form::Builder
  class Base
    def bound_date_field(method, attrs = {})
      name = control_name(method)
      update_bound_controls(method, attrs, nil)
      unbound_date_field({:name => name}.merge(attrs))
    end
    
    def bound_datetime_field(method, attrs = {})
      name = control_name(method)
      update_bound_controls(method, attrs, nil)
      unbound_datetime_field({:name => name}.merge(attrs))
    end
    
    def unbound_datetime_field(attrs)
 
      if attrs[:name] =~ /\[(.*)\]/
        date = @obj.send($1)
      end
      date = Date.new(Time.now.year) if date.nil?
            
      hour_attrs = attrs.merge(
        :class => "date hour",
        :selected => date.hour.to_s,
        :name => attrs[:name] + '[hour]',
        :id => attrs[:id] + '_hour',
        :collection => (0..24).map{|i|[i, i.to_s]},
        :label => nil
      )
      
      minute_attrs = attrs.merge(
        :class => "date minutes",
        :selected => date.min.to_s,
        :name => attrs[:name] + '[minute]',
        :id => attrs[:id] + '_minute',
        :collection => %w[00 05 10 15 20 25 30 35 40 45 50 55].map{|i|[i.to_i, i]},
        :value_method => :first,
        :text_method => :last,
        :label => nil
      )
 
      unbound_date_field(attrs) + ' ' + unbound_select(hour_attrs) + ':' + unbound_select(minute_attrs)
    end
    
    def unbound_date_field(attrs)
 
      if attrs[:name] =~ /\[(.*)\]/
        date = @obj.send($1)
      end
      date = Date.new(Time.now.year) if date.nil?
 
      month_attrs = attrs.merge(
        :class => "date month",
        :selected => date.month.to_s,
        :name => attrs[:name] + '[month]',
        :id => attrs[:id] + '_month',
        :collection => (1..12).map{|i|[i, DateTime::MONTHNAMES[i]]}
      )
      
      day_attrs = attrs.merge(
        :class => "date day",
        :selected => date.day.to_s,
        :name => attrs[:name] + '[day]',
        :id => attrs[:id] + '_day',
        :collection => (1..31).map{|i|i.to_s},
        :label => nil
      )
      
      year_attrs = attrs.merge(
        :class => "date year",
        :selected => date.year.to_s,
        :name => attrs[:name] + '[year]',
        :id => attrs[:id] + '_year',
        :collection => (Time.now.year..Time.now.year + 2).map{|i|i.to_s},
        :label => nil
      )
 
      unbound_select(month_attrs) + unbound_select(day_attrs) + unbound_select(year_attrs)
    end
  end
end