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:



72
73
74
75
76
77
78
79
80
# File 'app/models/concerns/spree/stores/markets.rb', line 72

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:



64
65
66
67
68
# File 'app/models/concerns/spree/stores/markets.rb', line 64

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:



15
16
17
18
19
20
21
# File 'app/models/concerns/spree/stores/markets.rb', line 15

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)


25
26
27
28
29
30
31
# File 'app/models/concerns/spree/stores/markets.rb', line 25

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)


47
48
49
50
51
52
53
# File 'app/models/concerns/spree/stores/markets.rb', line 47

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)


36
37
38
39
40
41
42
# File 'app/models/concerns/spree/stores/markets.rb', line 36

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

#has_markets?Boolean

Returns true if the store has markets, false otherwise

Returns:

  • (Boolean)


106
107
108
# File 'app/models/concerns/spree/stores/markets.rb', line 106

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:



58
59
60
# File 'app/models/concerns/spree/stores/markets.rb', line 58

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>)


84
85
86
87
88
89
90
91
92
# File 'app/models/concerns/spree/stores/markets.rb', line 84

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>)


96
97
98
99
100
101
102
# File 'app/models/concerns/spree/stores/markets.rb', line 96

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