Class: BootstrapDatepicker::InstanceTag

Inherits:
ActionView::Helpers::InstanceTag
  • Object
show all
Defined in:
lib/app/helpers/form_helper.rb

Constant Summary collapse

FORMAT_REPLACEMENTES =
{ "yy" => "%Y", "mm" => "%m", "dd" => "%d", "d" => "%-d", "m" => "%-m", "y" => "%y", "M" => "%b"}

Instance Method Summary collapse

Instance Method Details

#available_datepicker_optionsObject



45
46
47
# File 'lib/app/helpers/form_helper.rb', line 45

def available_datepicker_options
  [:format, :week_start, :view_mode, :min_view_mode]
end

#available_html_attributesObject



49
50
51
# File 'lib/app/helpers/form_helper.rb', line 49

def available_html_attributes
  [:class, :value, :maxlength]
end

#baked_options(options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/app/helpers/form_helper.rb', line 53

def baked_options(options)
  tf_options = Hash.new
  
  options.each do |key, value|

    if available_datepicker_options.include? key
      new_key = ("data-" << key.to_s)
      tf_options[new_key] = value
    end

    if available_html_attributes.include? key
        tf_options[key.to_s] = value
    end

  end
  
  puts 'options' 
  puts options
  puts 'tf_options'
  puts tf_options

  return options, tf_options
end

#format_date(tb_formatted, format) ⇒ Object



77
78
79
80
# File 'lib/app/helpers/form_helper.rb', line 77

def format_date(tb_formatted, format)
  new_format = translate_format(format)
  Date.parse(tb_formatted).strftime(new_format)
end

#get_name_and_id(options = {}) ⇒ Object

Extending ActionView::Helpers::InstanceTag module to make Rails build the name and id Just returns the options before generate the HTML in order to use the same id and name (see to_input_field_tag mehtod)



40
41
42
43
# File 'lib/app/helpers/form_helper.rb', line 40

def get_name_and_id(options = {})
  add_default_name_and_id(options)
  options
end

#translate_format(format) ⇒ Object

Method that translates the datepicker date formats, defined in (docs.jquery.com/UI/Datepicker/formatDate) to the ruby standard format (www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime). This gem is not going to support all the options, just the most used.



86
87
88
# File 'lib/app/helpers/form_helper.rb', line 86

def translate_format(format)
  format.gsub!(/#{FORMAT_REPLACEMENTES.keys.join("|")}/) { |match| FORMAT_REPLACEMENTES[match] }
end