Class: Spree::Country
- Inherits:
-
Object
- Object
- Spree::Country
- Defined in:
- app/models/spree/country.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#current_market ⇒ Spree::Market?
Lookups Market for Country for the current Store.
- #default?(store = nil) ⇒ Boolean
-
#default_currency ⇒ Object
Returns the default currency code for this country (e.g., 'USD', 'EUR') Uses the countries gem (ISO3166) for accurate currency data.
-
#default_locale ⇒ Object
Returns the default locale/language for this country (e.g., 'en', 'de') Uses the countries gem (ISO3166) for accurate language data.
-
#localized_name(locale: I18n.locale) ⇒ String
Display name localized to
localevia the countries gem (CLDR), falling back to the storedname(or the ISO when there is no name) for an unknown ISO or missing translation. -
#option_label(locale: I18n.locale) ⇒ String
Flag emoji + localized name, for select options.
- #to_s ⇒ Object
Class Method Details
.by_iso(iso) ⇒ Object
21 22 23 |
# File 'app/models/spree/country.rb', line 21 def self.by_iso(iso) where(['LOWER(iso) = ?', iso.downcase]).or(where(['LOWER(iso3) = ?', iso.downcase])).take end |
.iso_to_emoji_flag(iso) ⇒ Object
39 40 41 |
# File 'app/models/spree/country.rb', line 39 def self.iso_to_emoji_flag(iso) iso.upcase.chars.map { |c| (c.ord + 127397).chr(Encoding::UTF_8) }.join end |
.to_tom_select_json ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'app/models/spree/country.rb', line 30 def self.to_tom_select_json pluck(:name, :id, :iso).map do |name, id, iso| { id: id, name: new(iso: iso, name: name).option_label } end.as_json end |
Instance Method Details
#<=>(other) ⇒ Object
70 71 72 |
# File 'app/models/spree/country.rb', line 70 def <=>(other) name <=> other.name end |
#current_market ⇒ Spree::Market?
Lookups Market for Country for the current Store
45 46 47 |
# File 'app/models/spree/country.rb', line 45 def current_market @current_market ||= Spree::Current.store&.market_for_country(self) end |
#default?(store = nil) ⇒ Boolean
25 26 27 28 |
# File 'app/models/spree/country.rb', line 25 def default?(store = nil) store ||= Spree::Store.default self == store.default_country end |
#default_currency ⇒ Object
Returns the default currency code for this country (e.g., 'USD', 'EUR') Uses the countries gem (ISO3166) for accurate currency data
80 81 82 |
# File 'app/models/spree/country.rb', line 80 def default_currency iso3166_country&.currency_code end |
#default_locale ⇒ Object
Returns the default locale/language for this country (e.g., 'en', 'de') Uses the countries gem (ISO3166) for accurate language data
86 87 88 |
# File 'app/models/spree/country.rb', line 86 def default_locale iso3166_country&.languages&.first end |
#localized_name(locale: I18n.locale) ⇒ String
Display name localized to locale via the countries gem (CLDR), falling
back to the stored name (or the ISO when there is no name) for an
unknown ISO or missing translation.
54 55 56 57 58 59 60 61 |
# File 'app/models/spree/country.rb', line 54 def localized_name(locale: I18n.locale) fallback = name.presence || iso.to_s data = ISO3166::Country[iso.to_s.upcase] return fallback unless data base_locale = locale.to_s.downcase.tr('_', '-').split('-').first (data.translation(base_locale) || data.translation(:en)).presence || fallback end |
#option_label(locale: I18n.locale) ⇒ String
Flag emoji + localized name, for select options.
66 67 68 |
# File 'app/models/spree/country.rb', line 66 def option_label(locale: I18n.locale) "#{self.class.iso_to_emoji_flag(iso)} #{localized_name(locale: locale)}" end |
#to_s ⇒ Object
74 75 76 |
# File 'app/models/spree/country.rb', line 74 def to_s name end |