mcpField
A
action-mailer-coder

action-mailer-coder

github.com/majesticlabs-dev开发5067 次安装7 周前更新
代码仓库

关于 action-mailer-coder

为 Ruby on Rails 编码 Agent 提供 Action Mailer 设计与实现指导,涵盖邮件组织、后台投递、预览、国际化、附件和 RSpec 测试。

主要功能

  1. 01组织相关邮件与参数化 Mailer
  2. 02配置默认发件人与继承
  3. 03后台投递与延迟发送
  4. 04预览邮件并支持国际化
  5. 05处理附件与编写 RSpec 测试

适用场景

  1. 01构建账户通知和评论回复邮件
  2. 02安排欢迎邮件、摘要或发票投递
  3. 03为邮件模板添加预览与测试

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/majesticlabs-dev/majestic-marketplace --skill action-mailer-coder

安装后在 Claude Code 中输入/action-mailer-coder确认已加载。

详细介绍

Action Mailer 编码指南

邮件设计原则

将相关邮件分组
class NotificationMailer < ApplicationMailer
  def comment_reply(user, comment)
    @user = user
    @comment = comment
    mail(to: @user.email, subject: "New reply to your comment")
  end

  def mentioned(user, mention)
    @user = user
    @mention = mention
    mail(to: @user.email, subject: "You were mentioned")
  end
end
参数化邮件发送器
class NotificationMailer < ApplicationMailer
  before_action { @user = params.fetch(:user) }
  before_action { @account = params.fetch(:account) }

  def comment_reply
    @comment = params.fetch(:comment)
    mail(to: @user.email, subject: "New reply on #{@account.name}")
  end
end

# Calling the mailer
NotificationMailer.with(user: user, account: account, comment: comment).comment_reply.deliver_later
通过继承设置动态默认值
class AccountMailer < ApplicationMailer
  default from: -> { build_from_address }
  before_action { @account = params.fetch(:account) }

  private

  def build_from_address
    @account.custom_email_sender? ?
      email_address_with_name(@account.custom_email_address, @account.custom_email_name) :
      email_address_with_name("[email protected]", @account.name)
  end
end

后台发送

UserMailer.with(user: user).welcome.deliver_later                    # Immediate queue
UserMailer.with(user: user).welcome.deliver_later(wait: 1.hour)      # Delayed
UserMailer.with(user: user).digest.deliver_later(wait_until: Date.tomorrow.morning)  # Scheduled

邮件预览

# test/mailers/previews/notification_mailer_preview.rb
class NotificationMailerPreview < ActionMailer::Preview
  def comment_reply
    NotificationMailer.with(
      user: User.first,
      account: Account.first,
      comment: Comment.first
    ).comment_reply
  end
end

访问地址:http://localhost:3000/rails/mailers

国际化

def welcome
  @user = params.fetch(:user)
  I18n.with_locale(@user.locale) do
    mail(to: @user.email, subject: t(".subject", name: @user.name))
  end
end

附件

def invoice(order)
  attachments.inline["logo.png"] = File.read("app/assets/images/logo.png")
  attachments["invoice.pdf"] = generate_pdf(order)
  mail(to: order.email, subject: "Your Invoice ##{order.number}")
end

测试(RSpec)

RSpec.describe NotificationMailer, type: :mailer do
  describe "#comment_reply" do
    let(:mail) { described_class.with(user: user, comment: comment).comment_reply }

    it "renders the headers" do
      expect(mail.subject).to match(/New reply/)
      expect(mail.to).to eq([user.email])
    end

    it "delivers later" do
      expect { mail.deliver_later }.to have_enqueued_job(ActionMailer::MailDeliveryJob)
    end
  end
end

反模式

反模式问题解决方案
每封邮件都使用单独的邮件发送器难以浏览将相关邮件分组
跳过 .with()依赖关系不明确使用参数化邮件发送器
deliver_now阻塞请求使用 deliver_later
缺少预览无法进行视觉测试创建预览类

输出格式

创建邮件发送器时,请提供:

  1. 邮件发送器类 - 完整实现
  2. 视图 - HTML 和文本模板
  3. 预览 - 用于视觉测试的预览类
  4. 测试 - 示例测试用例

评论

支持 Markdown