Class: Spree::Country

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/country.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_iso(iso) ⇒ Object



19
20
21
# File 'app/models/spree/country.rb', line 19

def self.by_iso(iso)
  where(['LOWER(iso) = ?', iso.downcase]).or(where(['LOWER(iso3) = ?', iso.downcase])).take
end

.iso_to_emoji_flag(iso) ⇒ Object



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

def self.iso_to_emoji_flag(iso)
  iso.upcase.chars.map { |c| (c.ord + 127397).chr(Encoding::UTF_8) }.join
end

.to_tom_select_jsonObject



28
29
30
31
32
33
34
35
# File 'app/models/spree/country.rb', line 28

def self.to_tom_select_json
  pluck(:name, :id, :iso).map do |name, id, iso|
    {
      id: id,
      name: "#{iso_to_emoji_flag(iso)} #{name}"
    }
  end.as_json
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  name <=> other.name
end

#current_marketSpree::Market?

Lookups Market for Country for the current Store

Returns:



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

def current_market
  @current_market ||= Spree::Current.store&.market_for_country(self)
end

#default?(store = nil) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'app/models/spree/country.rb', line 23

def default?(store = nil)
  store ||= Spree::Store.default
  self == store.default_country
end

#default_currencyObject

Returns the default currency code for this country (e.g., ‘USD’, ‘EUR’) Uses the countries gem (ISO3166) for accurate currency data



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

def default_currency
  iso3166_country&.currency_code
end

#default_localeObject

Returns the default locale/language for this country (e.g., ‘en’, ‘de’) Uses the countries gem (ISO3166) for accurate language data



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

def default_locale
  iso3166_country&.languages&.first
end

#to_sObject



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

def to_s
  name
end