Module: AUStateSelect::FormTagHelper

Defined in:
lib/au_state_select/form_tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#state_select_tag(name, options = {}, html_options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/au_state_select/form_tag_helper.rb', line 3

def state_select_tag(name, options = {}, html_options = {})

  states = ""

  if options.present? and (options[:include_blank] or (options[:prompt]))
    if options[:include_blank].present?
      option = options[:include_blank] == true ? "" : options[:include_blank]
    elsif options[:prompt].present?
      option = options[:prompt] == true ? "Please Select" : options[:prompt]
    end
    states += "<option value>#{option}</option>\n"
  end

  select_options = if options.key?(:short_name) and options[:short_name]
    ['ACT', 'NSW', 'NT', 'QLD', 'SA', 'TAS', 'VIC', 'WA']
  else
    [['Australian Capital Territory', 'ACT'],['New South Wales', 'NSW'],['Northern Territory', 'NT'],['Queensland', 'QLD'],['South Australia', 'SA'],['Tasmania', 'TAS'],['Victoria', 'VIC'],['Western Australia', 'WA']]
  end

  if options.key?(:value) and options[:value]
    states = states + options_for_select(select_options, options[:value])
  else
    states = states + options_for_select(select_options)
  end

  html_options = html_options.stringify_keys

  attribute_id = options[:id] || name

  (:select, states.html_safe, { "name" => name, "id" => sanitize_to_id(attribute_id) }.update(html_options.stringify_keys))
end