Class: Stay::Seeds::Countries

Inherits:
Object
  • Object
show all
Defined in:
app/services/stay/seeds/countries.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.callObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/stay/seeds/countries.rb', line 8

def self.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
    Stay::Country.insert_all(new_countries)

    # Find countries that do not use postal codes (by iso) and set 'zipcode_required' to false for them.
    Stay::Country.where(iso: Stay::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.
    Stay::Country.where(iso: Stay::Address::STATES_REQUIRED).update_all(states_required: true)
  end
end