DigitalOpera::States
Grant Klinsing edited this page Jan 19, 2016
·
3 revisions
States is a helper to easily add states select.
NOTE: This file is not included by default. To include in a project add this require 'digital_opera/states'
in your application.rb file.
This will return a collection of states for use in selects.
-
mapping
- hash optional- mapping keys can only be:
:key
or:value
.:key
is value of option,:value
is the displayed text. - mapping values can only be:
:name
or:abbr
.:name
is the full state name,:abbr
is the state abbriviation.
- mapping keys can only be:
collection_select(:post, :author_id, DigitalOpera::States.to_collection, :id, :name_with_initial, prompt: true)
DigitalOpera::States.to_collection
# ==> [['Alabama', 'AL'],['Alaska', 'AK'],...]
DigitalOpera::States.to_collection({key: :name})
# ==> [['Alabama', 'Alaska'],['Alaska', 'Alaska'],...]
# Defaults for :key and :value are used if not supplied. So it is essentially like writing
DigitalOpera::States.to_collection({key: :name, value: :name})
# ==> [['Alaska', 'Alabama'],['Alaska', 'Alaska'],...]
NOTE: Since the array of values is sorted on the value, changing the value mapping will change the order of the returned states.
DigitalOpera::States.to_collection({key: :name, value: :abbr})
# ==> [['AK', 'Alaska'],['AL', 'Alabama'],...]
DigitalOpera::States.to_collection({key: :abbr, value: :abbr})
# ==> [['AK', 'AL'],['AL', 'AL'],...]
Returns an alphabetical array of state abbreviations
DigitalOpera::States.abbreviations
Returns an alphabetical array of state names
DigitalOpera::States.names
Finds a state name by abbreviation
-
abbr
- string or symbol
DigitalOpera::States.find_name_by_abbreviation('KY')
# ==> 'Kentucky'
Finds a state abbreviation by name
-
name
- string or symbol
DigitalOpera::States.find_abbreviation_by_name('Minnesota')
# ==> 'MN'
Returns a hash of states
DigitalOpera::States.to_hash
# ==> {'AL':'Alabama','AK','Alaska',...}