Module: AUStateSelect::InstanceTag

Defined in:
lib/au_state_select/instance_tag.rb

Instance Method Summary collapse

Instance Method Details

#state_select(priority_states, options, html_options) ⇒ Object

Adapted from Rails country_select. Just uses country codes instead of full names.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/au_state_select/instance_tag.rb', line 13

def state_select(priority_states, options, html_options)
  selected = object.send(@method_name) if object.respond_to?(@method_name)

  states = ""

  if options.present? and (options[:include_blank] or (options[:prompt] and !selected))
    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>#{option}</option>\n"
  end

  if priority_states
    states += options_for_select(priority_states, selected)
    states += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
  end
  states = states + options_for_select([['Australian Capital Territory', 'ACT'],['New South Wales', 'NSW'],['Northern Territory', 'NT'],['Queensland', 'QLD'],['South Australia', 'SA'],['Tasmania', 'TAS'],['Victoria', 'VIC'],['Western Australia', 'WA']], selected)

  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)

  (:select, states.html_safe, html_options)
end

#to_state_select_tag(priority_states, html_options = {}, options = {}) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/au_state_select/instance_tag.rb', line 4

def to_state_select_tag(priority_states, html_options = {}, options = {})
  # Rails 4 stores options sent when creating an InstanceTag.
  # Let's use them!
  options = @options if defined?(@options)

  state_select(priority_states, options, html_options)
end