gpt4 book ai didi

ruby-on-rails - 确认邮件 - 如何避免代码重复

转载 作者:行者123 更新时间:2023-12-04 11:02:23 24 4
gpt4 key购买 nike

我的应用程序中有两个不同的进程,之后我想在每个进程完成后向用户发送一封确认电子邮件。我可以创建两个单独的邮件,例如。 BankProcessMailerShopProcessMailer但它们将包含如下相同的代码:

class BankProcess < BaseMailer
layout 'bank_process'

def send_email(email)
mail_headers = headers(email)
mail(mail_headers)
end

private

def headers(email)
super(email).merge(subject: 'Bank process')
end
end

是否有机会处理具有两种不同 View 和主题的邮件程序以避免代码重复,并且不要为 ShopProcessMailer 创建新邮件程序?像下面?
class ShopProcess < BaseMailer
layout 'shop_process'

def send_email(email)
mail_headers = headers(email)
mail(mail_headers)
end

private

def headers(email)
super(email).merge(subject: 'Shop process')
end
end

最佳答案

如果有什么变化只是subject和布局,可以创建一个继承BaseMailer的类,然后用来继承ShopProcessBankProcess从,那么你可以尝试这样的事情:

class ConfirmationMailer < BaseMailer
protected

def send_email(email)
mail_headers = headers(email)
mail(mail_headers)
end

def headers(email)
super(email).merge(subject: SUBJECT)
end
end

# frozen_string_literal: true

class BankProcess < ConfirmationMailer
SUBJECT = 'Bank process'
layout 'bank_process'
end

# frozen_string_literal: true

class ShopProcess < ConfirmationMailer
SUBJECT = 'Shop process'
layout 'shop_process'
end

关于ruby-on-rails - 确认邮件 - 如何避免代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58714643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com