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 =

North american countries iso.

%w[US CA].freeze
EU_COUNTRIES_ISO =

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 =

Common countries.

NORTH_AMERICAN_COUNTRIES_ISO + EU_COUNTRIES_ISO
EU_VAT_NUMBERS =

Eu vat numbers.

{  CZ: 'CZ686065176',
FR: 'FR55920674512',
DE: 'DE359871926',
IT: 'IT00349259994',
NL: 'NL863775627B01',
PL: 'PL5263606694',
ES: 'ESN0248585B',
SE: 'SE502093411201' }.freeze

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/country.rb', line 87

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



102
103
104
105
106
# File 'app/models/country.rb', line 102

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



108
109
110
111
112
113
114
115
116
# File 'app/models/country.rb', line 108

def find_iso3166_country(str)
  return if str.blank?

  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



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

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

.iso3_to_iso_mapObject



131
132
133
# File 'app/models/country.rb', line 131

def iso3_to_iso_map
  iso_to_iso3_map.invert
end

.iso_for_string(str) ⇒ Object



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

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

.iso_to_iso3_mapObject



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

def iso_to_iso3_map
  ISO3166::Country.all.to_h { |obj| [obj.alpha2, obj.alpha3] }
end

Instance Method Details

#companyCompany

Returns:

See Also:



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

belongs_to :company, optional: true

#default_shipping_countryObject



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

def default_shipping_country
  company.address.country
end

#eu_country?Boolean

Returns:

  • (Boolean)


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

def eu_country?
  iso.in?(EU_COUNTRIES_ISO)
end

#eu_vat_numberObject



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

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)


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

def has_state_list?
  iso3166_country.subdivisions.present?
end

#iso3166_countryObject



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

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

#locales_for_countryObject



80
81
82
# File 'app/models/country.rb', line 80

def locales_for_country
  LocaleUtility.locales_for_country_iso3(iso3)
end

#north_america?Boolean

Returns:

  • (Boolean)


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

def north_america?
  iso.in?(NORTH_AMERICAN_COUNTRIES_ISO)
end

#statesActiveRecord::Relation<State>

Returns:

  • (ActiveRecord::Relation<State>)

See Also:



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

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

#states_for_selectObject



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

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

#tax_rateTaxRate

Returns:

See Also:



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

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

#to_sObject



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

def to_s
  printable_name
end