require 'erb' module DatePicker module FormTagHelper def date_picker_tag(name, value, options = {}) options[:type]||= :date options[:time_zone]||= false options[:data]||= {} options[:id]||= "date_picker_" + Digest::SHA1.hexdigest(name.to_s)[8..16] type = options[:type] if value.blank? case type when :date value = Date.today when :datetime value = DateTime.now when :time value = Time.now end end # Get Type format if not specified if options[:format].present? format = options[:format] else format = DatePicker.config.formats[type] end if format.blank? || !format.is_a?(String) case type when :date format_id = format.present? && format.is_a?(Symbol) ? format.to_s : 'default' format = I18n.t('date.formats.' + format_id, default: "%Y-%m-%d") when :datetime format_id = format.present? && format.is_a?(Symbol) ? format.to_s : 'default' format = I18n.t('time.formats.' + format_id, default: "%a, %d %b %Y %H:%M:%S") when :time format_id = format.present? && format.is_a?(Symbol) ? format.to_s : 'only_time' format = I18n.t('time.formats.' + format_id, default: "%H:%M:%S") end else format = format.to_s end if options[:style].present? style = options[:style] elsif DatePicker.config.style.present? style = DatePicker.config.style else style = :bootstrap end path = File.join(File.dirname(__FILE__), "styles", style.to_s) require path obj = Object::const_get('DatePicker::Styles::' + style.to_s.classify).new types = [:date] if obj.respond_to?(:types) types = obj.send(:types) end formatted_value = value.present? ? value.strftime(format) : nil input_options = options.except(:time_zone, :format, :input_tag, :type) if obj.respond_to?(:options) input_options = input_options.merge(obj.send(:options)) end if obj.mapping.present? mapping = obj.mapping end if obj.mapping.is_a? Symbol mapping = DatePicker::Mappings.send(obj.mapping) end input_options[:type] = :text input_options[:value] = formatted_value input_tag = options[:tag] || :input input_id = options[:id] input_html = content_tag(input_tag, nil, input_options) # Escape special chars if mapping[:_].present? format.gsub!(/(?