Class: Country

Inherits:
ApplicationRecord show all
Extended by:
Memery
Defined in:
app/models/country.rb

Overview

== Schema Information

Table name: countries
Database name: primary

id :integer not null, primary key
country_code :integer
currency_code :string(10)
currency_symbol :string(5)
iso :string(255) not null
iso3 :string(255) not null
name :string(255) not null
numcode :integer not null
printable_name :string(255) not null
require_state :boolean default(FALSE)
require_zip :boolean default(FALSE)
telephone_code :integer
company_id :integer

Indexes

index_countries_on_iso (iso) UNIQUE
index_countries_on_iso3 (iso3) UNIQUE

Constant Summary collapse

NORTH_AMERICAN_COUNTRIES_ISO =
%w[US CA]
EU_COUNTRIES_ISO =
%w[AT BE BG HR CY CZ DK EE FI FR DE GR HU IE IT LV LT LU MT NL PL RO SK SI ES SE GB PT].freeze
COMMON_COUNTRIES =
NORTH_AMERICAN_COUNTRIES_ISO + EU_COUNTRIES_ISO
EU_VAT_NUMBERS =
{  CZ: 'CZ686065176',
FR: 'FR55920674512',
DE: 'DE359871926',
IT: 'IT00349259994',
NL: 'NL863775627B01',
PL: 'PL5263606694',
ES: 'ESN0248585B',
SE: 'SE502093411201' }

Belongs to collapse

Has one collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.countries_for_select(locale: I18n.locale, alpha2: false, locale_country_first: false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/country.rb', line 82

def countries_for_select(locale: I18n.locale, alpha2: false, locale_country_first: false)
  all_countries = ISO3166::Country.all
  res = all_countries.map { |c| [c.translation(locale) || c.name, (alpha2 ? c.alpha2 : c.alpha3)] }
  if locale_country_first
    if locale == :'en-US'
      res = res.sort_by { |r| r[1].starts_with?('US') ? '' : I18n.transliterate(r[0]) }
    elsif %i[en-CA fr-CA].include?(locale)
      res = res.sort_by { |r| r[1].starts_with?('CA') ? '' : I18n.transliterate(r[0]) }
    end
  else
    res = res.sort_by { |r| I18n.transliterate(r[0]).upcase } # This sorts without accents
  end
  res
end

.country_name_from_locale(locale = I18n.locale) ⇒ Object



97
98
99
100
101
# File 'app/models/country.rb', line 97

def country_name_from_locale(locale = I18n.locale)
  lang, alpha2 = locale.to_s.split('-')
  c = ISO3166::Country[alpha2]
  c.translations[lang] || c.name
end

.find_iso3166_country(str) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'app/models/country.rb', line 103

def find_iso3166_country(str)
  return unless str.present?

  c = ISO3166::Country.find_country_by_any_name(str)
  c ||= ISO3166::Country.find_country_by_alpha3(str)
  c ||= ISO3166::Country.find_country_by_alpha2(str)
  c ||= ISO3166::Country.find_all_by(:translated_names, str)&.first&.last
  c
end

.iso3_for_string(str) ⇒ Object



117
118
119
# File 'app/models/country.rb', line 117

def iso3_for_string(str)
  find_iso3166_country(str)&.alpha3
end

.iso3_to_iso_mapObject



126
127
128
# File 'app/models/country.rb', line 126

def iso3_to_iso_map
  iso_to_iso3_map.invert
end

.iso_for_string(str) ⇒ Object



113
114
115
# File 'app/models/country.rb', line 113

def iso_for_string(str)
  find_iso3166_country(str)&.alpha2
end

.iso_to_iso3_mapObject



121
122
123
# File 'app/models/country.rb', line 121

def iso_to_iso3_map
  ISO3166::Country.all.each_with_object({}) { |obj, hsh| hsh[obj.alpha2] = obj.alpha3 }
end

Instance Method Details

#companyCompany

Returns:

See Also:



39
# File 'app/models/country.rb', line 39

belongs_to :company, optional: true

#default_shipping_countryObject



71
72
73
# File 'app/models/country.rb', line 71

def default_shipping_country
  company.address.country
end

#eu_country?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/country.rb', line 43

def eu_country?
  iso.in?(EU_COUNTRIES_ISO)
end

#eu_vat_numberObject



51
52
53
# File 'app/models/country.rb', line 51

def eu_vat_number
  EU_VAT_NUMBERS[iso.to_sym] || EU_VAT_NUMBERS[:NL] # Default will be dutch VAT
end

#has_state_list?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/country.rb', line 63

def has_state_list?
  iso3166_country.subdivisions.present?
end

#iso3166_countryObject



55
56
57
# File 'app/models/country.rb', line 55

def iso3166_country
  ISO3166::Country.find_country_by_alpha3(iso3)
end

#locales_for_countryObject



75
76
77
# File 'app/models/country.rb', line 75

def locales_for_country
  LocaleUtility.locales_for_country_iso3(iso3)
end

#north_america?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/country.rb', line 47

def north_america?
  iso.in?(NORTH_AMERICAN_COUNTRIES_ISO)
end

#statesActiveRecord::Relation<State>

Returns:

  • (ActiveRecord::Relation<State>)

See Also:



41
# File 'app/models/country.rb', line 41

has_many :states, foreign_key: :country_iso3, primary_key: :iso3

#states_for_selectObject



59
60
61
# File 'app/models/country.rb', line 59

def states_for_select
  iso3166_country.subdivisions.map { |k, v| [v.name || k, k] }
end

#tax_rateTaxRate

Returns:

See Also:



40
# File 'app/models/country.rb', line 40

has_one :tax_rate, -> { where(tax_type: 'vat') }, foreign_key: :country_iso, primary_key: :iso

#to_sObject



67
68
69
# File 'app/models/country.rb', line 67

def to_s
  printable_name
end