Last active
April 2, 2021 18:43
-
-
Save Lukom/a0135204de7e8a5eedaf45a0952eedc5 to your computer and use it in GitHub Desktop.
Preview Emails in ActiveAdmin / Rails 5.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/admin/email_previews.rb | |
ActiveAdmin.register_page 'Email Previews' do | |
menu parent: 'dashboard', priority: 10 | |
content do | |
div '1' | |
end | |
sidebar 'Mail Previews' do | |
Dir['test/mailers/previews/**/*_preview.rb'].each do |preview_path| | |
preview_mailer = File.basename(preview_path, '.rb') | |
mailer = preview_mailer.chomp('_preview') | |
div { strong { mailer } } | |
preview_mailer_class = preview_mailer.camelize.constantize | |
mails = preview_mailer_class.public_instance_methods(false).map(&:to_s).sort | |
mails.each do |mail| | |
div { link_to mail, {action: :preview, mailer: mailer, mail: mail} } | |
end | |
br | |
end | |
end | |
page_action :preview | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-# app/views/admin/email_previews/preview.haml | |
%iframe(src="/admin/mailers/#{params[:mailer]}/#{params[:mail]}" | |
style="width: 100%;height: 500px;border: 2px solid lightgrey;") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/preview_emails.rb | |
Rails.application.configure do | |
config.action_mailer.show_previews = true | |
config.action_mailer.preview_path ||= "#{Rails.root}/test/mailers/previews" | |
config.autoload_paths += [config.action_mailer.preview_path] | |
routes.append do | |
get '/admin/mailers' => 'rails/mailers#index' | |
get '/admin/mailers/*path' => 'rails/mailers#preview' | |
end | |
end | |
class Rails::MailersController | |
include ApplicationController::CurrentMethods | |
before_action :require_admin | |
private | |
def require_admin | |
redirect_to root_path unless current_user&.admin? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment