action-mailer-coder について
Rails の Action Mailer を使うコーディングエージェント向けに、メーラーの設計と実装を案内するスキルです。バックグラウンド配信、プレビュー、添付ファイル、RSpec テストなどを扱います。
主な機能
- 01関連メールの集約とパラメーター化
- 02継承を使った動的な既定値の設定
- 03配信のキュー登録と予約
- 04メールのプレビューとローカライズ
- 05添付ファイル処理と RSpec テスト
活用シーン
- 01アカウント通知やコメント返信の作成
- 02ウェルカムメール、ダイジェスト、請求書の配信予約
- 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 を使う |
| プレビューがない | 見た目をテストできない | プレビュークラスを作成する |
出力形式
メーラーを作成するときは、以下を提示してください。
- メーラークラス - 完全な実装
- ビュー - HTML およびテキストのテンプレート
- プレビュー - 見た目のテスト用プレビュークラス
- テスト - テストケースの例
コメント
Markdown 対応