-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Region Seed Data for Idempotence
Ensure that region seed data will not create duplicate entries if it is run more than once.
- Loading branch information
1 parent
cd9b7b4
commit 1091e74
Showing
2 changed files
with
14 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |