Skip to content

Commit

Permalink
Merge pull request #874 from joshhepworth/idempotent_region_seed_data
Browse files Browse the repository at this point in the history
Update Region Seed Data for Idempotence
  • Loading branch information
jhawthorn committed Feb 18, 2016
2 parents df312f0 + 1091e74 commit 70c4ffc
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 70c4ffc

Please sign in to comment.