Module: Spree::Stores::Markets

Extended by:
ActiveSupport::Concern
Included in:
Spree::Store
Defined in:
app/models/concerns/spree/stores/markets.rb

Instance Method Summary collapse

Instance Method Details

#countries_available_for_checkoutArray<Spree::Country>

Returns the countries available for checkout, derived from markets

Returns:



75
76
77
78
79
80
81
82
83
# File 'app/models/concerns/spree/stores/markets.rb', line 75

def countries_available_for_checkout
  @countries_available_for_checkout = begin
    if has_markets?
      markets.flat_map(&:countries).uniq.sort_by(&:name)
    else
      Spree::Country.all.to_a
    end
  end
end

#countries_from_marketsActiveRecord::Relation<Spree::Country>

Returns countries from all markets as an ActiveRecord relation

Returns:



67
68
69
70
71
# File 'app/models/concerns/spree/stores/markets.rb', line 67

def countries_from_markets
  Spree::Country
            .where(id: Spree::Country.joins(market_countries: :market).where(Spree::Market.table_name => { store_id: id, deleted_at: nil }).select(:id))
            .order(:name)
end

#default_countrySpree::Country?

Returns the default country, derived from the default market

Returns:



18
19
20
21
22
23
24
# File 'app/models/concerns/spree/stores/markets.rb', line 18

def default_country
  if has_markets?
    default_market&.default_country
  else
    super
  end
end

#default_country_idInteger?

Returns the default country ID, derived from the default market

Returns:

  • (Integer, nil)


28
29
30
31
32
33
34
# File 'app/models/concerns/spree/stores/markets.rb', line 28

def default_country_id
  if has_markets?
    default_country&.id
  else
    read_attribute(:default_country_id)
  end
end

#default_currencyString?

Returns the default currency, delegating to the default market when markets exist Falls back to the store’s own default_currency column

Returns:

  • (String, nil)


50
51
52
53
54
55
56
# File 'app/models/concerns/spree/stores/markets.rb', line 50

def default_currency
  if has_markets?
    default_market&.currency || read_attribute(:default_currency)
  else
    read_attribute(:default_currency)
  end
end

#default_localeString?

Returns the default locale, delegating to the default market when markets exist Falls back to the store’s own default_locale column

Returns:

  • (String, nil)


39
40
41
42
43
44
45
# File 'app/models/concerns/spree/stores/markets.rb', line 39

def default_locale
  if has_markets?
    default_market&.default_locale || read_attribute(:default_locale)
  else
    read_attribute(:default_locale)
  end
end

#default_marketSpree::Market?

Returns the default market for this store

Returns:



12
13
14
# File 'app/models/concerns/spree/stores/markets.rb', line 12

def default_market
  @default_market ||= Spree::Market.default_for_store(self)
end

#has_markets?Boolean

Returns true if the store has markets, false otherwise

Returns:

  • (Boolean)


109
110
111
# File 'app/models/concerns/spree/stores/markets.rb', line 109

def has_markets?
  @has_markets ||= persisted? && (markets.loaded? ? markets.any? : markets.exists?)
end

#market_for_country(country) ⇒ Spree::Market?

Returns the market that contains the given country

Parameters:

Returns:



61
62
63
# File 'app/models/concerns/spree/stores/markets.rb', line 61

def market_for_country(country)
  Spree::Market.for_country(country, store: self)
end

#supported_currencies_listArray<Money::Currency>

Returns supported currencies derived from markets, falling back to store attributes

Returns:

  • (Array<Money::Currency>)


87
88
89
90
91
92
93
94
95
# File 'app/models/concerns/spree/stores/markets.rb', line 87

def supported_currencies_list
  @supported_currencies_list ||= if has_markets?
                                   markets.pluck(:currency).uniq.map do |code|
                                     ::Money::Currency.find(code)
                                   end.compact.sort_by { |c| c.iso_code == default_currency ? 0 : 1 }
                                 else
                                   legacy_supported_currencies_list
                                 end
end

#supported_locales_listArray<String>

Returns supported locales derived from markets, falling back to store attributes

Returns:

  • (Array<String>)


99
100
101
102
103
104
105
# File 'app/models/concerns/spree/stores/markets.rb', line 99

def supported_locales_list
  @supported_locales_list ||= if has_markets?
                                (markets.flat_map(&:supported_locales_list) << default_locale).compact.uniq.sort
                              else
                                legacy_supported_locales_list
                              end
end