Module: Spree::Core::ControllerHelpers::Currency

Extended by:
ActiveSupport::Concern
Included in:
BaseController
Defined in:
lib/spree/core/controller_helpers/currency.rb

Instance Method Summary collapse

Instance Method Details

#currency_paramString

Returns the currency parameter from the request.

Returns:

  • (String)

    the currency parameter, eg. ‘USD`



68
69
70
71
72
# File 'lib/spree/core/controller_helpers/currency.rb', line 68

def currency_param
  return if current_currency == current_store.default_currency

  current_currency
end

#current_currencyString

Returns the currently selected currency.

Returns:

  • (String)

    the currently selected currency, eg. ‘USD`



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spree/core/controller_helpers/currency.rb', line 19

def current_currency
  @current_currency ||= begin
    currency = if defined?(session) && session.key?(:currency) && supported_currency?(session[:currency])
                 session[:currency]
               elsif params[:currency].present? && supported_currency?(params[:currency])
                 params[:currency]
               elsif current_store.present?
                 current_store.default_currency
               else
                 Spree::Store.default.default_currency
               end&.upcase
    Spree::Current.currency = currency
    currency
  end
end

#supported_currenciesArray<Money::Currency>

Returns the list of supported currencies for the current store.

Returns:

  • (Array<Money::Currency>)

    the list of supported currencies



37
38
39
# File 'lib/spree/core/controller_helpers/currency.rb', line 37

def supported_currencies
  @supported_currencies ||= current_store&.supported_currencies_list
end

#supported_currencies_for_all_storesArray<String>

Deprecated.

This method will be removed in Spree 5.5.

Returns the list of supported currencies for all stores.

Returns:

  • (Array<String>)

    the list of supported currencies, eg. ‘[“USD”, “EUR”]`



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spree/core/controller_helpers/currency.rb', line 44

def supported_currencies_for_all_stores
  Spree::Deprecation.warn(
    'supported_currencies_for_all_stores is deprecated and will be removed in Spree 5.5.'
  )

  @supported_currencies_for_all_stores ||= begin
    (
      Spree::Store.pluck(:supported_currencies).map { |c| c&.split(',') }.flatten + Spree::Store.pluck(:default_currency)
    ).
      compact.uniq.map { |code| ::Money::Currency.find(code.strip) }
  end
end

#supported_currency?(currency_iso_code) ⇒ Boolean

Checks if the given currency is supported.

Parameters:

  • currency_iso_code (String)

    the ISO code of the currency, eg. ‘USD`

Returns:

  • (Boolean)

    ‘true` if the currency is supported, `false` otherwise



60
61
62
63
64
# File 'lib/spree/core/controller_helpers/currency.rb', line 60

def supported_currency?(currency_iso_code)
  return false if supported_currencies.nil?

  supported_currencies.map(&:iso_code).include?(currency_iso_code.upcase)
end