Class: Spree::Market

Inherits:
Object
  • Object
show all
Includes:
SingleStoreResource
Defined in:
app/models/spree/market.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_for_store(store) ⇒ Spree::Market?

Returns the default market for a store, or falls back to the first by position

Parameters:

Returns:



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

Parameters:

Returns:



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.

Returns:

  • (Boolean)


120
121
122
# File 'app/models/spree/market.rb', line 120

def can_be_deleted?
  !default? && !last_in_store?
end

#country_isosArray<String>

Read companion for ‘country_isos=`. Returns the sorted list of ISO codes currently assigned to the market.

Returns:

  • (Array<String>)


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.

Parameters:

  • values (Array<String>)


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_countrySpree::Country?

Returns the first country by name from this market’s countries

Returns:



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.

Parameters:

  • value (Array<String>, String, nil)


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_listArray<String>

Returns supported locales as an array, always including default_locale

Returns:

  • (Array<String>)


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_zoneSpree::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.

Returns:



73
74
75
# File 'app/models/spree/market.rb', line 73

def tax_zone
  @tax_zone ||= Spree::Zone.match(default_country)
end