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_store ⇒ Object



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_address ⇒ Object



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

def from_address
  current_store.mail_from_address
end

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



64
65
66
67
68
69
# File 'app/mailers/spree/base_mailer.rb', line 64

def mail(headers = {}, &block)
  ensure_default_action_mailer_url_host(headers[:store_url])
  # Mailers that don't wrap their action in `with_store_locale` still get
  # the store's default locale, as `mail` applied before Spree 5.6.
  in_store_locale { super }
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

#render_in_email_layout(html) ⇒ ActiveSupport::SafeBuffer

Wraps HTML a mailer rendered from its own ERB views in the Liquid email layout, so every email a Spree mailer sends carries the store's logo, header and footer. Called by layouts/spree/base_mailer.html.erb.

Parameters:

  • html (String) —

    the rendered view

Returns:

  • (ActiveSupport::SafeBuffer)


59
60
61
# File 'app/mailers/spree/base_mailer.rb', line 59

def render_in_email_layout(html)
  email_renderer.wrap(html, subject: message.subject).html_safe
end

#reply_to_address ⇒ Object



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

def reply_to_address
  current_store.support_email_address
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
    Spree::Current.with_content_locale(store&.default_market&.default_locale) { I18n.with_locale(locale, &block) }
  ensure
    @_store_locale_active = previously_active
    Mobility.store_based_fallbacks = previous_fallbacks
  end
end