Sha256: 0428c10111b6e1d27c2d4f8d247afc8e7cff970036bd90717aa14717e45fcc1a

Contents?: true

Size: 1.5 KB

Versions: 48

Compression:

Stored size: 1.5 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 = ApplicationRecord.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

48 entries across 48 versions & 2 rubygems

Version Path
solidus_core-2.10.5 db/default/spree/countries.rb
solidus_core-2.10.3 db/default/spree/countries.rb
solidus_core-2.10.2 db/default/spree/countries.rb
solidus_core-2.9.6 db/default/spree/countries.rb
solidus_core-2.8.6 db/default/spree/countries.rb
solidus_core-2.10.1 db/default/spree/countries.rb
solidus_core-2.9.5 db/default/spree/countries.rb
solidus_core-2.10.0 db/default/spree/countries.rb
solidus_core-2.9.4 db/default/spree/countries.rb
solidus_core-2.6.6 db/default/spree/countries.rb
solidus_core-2.7.4 db/default/spree/countries.rb
solidus_core-2.8.5 db/default/spree/countries.rb
solidus_core-2.9.3 db/default/spree/countries.rb
solidus_core-2.9.2 db/default/spree/countries.rb
solidus_core-2.10.0.beta1 db/default/spree/countries.rb
solidus_core-2.7.3 db/default/spree/countries.rb
solidus_core-2.6.5 db/default/spree/countries.rb
solidus_core-2.9.1 db/default/spree/countries.rb
solidus_core-2.9.0 db/default/spree/countries.rb
solidus_core-2.9.0.rc.1 db/default/spree/countries.rb