Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/us_states_select.rb

Constant Summary collapse

US_STATES =
[["Alaska", "AK"], ["Alabama", "AL"], ["Arkansas", "AR"], ["Arizona", "AZ"], 
["California", "CA"], ["Colorado", "CO"], ["Connecticut", "CT"], ["District of Columbia", "DC"], 
["Delaware", "DE"], ["Florida", "FL"], ["Georgia", "GA"], ["Hawaii", "HI"], ["Iowa", "IA"], 
["Idaho", "ID"], ["Illinois", "IL"], ["Indiana", "IN"], ["Kansas", "KS"], ["Kentucky", "KY"], 
["Louisiana", "LA"], ["Massachusetts", "MA"], ["Maryland", "MD"], ["Maine", "ME"], ["Michigan", "MI"], 
["Minnesota", "MN"], ["Missouri", "MO"], ["Mississippi", "MS"], ["Montana", "MT"], ["North Carolina", "NC"], 
["North Dakota", "ND"], ["Nebraska", "NE"], ["New Hampshire", "NH"], ["New Jersey", "NJ"], 
["New Mexico", "NM"], ["Nevada", "NV"], ["New York", "NY"], ["Ohio", "OH"], ["Oklahoma", "OK"], 
["Oregon", "OR"], ["Pennsylvania", "PA"], ["Rhode Island", "RI"], ["South Carolina", "SC"], ["South Dakota", "SD"], 
["Tennessee", "TN"], ["Texas", "TX"], ["Utah", "UT"], ["Virginia", "VA"], ["Vermont", "VT"], 
["Washington", "WA"], ["Wisconsin", "WI"], ["West Virginia", "WV"], ["Wyoming", "WY"]]

Instance Method Summary collapse

Instance Method Details

#us_state_options_for_select(selected = nil, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/us_states_select.rb', line 4

def us_state_options_for_select(selected = nil, options = {})
  state_options      = ""
  priority_states    = lambda { |state| options[:priority].include?(state.last) }
  options[:show] = :full if options[:with_abbreviation]
  states_label = case options[:show]
    when :full_abb          then lambda { |state| [state.first, state.last] }
    when :full              then lambda { |state| [state.first, state.first] }
    when :abbreviations     then lambda { |state| [state.last, state.last] }
    when :abb_full_abb      then lambda { |state| ["#{state.last} - #{state.first}", state.last] }
    else                         lambda { |state| state }
  end
  states_label = options[:show] if options[:show].is_a?(Proc)

  if options[:priority]
    state_options += options_for_select(US_STATES.select(&priority_states).collect(&states_label), selected)
    state_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
    
    selected = nil if options[:priority].include?(selected)
  end

  state_options += options_for_select(US_STATES.collect(&states_label), selected)
  state_options
end

#us_state_select(object, method, us_state_options = {}, options = {}, html_options = {}) ⇒ Object



28
29
30
31
# File 'lib/us_states_select.rb', line 28

def us_state_select(object, method, us_state_options = {}, options = {}, html_options = {})
  options.merge!(us_state_options)
  InstanceTag.new(object, method, self, options.delete(:object)).to_us_state_select_tag(options, html_options)
end