ruby-on-rails 如何在“邮件预览”中使用设置方法?

2sbarzqh  于 2023-10-21  发布在  Ruby
关注(0)|答案(4)|浏览(116)

我想避免重复设置多个邮件预览。什么是最好的方法来清理这个吗?

class MyMailerPreview < ActionMailer::Preview
  def email1
    setup
    mailer.email1
  end

  def email2
    setup
    mailer.email2
  end

  def email3
    setup
    mailer.email3
  end
end
qkf9rpyu

qkf9rpyu1#

以下是我发现的两种可能的解决方案:
有一个叫做preview_interceptors的东西在生成邮件预览时使用,你可以像这样添加你自己的:
config/environments/development.rb

config.action_mailer.preview_interceptors = :my_setup

test/mailers/previews/my_setup.rb

class MySetup                                                                                         
  def self.previewing_email(message)                                                                   
    message.subject = "New subject"                                                                                             
  end                                                                                                  
end

test/mailers/previews/user_mailer_preview.rb

class UserMailerPreview < ActionMailer::Preview 
  include ActionMailer::Previews
  register_preview_interceptor :my_setup

  def welcome_email                                                                                    
    UserMailer.with(user: User.first).welcome_email                                                    
  end                                                                                                                                                                                                                                                                                                         
end

message参数是ActionMailer::Parameterized::MessageDelivery的一个示例,我不确定你能用它做什么,但你可以在电子邮件本身设置一些属性。
我找不到太多关于预览拦截器的文档,但这里有一个链接,介绍如何在Rails中使用它们。

# Previews can also be intercepted in a similar manner as deliveries can be by registering
  # a preview interceptor that has a <tt>previewing_email</tt> method:
  #
  #   class CssInlineStyler
  #     def self.previewing_email(message)
  #       # inline CSS styles
  #     end
  #   end
  #
  #   config.action_mailer.preview_interceptors :css_inline_styler
  #
  # Note that interceptors need to be registered both with <tt>register_interceptor</tt>
  # and <tt>register_preview_interceptor</tt> if they should operate on both sending and
  # previewing emails.

我试图在类中包含Rails before_action,但它不会在预览器中挂钩方法,所以我发现的第二个选择是像这样构建自己的before_action

module MySetup                                                                                      
  def before_action(*names)                                                                         
    UserMailer.instance_methods(false).each do |method|                                             
      alias_method "old_#{method}", method                                                          

      define_method method do                                                                       
        names.each do |name|                                                                        
          send(name)                                                                                
        end                                                                                         

        send("old_#{method}")                                                                       
      end                                                                                           
    end                                                                                             
  end                                                                                               
end    

class UserMailerPreview < ActionMailer::Preview 
  extend MySetup

  def welcome_email                                                                                 
    UserMailer.with(user: User.first).welcome_email                                                 
  end 

  before_action :setup

  private

  def setup
    puts "Setting up"
  end
end
sz81bmfz

sz81bmfz2#

使用initialize方法。

只需覆盖父initialize方法,调用super,然后运行设置:

class MyMailerPreview < ActionMailer::Preview

  def initialize( params = {} )
    super( params )

    @email_address = "[email protected]"
  end

  def email1
    mailer.email1( @email_address )
  end

end

您可以在这里查看ActionMailer::Preview.new方法作为参考。

dhxwm5r4

dhxwm5r43#

根据我对你所问问题的理解,也许你可以把它添加到一个单独的方法中,该方法将mailer方法作为参数

class MyMailerPreview < ActionMailer::Preview
  def email_for(emailx) # (Pass the method(email1, etc) as an argument where you're calling it
    setup
    mailer.send(emailx.to_sym) # Call the method param as a method on the mailer
  end
end

这样行吗

xxb16uws

xxb16uws4#

可以扩展Rails::MailersController

# config/initializers/mailer_preview_setup.rb

class Rails::MailersController
  before_action :setup, only: [:preview, :download]

  private

  def setup
    # your code here
  end
end

prepend来覆盖

# config/initializers/mailer_preview_setup.rb
module MailerPreviewSetup
  def preview
    setup
    super
  end

  private

  def setup
    # your code here
  end
end

Rails.configuration.to_prepare do
  Rails::MailersController.prepend MailerPreviewSetup
end

相关问题