class ActiveRecord::Base
alias_method :orig_extract_callstack_for_multiparameter_attributes, :extract_callstack_for_multiparameter_attributes
def extract_callstack_for_multiparameter_attributes(pairs)
attributes = orig_extract_callstack_for_multiparameter_attributes(pairs)
attributes.each do |name, values|
klass = (self.class.reflect_on_aggregation(name) || column_for_attribute(name)).klass
if klass == Time && values.length == 6
if values[5] == "AM" && values[3] == 12
values[3] = 0
elsif values[5] == "PM" && values[3] != 12
values[3] += 12
end
end
end
end
end
module ActionView::Helpers::DateHelper
def select_hour(datetime, options = {})
hour_options = []
1.upto(12) do |hour|
time = Time.local(0, 1, 1, ((datetime.kind_of?(Fixnum)) ? datetime : datetime.hour))
selected = (time.strftime("%I").to_i == hour) ? ' selected="selected"' : ''
hour_options << %(\n)
end
select_html(options[:field_name] || 'hour', hour_options, options[:prefix], options[:include_blank], options[:discard_type], options[:disabled])
end
def select_ampm(datetime, options = {})
ampm_options = []
["AM", "PM"].each do |value|
time = Time.local(0, 1, 1, ((datetime.kind_of?(Fixnum)) ? datetime : datetime.hour))
selected = (time.strftime("%p") == value) ? ' selected="selected"' : ''
ampm_options << %(\n)
end
select_html(options[:field_name] || "ampm", ampm_options, options[:prefix], options[:include_blank], options[:discard_type], options[:disabled])
end
end
class ActionView::Helpers::InstanceTag
alias_method :orig_to_datetime_select_tag, :to_datetime_select_tag
def to_datetime_select_tag(options = {})
defaults = { :discard_type => true }
options = defaults.merge(options)
options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}s)]") }
datetime = options[:include_blank] ? (value || nil) : (value || Time.now)
orig_to_datetime_select_tag(options) + ' ' + select_ampm(datetime, options_with_prefix.call(6)) unless options[:discard_hour]
end
end