Browse Source

Move rendering of mail body to a utility class

feature/tags
Bèr Kessels 1 year ago
parent
commit
ce50e56fa1

+ 4
- 6
app/reactors/confirmation_mailer.rb View File

@@ -19,7 +19,10 @@ module Reactors
to: address,
from: 'Bèr at Flockingbird <ber@flockinbird.social>',
subject: 'Welcome to Flockingbird. Please confirm your email address',
body: render(:registration_mail, confirmation_url(aggregate_id))
body: MailRenderer.new.render(
:registration_mail,
confirmation_url: confirmation_url(aggregate_id)
)
}
Mail.new(email_attrs).deliver

@@ -33,11 +36,6 @@ module Reactors

private

def render(template, confirmation_url)
file = File.read(File.join("app/mail/#{template}.erb"))
ERB.new(file).result_with_hash(confirmation_url: confirmation_url)
end

def confirmation_url(aggregate_id)
"#{Roost.config.web_url}/confirmation/#{aggregate_id}"
end

+ 5
- 0
config/environment.rb View File

@@ -5,6 +5,7 @@ require 'uuidtools'
require 'event_sourcery'
require 'event_sourcery/postgres'

Dir.glob("#{__dir__}/../lib/*.rb").sort.each { |f| require f }
Dir.glob("#{__dir__}/../app/events/*.rb").sort.each { |f| require f }
Dir.glob("#{__dir__}/../app/reactors/*.rb").sort.each { |f| require f }
Dir.glob("#{__dir__}/../app/projections/**/projector.rb").sort.each do |f|
@@ -53,6 +54,10 @@ class Roost
@mailer = Mail::TestMailer
end

def self.root
Pathname.new(File.expand_path("#{__dir__}/../"))
end

def self.event_store
EventSourcery::Postgres.config.event_store
end

+ 23
- 0
lib/mail_renderer.rb View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true

##
# Mail Renderer: renders ERB templates from app/mail into mail
# body
class MailRenderer
def initialize(templates_root: 'app/mail/', renderer: ERB)
@templates_root = templates_root
@renderer = renderer
end

def render(template, locals = {})
renderer.new(file(template)).result_with_hash(locals)
end

private

attr_reader :templates_root, :renderer

def file(template)
File.read(File.join("#{templates_root}/#{template}.erb"))
end
end

+ 38
- 0
test/lib/mail_renderer_test.rb View File

@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'test_helper'

class MailRendererTest < Minitest::Spec
let(:templates_root) { Roost.root.join('tmp').to_s }
subject { MailRenderer.new(templates_root: templates_root) }

before :each do
# ensure we have a template file
File.write(File.join(templates_root, "#{template}.erb"), template_body)
end

after :each do
# Clean up the template file
File.unlink(File.join(templates_root, "#{template}.erb"))
end

describe '#render' do
let(:template) { :test_mail_body }
let(:template_body) { 'Hello <%= planet %>' }

it 'renders a template with ERB' do
assert_equal(
subject.render(template, { planet: 'world' }),
'Hello world'
)
end

it 'raises a NameError for unkown locals' do
assert_raises(NameError) { subject.render(template, {}) }
end

it 'raises an Errno::ENOENT for a missing template' do
assert_raises(Errno::ENOENT) { subject.render(:missing) }
end
end
end

+ 0
- 0
tmp/.gitkeep View File


Loading…
Cancel
Save