Class: Spree::Market
- Inherits:
-
Object
- Object
- Spree::Market
- Includes:
- SingleStoreResource
- Defined in:
- app/models/spree/market.rb
Class Method Summary collapse
-
.default_for_store(store) ⇒ Spree::Market?
Returns the default market for a store, or falls back to the first by position.
-
.for_country(country, store:) ⇒ Spree::Market?
Find the market that contains the given country for a store.
Instance Method Summary collapse
-
#can_be_deleted? ⇒ Boolean
Returns true when the market is safe to delete.
-
#country_isos ⇒ Array<String>
Read companion for ‘country_isos=`.
-
#country_isos=(values) ⇒ Object
Accepts an Array of 2-letter ISO codes and resolves them to the matching ‘Spree::Country` records, replacing the market’s countries.
-
#default_country ⇒ Spree::Country?
Returns the first country by name from this market’s countries.
-
#supported_locales=(value) ⇒ Object
Accepts an Array of locale codes and persists them as a comma-separated string on the ‘supported_locales` column.
-
#supported_locales_list ⇒ Array<String>
Returns supported locales as an array, always including default_locale.
-
#tax_zone ⇒ Spree::Zone?
Returns the tax zone matching this market’s default country.
Class Method Details
.default_for_store(store) ⇒ Spree::Market?
Returns the default market for a store, or falls back to the first by position
56 57 58 59 60 |
# File 'app/models/spree/market.rb', line 56 def self.default_for_store(store) return nil unless store store.markets.default.first || store.markets.order(:position).first end |
.for_country(country, store:) ⇒ Spree::Market?
Find the market that contains the given country for a store
43 44 45 46 47 48 49 50 |
# File 'app/models/spree/market.rb', line 43 def self.for_country(country, store:) return nil unless country && store joins(:market_countries) .where(store_id: store.id) .where(spree_market_countries: { country_id: country.id }) .take end |
Instance Method Details
#can_be_deleted? ⇒ Boolean
Returns true when the market is safe to delete. A market cannot be deleted if it is the default market or the only market in the store, since Spree::Current.currency would have no fallback.
120 121 122 |
# File 'app/models/spree/market.rb', line 120 def can_be_deleted? !default? && !last_in_store? end |
#country_isos ⇒ Array<String>
Read companion for ‘country_isos=`. Returns the sorted list of ISO codes currently assigned to the market.
100 101 102 |
# File 'app/models/spree/market.rb', line 100 def country_isos countries.map(&:iso).compact.sort end |
#country_isos=(values) ⇒ Object
Accepts an Array of 2-letter ISO codes and resolves them to the matching ‘Spree::Country` records, replacing the market’s countries. Unknown codes are silently dropped — the ‘validates :countries, presence: true` covers the “every ISO was bogus” case.
110 111 112 113 |
# File 'app/models/spree/market.rb', line 110 def country_isos=(values) isos = Array(values).compact.map { |v| v.to_s.upcase }.reject(&:blank?) self.countries = isos.any? ? Spree::Country.where(iso: isos) : [] end |
#default_country ⇒ Spree::Country?
Returns the first country by name from this market’s countries
65 66 67 |
# File 'app/models/spree/market.rb', line 65 def default_country countries.order(:name).first end |
#supported_locales=(value) ⇒ Object
Accepts an Array of locale codes and persists them as a comma-separated string on the ‘supported_locales` column. Strings are still accepted verbatim so legacy callers (the Rails admin form, raw seed scripts) keep working.
90 91 92 93 94 |
# File 'app/models/spree/market.rb', line 90 def supported_locales=(value) @supported_locales_list = nil normalized = value.is_a?(Array) ? value.compact.uniq.reject(&:blank?).join(',') : value super(normalized) end |
#supported_locales_list ⇒ Array<String>
Returns supported locales as an array, always including default_locale
80 81 82 |
# File 'app/models/spree/market.rb', line 80 def supported_locales_list @supported_locales_list ||= (supported_locales.to_s.split(',').map(&:strip) << default_locale).compact.uniq.sort end |
#tax_zone ⇒ Spree::Zone?
Returns the tax zone matching this market’s default country. Used by Spree::Current to determine the browsing tax zone before a customer enters an address.
73 74 75 |
# File 'app/models/spree/market.rb', line 73 def tax_zone @tax_zone ||= Spree::Zone.match(default_country) end |