Module: Glib::CountryList

Defined in:
app/models/glib/country_list.rb

Overview

ISO 3166 country data for country dropdowns.

Backed entirely by the countries gem, so there is no table to migrate or seed, and renames (Türkiye, Czechia) arrive with a bundle update. Store the alpha-2 code rather than the name, so a rename never invalidates existing data.

Class Method Summary collapse

Class Method Details

.codesObject



21
22
23
# File 'app/models/glib/country_list.rb', line 21

def codes
  @codes ||= options.pluck(:value).freeze
end

.name_for(code) ⇒ Object



25
26
27
28
29
30
# File 'app/models/glib/country_list.rb', line 25

def name_for(code)
  return if code.blank?

  country = ISO3166::Country[code]
  country && (country.common_name || country.iso_short_name)
end

.optionsObject

common_name is what people actually call the place ("South Korea", "United Kingdom") rather than the formal ISO name ("Korea (Republic of)").



14
15
16
17
18
19
# File 'app/models/glib/country_list.rb', line 14

def options
  @options ||= ISO3166::Country.all
    .map { |country| { value: country.alpha2, text: country.common_name || country.iso_short_name } }
    .sort_by { |option| option[:text] }
    .freeze
end