Skip to content

Commit

Permalink
Fix Italy state seed generation
Browse files Browse the repository at this point in the history
Previously, Italy was using the top-level `subregions` - `regions`
in Carmen to generate states, however for a mailing address this
is incorrect, and it instead needs to use `provinces`, which is
nested as the second level of `subregions` in Carmen. This fixes the
seed generation, allowing for more countries to make this switch
as necessary (there are a handful of other countries that need this)

Also, adds a `state:regenerate` rake task that updates states for
countries that were generated incorrectly, like Italy. This is so
people updgrading from v2.10 to v2.11 can easily update to the
correct state list.
  • Loading branch information
seand7565 committed Aug 4, 2020
1 parent 7c76993 commit aa3b311
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
18 changes: 14 additions & 4 deletions core/db/default/spree/states.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# frozen_string_literal: true

countries_that_use_nested_subregions = %w(IT)

def create_states(subregions, country)
subregions.each do |subregion|
Spree::State.where(abbr: subregion.code, country: country).first_or_create!(
name: subregion.name
)
end
end

ActiveRecord::Base.transaction do
Spree::Country.all.each do |country|
carmen_country = Carmen::Country.coded(country.iso)
next unless carmen_country.subregions?

carmen_country.subregions.each do |subregion|
Spree::State.where(abbr: subregion.code, country: country).first_or_create!(
name: subregion.name
)
if countries_that_use_nested_subregions.include? country.iso
create_states(carmen_country.subregions.flat_map(&:subregions), country)
else
create_states(carmen_country.subregions, country)
end
end
end
57 changes: 57 additions & 0 deletions core/lib/tasks/states.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

def get_carmen_states(iso)
carmen_country = Carmen::Country.coded(iso)
return false unless carmen_country&.subregions?

if %w(IT).include? iso
return carmen_country.subregions.flat_map(&:subregions)
else
return carmen_country.subregions
end
end

def user_agrees(destroy_states, create_states, country)
puts "\e[31mStates for #{country.name} are incorrect. If you proceed, the following changes will happen:\e[0m"
puts "\e[31mThese states will be destroyed:\e[0m #{destroy_states}"
puts "\e[31mThese states will be created:\e[0m #{create_states}"
puts "\e[31mDo you want to proceed? [y/n]\e[0m "
["y", "yes"].include? STDIN.gets.strip.downcase
end

def delete_states(country, carmen_names)
country.states.where.not(name: carmen_names).destroy_all
end

def create_states(country, carmen_states, state_names)
carmen_states.each do |state|
unless state_names.include? state.name
Spree::State.where(abbr: state.code, country: country).first_or_create!(
name: state.name
)
end
end
end

namespace :states do
desc "Regenerates states in cases where previous Solidus versions generated incorrect states"
task regenerate: :environment do
Spree::Country.all.each do |country|
if carmen_states = get_carmen_states(country.iso)
if (state_names = country.states.pluck(:name)) != (carmen_names = carmen_states.map(&:name).sort)
if user_agrees(
(state_names - carmen_names),
(carmen_names - state_names),
country
)
delete_states(country, carmen_names)
create_states(country, carmen_states, state_names)
puts "#{country} states have been regenerated"
else
puts "Skipping #{country}"
end
end
end
end
end
end

0 comments on commit aa3b311

Please sign in to comment.