Module: Spree::CurrencyHelper

Defined in:
app/helpers/spree/currency_helper.rb

Instance Method Summary collapse

Instance Method Details

#currency_money(currency = current_currency) ⇒ Object



57
58
59
# File 'app/helpers/spree/currency_helper.rb', line 57

def currency_money(currency = current_currency)
  ::Money::Currency.find(currency)
end

#currency_options(selected_value = nil) ⇒ String

Returns the list of all currencies as options for a select field. By default the value is the default currency of the default store.

Parameters:

  • selected_value (String) (defaults to: nil)

    the selected value

Returns:

  • (String)

    the options for a select field



7
8
9
10
11
12
13
# File 'app/helpers/spree/currency_helper.rb', line 7

def currency_options(selected_value = nil)
  selected_value ||= Spree::Store.default.default_currency
  currencies = ::Money::Currency.table.map do |_code, details|
    currency_presentation(details[:iso_code])
  end
  options_from_collection_for_select(currencies, :last, :first, selected_value)
end

#currency_presentation(currency) ⇒ Array

Returns the currency presentation and ISO code.

Parameters:

  • currency (String)

    the currency ISO code

Returns:

  • (Array)

    the currency presentation and ISO code



38
39
40
41
42
43
# File 'app/helpers/spree/currency_helper.rb', line 38

def currency_presentation(currency)
  currency_money = currency_money(currency)
  label = "#{currency_money.name} (#{currency_money.iso_code})"

  [label, currency]
end

#currency_symbol(currency) ⇒ String

Returns the currency symbol for the given currency.

Parameters:

  • currency (String)

    the currency ISO code

Returns:

  • (String)

    the currency symbol



32
33
34
# File 'app/helpers/spree/currency_helper.rb', line 32

def currency_symbol(currency)
  ::Money::Currency.find(currency).symbol
end

#preferred_currenciesArray<Money::Currency>

Returns the list of supported currencies for the current store.

Returns:

  • (Array<Money::Currency>)

    the list of supported currencies



47
48
49
50
# File 'app/helpers/spree/currency_helper.rb', line 47

def preferred_currencies
  Spree::Deprecation.warn('preferred_currencies is deprecated and will be removed in Spree 6.0. Use current_store.supported_currencies_list instead.')
  @preferred_currencies ||= current_store.supported_currencies_list
end

#preferred_currencies_select_optionsObject



52
53
54
55
# File 'app/helpers/spree/currency_helper.rb', line 52

def preferred_currencies_select_options
  Spree::Deprecation.warn('preferred_currencies_select_options is deprecated and will be removed in Spree 6.0. Use supported_currency_options instead.')
  preferred_currencies.map { |currency| currency_presentation(currency) }
end

#should_render_currency_dropdown?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'app/helpers/spree/currency_helper.rb', line 23

def should_render_currency_dropdown?
  return false if current_store.nil?

  current_store.supported_currencies_list.size > 1
end

#supported_currency_optionsString

Returns the list of supported currencies for the current store as options for a select field.

Returns:

  • (String)

    the options for a select field



17
18
19
20
21
# File 'app/helpers/spree/currency_helper.rb', line 17

def supported_currency_options
  return if current_store.nil?

  @supported_currency_options ||= current_store.supported_currencies_list.map(&:iso_code).map { |currency| currency_presentation(currency) }
end