Sha256: 95cd66e8754d707e3f0d080ae59066f8d8f3aabb037ebb183380ad4a037a7d8b

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

require 'carmen'

module Spree
  module Seeds
    class Countries
      prepend Spree::ServiceModule::Base

      EXCLUDED_COUNTRIES = ['AQ', 'AX', 'GS', 'UM', 'HM', 'IO', 'EH', 'BV', 'TF'].freeze

      def call
        ApplicationRecord.transaction do
          new_countries = Carmen::Country.all.map do |country|
            # Skip the creation of some territories, uninhabited islands and the Antarctic.
            next if EXCLUDED_COUNTRIES.include?(country.alpha_2_code)

            Hash[
              'name': country.name,
              'iso3': country.alpha_3_code,
              'iso': country.alpha_2_code,
              'iso_name': country.name.upcase,
              'numcode': country.numeric_code,
            ]
          end.compact.uniq
          Spree::Country.insert_all(new_countries)

          # Find countries that do not use postal codes (by iso) and set 'zipcode_required' to false for them.
          Spree::Country.where(iso: Spree::Address::NO_ZIPCODE_ISO_CODES).update_all(zipcode_required: false)

          # Find all countries that require a state (province) at checkout and set 'states_required' to true.
          Spree::Country.where(iso: Spree::Address::STATES_REQUIRED).update_all(states_required: true)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/services/spree/seeds/countries.rb
spree_core-4.10.0 app/services/spree/seeds/countries.rb
spree_core-4.9.0 app/services/spree/seeds/countries.rb
spree_core-4.8.3 app/services/spree/seeds/countries.rb
spree_core-4.8.2 app/services/spree/seeds/countries.rb
spree_core-4.7.3 app/services/spree/seeds/countries.rb
spree_core-4.7.2 app/services/spree/seeds/countries.rb
spree_core-4.7.1 app/services/spree/seeds/countries.rb
spree_core-4.7.0 app/services/spree/seeds/countries.rb