Sha256: 5aa9c537de6b14ba3d106b3466d0993e42cdae7b3e5fd0f6ae690ccaa64abfcb

Contents?: true

Size: 1.49 KB

Versions: 85

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'carmen'

# Insert Countries into the spree_countries table, checking to ensure that no
# duplicates are created, using as few SQL statements as possible (2)

connection = Spree::Base.connection

country_mapper = ->(country) do
  name            = connection.quote country.name
  iso3            = connection.quote country.alpha_3_code
  iso             = connection.quote country.alpha_2_code
  iso_name        = connection.quote country.name.upcase
  numcode         = connection.quote country.numeric_code
  states_required = connection.quote country.subregions?

  [name, iso3, iso, iso_name, numcode, states_required].join(", ")
end

country_values = -> do
  carmen_countries = Carmen::Country.all

  # find entires already in the database (so that we may ignore them)
  existing_country_isos =
    Spree::Country.where(iso: carmen_countries.map(&:alpha_2_code)).pluck(:iso)

  # create VALUES statements for each country _not_ already in the database
  carmen_countries
    .reject { |c| existing_country_isos.include?(c.alpha_2_code) }
    .map(&country_mapper)
    .join("), (")
end

country_columns = %w(name iso3 iso iso_name numcode states_required).join(', ')
country_vals = country_values.call

if country_vals.present?
  # execute raw SQL (insted of ActiveRecord.create) to use a single
  # INSERT statement, and to avoid any validations or callbacks
  connection.execute <<-SQL
    INSERT INTO spree_countries (#{country_columns})
    VALUES (#{country_vals});
  SQL
end

Version data entries

85 entries across 85 versions & 1 rubygems

Version Path
solidus_core-4.4.2 db/default/spree/countries.rb
solidus_core-4.4.1 db/default/spree/countries.rb
solidus_core-4.4.0 db/default/spree/countries.rb
solidus_core-4.3.4 db/default/spree/countries.rb
solidus_core-4.2.4 db/default/spree/countries.rb
solidus_core-4.1.5 db/default/spree/countries.rb
solidus_core-4.3.3 db/default/spree/countries.rb
solidus_core-4.3.2 db/default/spree/countries.rb
solidus_core-4.1.4 db/default/spree/countries.rb
solidus_core-4.3.1 db/default/spree/countries.rb
solidus_core-4.3.0 db/default/spree/countries.rb
solidus_core-4.2.3 db/default/spree/countries.rb
solidus_core-4.1.3 db/default/spree/countries.rb
solidus_core-4.0.4 db/default/spree/countries.rb
solidus_core-3.4.6 db/default/spree/countries.rb
solidus_core-4.0.3 db/default/spree/countries.rb
solidus_core-4.1.2 db/default/spree/countries.rb
solidus_core-4.2.2 db/default/spree/countries.rb
solidus_core-3.4.5 db/default/spree/countries.rb
solidus_core-4.2.1 db/default/spree/countries.rb