Sha256: 67eda86e6616c02c843f9f4fa9cbf8e666c449089fe77a3c749cbf0743b4b41c

Contents?: true

Size: 1.51 KB

Versions: 21

Compression:

Stored size: 1.51 KB

Contents

module Spree
  class BaseMailer < ActionMailer::Base
    helper Spree::MailHelper

    def current_store
      @current_store ||= @order&.store.presence || Spree::Dependencies.current_store_finder.constantize.new.execute
    end
    helper_method :current_store

    def from_address
      current_store.mail_from_address
    end

    def reply_to_address
      current_store.mail_from_address
    end

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

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

    def mail(headers = {}, &block)
      ensure_default_action_mailer_url_host(headers[:store_url])
      set_email_locale
      super if Spree::Config[:send_core_emails]
    end

    private

    # this ensures that ActionMailer::Base.default_url_options[:host] is always set
    # this is only a fail-safe solution if developer didn't set this in environment files
    # http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views
    def ensure_default_action_mailer_url_host(store_url = nil)
      ActionMailer::Base.default_url_options ||= {}
      ActionMailer::Base.default_url_options[:host] ||= store_url || current_store.url
    end

    def set_email_locale
      locale = @order&.store&.default_locale || current_store&.default_locale
      I18n.locale = locale if locale.present?
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
spree_emails-4.10.1 app/mailers/spree/base_mailer.rb
spree_emails-4.10.0 app/mailers/spree/base_mailer.rb
spree_emails-4.9.0 app/mailers/spree/base_mailer.rb
spree_emails-4.8.3 app/mailers/spree/base_mailer.rb
spree_emails-4.8.2 app/mailers/spree/base_mailer.rb
spree_emails-4.7.3 app/mailers/spree/base_mailer.rb
spree_emails-4.6.6 app/mailers/spree/base_mailer.rb
spree_emails-4.5.5 app/mailers/spree/base_mailer.rb
spree_emails-4.5.4 app/mailers/spree/base_mailer.rb
spree_emails-4.6.5 app/mailers/spree/base_mailer.rb
spree_emails-4.7.1 app/mailers/spree/base_mailer.rb
spree_emails-4.6.4 app/mailers/spree/base_mailer.rb
spree_emails-4.7.0 app/mailers/spree/base_mailer.rb
spree_emails-4.6.3 app/mailers/spree/base_mailer.rb
spree_emails-4.6.2 app/mailers/spree/base_mailer.rb
spree_emails-4.6.1 app/mailers/spree/base_mailer.rb
spree_emails-4.6.0 app/mailers/spree/base_mailer.rb
spree_emails-4.5.3 app/mailers/spree/base_mailer.rb
spree_emails-4.5.2 app/mailers/spree/base_mailer.rb
spree_emails-4.5.1 app/mailers/spree/base_mailer.rb