Skip to content

Commit

Permalink
Update Region Seed Data for Idempotence
Browse files Browse the repository at this point in the history
Ensure that region seed data will not create duplicate entries if it is run more than once.
  • Loading branch information
joshhepworth committed Feb 18, 2016
1 parent cd9b7b4 commit 1091e74
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
22 changes: 9 additions & 13 deletions core/db/default/spree/countries.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
require 'carmen'

countries = []
Carmen::Country.all.each do |country|
countries << {
name: country.name,
iso3: country.alpha_3_code,
iso: country.alpha_2_code,
iso_name: country.name.upcase,
numcode: country.numeric_code,
states_required: country.subregions?
}
end

ActiveRecord::Base.transaction do
Spree::Country.create!(countries)
Carmen::Country.all.each do |country|
Spree::Country.where(iso: country.alpha_2_code).first_or_create!(
name: country.name,
iso3: country.alpha_3_code,
iso_name: country.name.upcase,
numcode: country.numeric_code,
states_required: country.subregions?
)
end
end
13 changes: 5 additions & 8 deletions core/db/default/spree/states.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
ActiveRecord::Base.transaction do
Spree::Country.all.each do |country|
carmen_country = Carmen::Country.named(country.name)
@states ||= []
carmen_country = Carmen::Country.coded(country.iso)
next unless carmen_country.subregions?

carmen_country.subregions.each do |subregion|
@states << {
name: subregion.name,
abbr: subregion.code,
country: country
}
Spree::State.where(abbr: subregion.code, country: country).first_or_create!(
name: subregion.name
)
end
end
Spree::State.create!(@states)
end

0 comments on commit 1091e74

Please sign in to comment.