Class: Spree::Seeds::States
- Inherits:
-
Object
- Object
- Spree::Seeds::States
- Includes:
- Spree::ServiceModule::Base
- Defined in:
- app/services/spree/seeds/states.rb
Constant Summary collapse
- EXCLUDED_US_STATES =
['UM', 'AS', 'MP', 'VI', 'PR', 'GU'].freeze
- EXCLUDED_CN_STATES =
['HK', 'MO', 'TW'].freeze
Instance Method Summary collapse
Methods included from Spree::ServiceModule::Base
Instance Method Details
#call ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/spree/seeds/states.rb', line 11 def call Spree::Country.where(states_required: true).each do |country| # Match on ISO code, not name: Spree stores official ISO-3166 names # ("United States of America") while Carmen uses common ones # ("United States"), and `named` is an exact match — so a name lookup # silently skipped the US, leaving it with no states at all. carmen_country = Carmen::Country.coded(country.iso) || Carmen::Country.named(country.name) next unless carmen_country states = carmen_country.subregions.flat_map do |subregion| if carmen_country.alpha_2_code == 'US' # Produces 50 states, one postal district (Washington DC) # and 3 APO's as you would expect to see on any good U.S. states list. next [] if EXCLUDED_US_STATES.include?(subregion.code) state_level(country, subregion) elsif carmen_country.alpha_2_code == 'CA' || carmen_country.alpha_2_code == 'MX' # Force Canada and Mexico to use state-level data import from Carmen Gem # else we pull in a subset of provinces that are not common at checkout. state_level(country, subregion) elsif carmen_country.alpha_2_code == 'CN' # Removes 3 "States" from that list that are also listed as Countries, # Hong Kong, Taiwan and Macao next [] if EXCLUDED_CN_STATES.include?(subregion.code) state_level(country, subregion) elsif subregion.subregions? province_level(country, subregion) else state_level(country, subregion) end end # One upsert per country rather than one per subregion. upsert_states(states) end end |