DigitalOpera::States - noiseunion/do-toolbox GitHub Wiki

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.

to_collection(mapping={})

This will return a collection of states for use in selects.

Params
  • 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.
Examples
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'],...]

abbreviations

Returns an alphabetical array of state abbreviations

Example
DigitalOpera::States.abbreviations

names

Returns an alphabetical array of state names

Example
DigitalOpera::States.names

find_name_by_abbreviation(abbr)

Finds a state name by abbreviation

Params
  • abbr - string or symbol
Example
DigitalOpera::States.find_name_by_abbreviation('KY')
# ==> 'Kentucky'

find_abbreviation_by_name(name)

Finds a state abbreviation by name

Params
  • name - string or symbol
Example
DigitalOpera::States.find_abbreviation_by_name('Minnesota')
# ==> 'MN'

to_hash

Returns a hash of states

Example
DigitalOpera::States.to_hash
# ==> {'AL':'Alabama','AK','Alaska',...}