AutoPreviews
I got tired of writing ActionMailer::Preview classes. So I let Ruby write it for me.
Usage
In your mailer class, define some options:
class PostMailer < ApplicationMailer
previews_for model: 'Post', # otherwise automatically infered based on class.name.delete_suffix('Mailer')
params: { post_id: :id },
only: [:created]
previews_for model: 'Post',
params: { post_id: :id },
only: [:deleted],
scope: :with_deleted,
using: :arguments
before_action only: [:created] do
@post = Post.find_by(id: params[:post_id])
end
def created
mail(to: "joshmn@example.com", subject: "Nice post #{@post.id}")
end
def deleted(post_id)
@post = Post.find_by(id: post_id)
mail(to: "joshmn@example.com", subject: "Deleted post #{@post.id}")
end
endIn your previewer class, just call auto_preview!
class PostMailerPreview < ActionMailer::Preview
auto_preview!
end auto_preview! will do some metaprogramming to create your mailer previews, like so:
post = Post.first
params_to_send_to_mailer = {}
params_given = { post_id: :id }
params_given.each do |mailer_key, model_method|
params_to_send_to_mailer[mailer_key] = post.public_send(model_method)
end
PostMailer.with(params_to_send_to_mailer).created # or `*params_to_send_to_mailer.values` if `using: :arguments`Installation
- Add this line to your application's Gemfile:
gem 'auto_previews'- And then execute:
$ bundle-
Restart your Rails server
-
Implement
-
Enjoy!
Options
By default, the previews_for will define mailer preview methods for class.instance_methods(false).
-
model: the model you want to use for the record lookup; defaults toself.class.name.delete_suffix('Mailer'); iffalseis passed, no model will be used -
params: a hash ofmailer_method_name: :model_method_name; used to map values to the mailer from the model -
using: the type of mailer to use; defaults to usingActionMailer::Parameterized -
only: the methods to only use the givenpreviews_foron; defaults to[] -
except: the methods to not use the givenpreviews_foron; defaults to[] -
scope: scope to use on the model; defaults to:all
Usage without a model
Just pass model: false and define some sort of values to pass in the params:
Passing a proc will call; passing a symbol will call the given method on the mailer preview class if it is defined, otherwise it will be sent to your mailer preview as the symbol.
class OneOffMailer < ApplicationMailer
previews_for model: false,
params: {
subject: "Hi",
body: -> { FFaker::CheesyLingo.paragraph },
}
end Roadmap
- More options
- Better extensibility
- Better record lookup
- Better serialization of mailer params
License
The gem is available as open source under the terms of the MIT License.