Class: Spree::BaseMailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
app/mailers/spree/base_mailer.rb

Instance Method Summary collapse

Instance Method Details

#current_storeObject



7
8
9
# File 'app/mailers/spree/base_mailer.rb', line 7

def current_store
  @current_store ||= @order&.store.presence || Spree::Store.current || Spree::Store.default
end

#from_addressObject



39
40
41
# File 'app/mailers/spree/base_mailer.rb', line 39

def from_address
  current_store.mail_from_address
end

#frontend_available?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/mailers/spree/base_mailer.rb', line 53

def frontend_available?
  Spree::Core::Engine.frontend_available?
end

#mail(headers = {}, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/mailers/spree/base_mailer.rb', line 58

def mail(headers = {}, &block)
  ensure_default_action_mailer_url_host(headers[:store_url])
  return unless Spree::Config[:send_core_emails]

  if @_store_locale_active
    super
  else
    # Subclasses that call `mail` without wrapping their action in
    # `with_store_locale` (e.g. Devise mailers, extensions) still get the
    # store default locale, as `mail` applied before Spree 5.6.
    with_store_locale(current_store) { super }
  end
end

#money(amount, currency = nil) ⇒ Object



47
48
49
50
# File 'app/mailers/spree/base_mailer.rb', line 47

def money(amount, currency = nil)
  currency ||= current_store.default_currency
  Spree::Money.new(amount, currency: currency).to_s
end

#reply_to_addressObject



43
44
45
# File 'app/mailers/spree/base_mailer.rb', line 43

def reply_to_address
  current_store.customer_support_email.presence || current_store.mail_from_address
end

#set_email_localeObject

Deprecated.

Each mailer action now wraps its body in #with_store_locale, which also activates the store's translation fallbacks and restores the previous locale afterwards. This method mutates I18n.locale for the rest of the thread without restoring it. Will be removed in Spree 6.0.



76
77
78
79
80
81
82
83
# File 'app/mailers/spree/base_mailer.rb', line 76

def set_email_locale
  Spree::Deprecation.warn(
    'Spree::BaseMailer#set_email_locale is deprecated and will be removed in Spree 6.0. ' \
    'Wrap the mailer action body in `with_store_locale(store, locale) { ... }` instead.'
  )
  locale = @order&.locale.presence || @order&.store&.default_locale || current_store&.default_locale
  I18n.locale = locale if locale.present?
end

#with_store_locale(store, locale = nil, &block) ⇒ Object

Render an email in the given locale, with the store's translation fallbacks active, and restore both afterwards. Controllers set these fallbacks per request via set_fallback_locale, but mailers run in background jobs where that never happens — so without this, translatable attributes (store name, product names, taxon names, …) return nil under a non-default locale and leave e.g. the footer blank. Setting the fallbacks here mirrors a request, so reads fall back to the store's default-locale value.

Parameters:

  • store (Spree::Store)
  • locale (String, Symbol, nil) (defaults to: nil)

    defaults to the store's default locale



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/mailers/spree/base_mailer.rb', line 23

def with_store_locale(store, locale = nil, &block)
  locale = locale.presence || store&.default_locale
  return yield if locale.blank?

  previous_fallbacks = Mobility.store_based_fallbacks
  previously_active = @_store_locale_active
  @_store_locale_active = true
  begin
    Spree::Locales::SetFallbackLocaleForStore.new.call(store: store) if store
    I18n.with_locale(locale, &block)
  ensure
    @_store_locale_active = previously_active
    Mobility.store_based_fallbacks = previous_fallbacks
  end
end