Class: ActionView::Helpers::DateTimeSelector

Inherits:
Object
  • Object
show all
Includes:
FormTagHelper
Defined in:
lib/action_view/helpers/text_field_date_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_text(selected, value, options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 139

def build_text(selected, value, options = {})
  unless value.nil?
    options.reverse_merge!({:leading_zeros => true, :ampm => false})
    leading_zeros = options.delete(:leading_zeros)
    value = leading_zeros ? sprintf("%02d", value) : value
    text = options[:ampm] ? AMPM_TRANSLATION[value] : value
  else
    ""
  end
end

#build_text_field_from_types(order) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 130

def build_text_field_from_types(order)
  input = ''
  order.reverse.each do |type|
    separator = separator(type) unless type == order.first # don't add on last field
    input.insert(0, separator.to_s + send("text_field_#{type}").to_s) 
  end
  text_field_tag("", input.html_safe, @html_options).html_safe
end

#text_field_dateObject



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
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 31

def text_field_date
  order = date_order.dup

  @options[:discard_hour]     = true
  @options[:discard_minute]   = true
  @options[:discard_second]   = true

  @options[:discard_year]   ||= true unless order.include?(:year)
  @options[:discard_month]  ||= true unless order.include?(:month)
  @options[:discard_day]    ||= true if @options[:discard_month] || !order.include?(:day)

  # Save this so we can restore it.
  original_datetime_separator_separator = @options[:datetime_separator] 
  original_date_separator = @options[:date_separator] 

  @options[:datetime_separator] = ""
  @options[:date_separator] = " "

  # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
  # valid (otherwise it could be 31 and February wouldn't be a valid date)
  if @datetime && @options[:discard_day] && !@options[:discard_month]
    @datetime = @datetime.change(:day => 1)
  end

  [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }

  build_text_field_from_types(order).tap do
    # Restore.
    @options[:datetime_separator] = original_datetime_separator_separator
    @options[:date_separator] = original_date_separator
  end
end

#text_field_dayObject



112
113
114
115
116
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 112

def text_field_day
  unless @options[:use_hidden] || @options[:discard_day]
    build_text(:day, day, :leading_zeros => false)
  end
end

#text_field_hourObject



106
107
108
109
110
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 106

def text_field_hour
  unless @options[:use_hidden] || @options[:discard_hour]
    build_text(:hour, hour, :ampm => @options[:ampm])
  end
end

#text_field_minuteObject



100
101
102
103
104
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 100

def text_field_minute
  unless @options[:use_hidden] || @options[:discard_minute]
    build_text(:minute, min)
  end
end

#text_field_monthObject



118
119
120
121
122
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 118

def text_field_month
  unless @options[:use_hidden] || @options[:discard_month]
    build_text(:month, month.nil? ? "" : month_name(month), :leading_zeros => false)
  end
end

#text_field_secondObject



94
95
96
97
98
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 94

def text_field_second
  unless @options[:use_hidden] || @options[:discard_second]
    build_text(:second, sec)
  end
end

#text_field_timeObject



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
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 64

def text_field_time
  order = []

  @options[:discard_month]    = true
  @options[:discard_year]     = true
  @options[:discard_day]      = true
  @options[:discard_second] ||= true unless @options[:include_seconds]

  # Save this so we can restore it.
  original_datetime_separator = @options[:datetime_separator]
  original_time_separator = @options[:time_separator] 
  original_date_separator = @options[:date_separator] 

  @options[:datetime_separator] = ''
  @options[:time_separator] = ':'
  @options[:date_separator] = " "

  order += [:year, :month, :day] unless @options[:ignore_date]

  order += [:hour, :minute]
  order << :second if @options[:include_seconds]

  build_text_field_from_types(order).tap do
    # Restore.
    @options[:datetime_separator] = original_datetime_separator 
    @options[:date_separator] = original_date_separator
    @options[:time_separator] = original_time_separator
  end
end

#text_field_yearObject



124
125
126
127
128
# File 'lib/action_view/helpers/text_field_date_helper.rb', line 124

def text_field_year
  unless @options[:use_hidden] || @options[:discard_year]
    build_text(:year, year, :leading_zeros => false)
  end
end