Solidus: How to get rails actionmailer::preview to work with spree?

Created on 7 Sep 2018  路  9Comments  路  Source: solidusio/solidus

Steps to reproduce

We would like to make use of rails built in email template preview feature, but we can't seem to set it up properly with our solidus/spree app. We followed the steps mentioned within https://edgeguides.rubyonrails.org/action_mailer_basics.html

Any help would be much appreciated.

Most helpful comment

I managed to have it working only by monkey patching:

# config/initializers/mail_previews.rb

module ActionMailer
  class Preview
    def self.all
      load_previews
      descendants
    end
  end
end

All 9 comments

Are you looking for previews of the solidus emails? If yes I think you can just find them into /rails/mailers/

@mobentec Can you share the code you did? Otherwise it's complicated to figurate what is the problem.

Personally, for mail previews I prefer to use letter_opener_web that not require too much extra configuration

I think we can close this one. Please reopen it if you have more details.

I'm stuck with the same problem. It seems that rails stops seeing mailer_preview folder after installing solidus.

It's where I putted it:

 |_mailer_previews
          |_test_mailer.rb

also, added this line into application.rb:

config.action_mailer.preview_path = "#{Rails.root}/app/mailer_previews"

as it written in the rails docs
https://edgeguides.rubyonrails.org/action_mailer_basics.html#previewing-emails

But get this:

All available email previews:

test_mailer.rb

class TestMailer < ApplicationMailer
  layout "mailer"

  def test_email
    mail to: "[email protected]", subject: "Welcome to #{ENV['app_default_name']}"
  end
end

versions
rails: 5.2.1
solidus: 2.7.0
ruby: 2.5.1

Preview confirmation emails path: http://localhost:3000/rails/mailers/

Source code core copy from: /solidus/core/app/views/spree/order_mailer/confirm_email.text.erb
Override code path on your app: /app/views/spree/order_mailer/confirm_email.html.erb

@mobentec you need to add this to your initializers folder

ActionMailer::Preview.send(:load_previews) if Rails.env.development?

There is a reason https://github.com/rails/rails/blob/v5.0.7/actionmailer/lib/action_mailer/preview.rb#L57

descendants already initialized with

[Spree::MailerPreviews::OrderPreview, Spree::MailerPreviews::CartonPreview, Spree::MailerPreviews::ReimbursementPreview]

on app boot, for this reason load_previews never calls.

I managed to have it working only by monkey patching:

# config/initializers/mail_previews.rb

module ActionMailer
  class Preview
    def self.all
      load_previews
      descendants
    end
  end
end

Hey all,

The above statement results in a warning 6.0.3.4 using Zeitwerk. In the future this warning will end up being an error, I believe this issue warrants another look as using Solidus as an engine within an existing Rails application does indeed blow away the previews in the containing Rails app. The above monkey patch against ActionMailer::Preview does indeed solve the issue, however it will soon break as soon as the constant definitions during initialization becomes an error in future versions of Rails.

Apologies, what I wrote above is true however the monkey patch is not the culprit. Rather the way in which the Previews are being loaded in core's engine.rb is. I'll file a bug with the project in addition to leaving the following

DEPRECATION WARNING: Initialization autoloaded the constants ...Spree::EmailNudgeMailerPreview, ..

If you remove the following line from lib/spree/core/engine.rb:101

if Rails.env.development? initializer "spree.mailer_previews" do ActionMailer::Preview.all #<-- Dir[root.join("lib/spree/mailer_previews/**/*_preview.rb")].each do |file| require file end end end

Then the error goes away, though obviously a different issue appears. This mailer is not provided by Spree, it is instead one of ours from our project FWIW.

Was this page helpful?
0 / 5 - 0 ratings