Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/courrier/email.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "erb"

require "courrier/email/address"
require "courrier/jobs/email_delivery_job" if defined?(Rails)
require "courrier/email/layouts"
Expand Down Expand Up @@ -133,7 +135,25 @@ def delivery_disabled?
ENV["COURRIER_EMAIL_DISABLED"] == "true" || ENV["COURRIER_EMAIL_ENABLED"] == "false"
end

def method_missing(name, *) = @context_options[name]
def method_missing(name, *)
if name == :text || name == :html
render_template(name.to_s)
else
@context_options[name]
end
end

def render_template(format)
template_path = template_file_path(format)

File.exist?(template_path) ? ERB.new(File.read(template_path)).result(binding) : nil
end

def template_file_path(format)
class_path = self.class.name.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase

File.join(Courrier.configuration&.email_path, "#{class_path}.#{format}.erb")
end

def respond_to_missing?(name, include_private = false) = true
end
Expand Down
46 changes: 46 additions & 0 deletions test/courrier/email_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,50 @@ def test_url_generation_with_missing_host

assert_equal "Click here: ", email.text
end

def test_template_rendering_with_files
email_path = "tmp/test_emails"

FileUtils.mkdir_p(email_path)
File.write("#{email_path}/test_email_with_templates.text.erb", "Hello <%= name %>!")
File.write("#{email_path}/test_email_with_templates.html.erb", "<p>Hello <strong><%= name %></strong>!</p>")

Courrier.configure do |config|
config.email_path = email_path
end

email = TestEmailWithTemplates.new(
to: "recipient@railsdesigner.com",
from: "devs@railsdesigner.com",
name: "World"
)

assert_equal "Hello World!", email.text
assert_equal "<p>Hello <strong>World</strong>!</p>", email.html

FileUtils.rm_rf(email_path)
end

def test_method_takes_precedence_over_template
email_path = "tmp/test_emails"

FileUtils.mkdir_p(email_path)
File.write("#{email_path}/test_email_with_mixed_content.text.erb", "Template text")
File.write("#{email_path}/test_email_with_mixed_content.html.erb", "<p>Template HTML</p>")

Courrier.configure do |config|
config.email_path = email_path
end

email = TestEmailWithMixedContent.new(
to: "recipient@railsdesigner.com",
from: "devs@railsdesigner.com"
)

assert_equal "Method text", email.text
assert_equal "<p>Template HTML</p>", email.html

FileUtils.rm_rf(email_path)
end

end
8 changes: 8 additions & 0 deletions test/fixtures/test_email_with_mixed_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "courrier/email"

class TestEmailWithMixedContent < Courrier::Email
def subject = "Mixed Content Test"

def text = "Method text"
# html is defined in template
end
5 changes: 5 additions & 0 deletions test/fixtures/test_email_with_templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "courrier/email"

class TestEmailWithTemplates < Courrier::Email
def subject = "Template Test"
end