Skip to content

Instantly share code, notes, and snippets.

@Lukom
Last active April 2, 2021 18:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Lukom/a0135204de7e8a5eedaf45a0952eedc5 to your computer and use it in GitHub Desktop.
Save Lukom/a0135204de7e8a5eedaf45a0952eedc5 to your computer and use it in GitHub Desktop.
Preview Emails in ActiveAdmin / Rails 5.1
# 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
-# app/views/admin/email_previews/preview.haml
%iframe(src="/admin/mailers/#{params[:mailer]}/#{params[:mail]}"
style="width: 100%;height: 500px;border: 2px solid lightgrey;")
# 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