Module: Glib::StateList

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

Overview

ISO 3166-2 subdivision (state/province/territory) data for state dropdowns.

The companion to Glib::CountryList: backed entirely by the countries gem, so there is no table to migrate or seed, and renames arrive with a bundle update. Store the subdivision code (e.g. "NSW") rather than the name, so a rename never invalidates existing data. The code is only unique within a country, so always store it alongside the country code.

Not every country has subdivisions in the ISO data (e.g. Vatican City); for those options_for returns [] and the UI should fall back to a plain text field or omit the state input entirely.

Class Method Summary collapse

Class Method Details

.codes_for(country_code) ⇒ Object



25
26
27
# File 'app/models/glib/state_list.rb', line 25

def codes_for(country_code)
  options_for(country_code).pluck(:value)
end

.name_for(country_code, state_code) ⇒ Object



29
30
31
32
33
34
35
# File 'app/models/glib/state_list.rb', line 29

def name_for(country_code, state_code)
  return if country_code.blank? || state_code.blank?

  country = ISO3166::Country[country_code]
  subdivision = country&.subdivisions&.dig(state_code.to_s.upcase)
  subdivision&.name
end

.options_for(country_code) ⇒ Object

Options for a single country's state dropdown, sorted by display name. country_code is an alpha-2 code, matching what Glib::CountryList stores.



19
20
21
22
23
# File 'app/models/glib/state_list.rb', line 19

def options_for(country_code)
  return [] if country_code.blank?

  (@options_by_country ||= {})[country_code.to_s.upcase] ||= build_options(country_code)
end