Module: JqueryDatetimepick::FormHelper

Includes:
ActionView::Helpers::JavaScriptHelper
Included in:
DatetimepickHelper
Defined in:
lib/jquery_datetimepick/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#datetimepicker(object_name, method, options = {}) ⇒ Object

Mehtod that generates datetimepicker input field inside a form



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jquery_datetimepick/form_helper.rb', line 9

def datetimepicker(object_name, method, options = {})
  input_tag =  JqueryDatetimepick::InstanceTag.new(object_name, method, self, options)
  dp_options, tf_options =  input_tag.split_options(options)
  tf_options[:value] = input_tag.format_datetime(tf_options[:value], String.new(dp_options[:dateFormat])) if  tf_options[:value] && !tf_options[:value].empty? && dp_options.has_key?(:dateFormat)
  [ :minDateTime, :maxDateTime ].each do |var|
    if(o = dp_options[var])
      if o.respond_to?(:strftime)
        dp_options[var] = "new Date(#{o.to_i*1000})"
      else
        dp_options[var] = "function() { return #{o}; }"
      end
    end
  end
  json = dp_options.to_json
  json.gsub! /"(new Date\([^\)]*\))"/, '\1'
  json.gsub! /"(function\(\) \{[^\}]*\})"/, '\1'
  html= if defined?(ActionView::Helpers::InstanceTag) && ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0
    input_tag.to_input_field_tag("text", tf_options)
  else
    ActionView::Helpers::Tags::TextField.new(object_name, method, tf_options).render
  end
  html += javascript_tag("jQuery(document).ready(function(){jQuery('##{input_tag.get_name_and_id["id"]}').datetimepicker($.extend(#{json}, $.timepicker.regional[#{I18n.locale}]))});")
  html.html_safe
end