Skip to content

Commit

Permalink
Merge pull request #6 from stevenh512/render_options
Browse files Browse the repository at this point in the history
Support custom renderer and render options
  • Loading branch information
josevalim committed May 28, 2012
2 parents 157e527 + 6b43713 commit 93b1e8b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/markerb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
require "markerb/railtie"

module Markerb
mattr_accessor :processing_options
mattr_accessor :processing_options, :renderer
@@processing_options = []
@@renderer = Redcarpet::Render::HTML

class Handler
def erb_handler
Expand All @@ -14,7 +15,7 @@ def erb_handler
def call(template)
compiled_source = erb_handler.call(template)
if template.formats.include?(:html)
"Redcarpet::Markdown.new(Redcarpet::Render::HTML, *Markerb.processing_options).render(begin;#{compiled_source};end)"
"Redcarpet::Markdown.new(Markerb.renderer, *Markerb.processing_options).render(begin;#{compiled_source};end)"
else
compiled_source
end
Expand Down
26 changes: 26 additions & 0 deletions test/markerb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ def multiple_format_contact(recipient)
end
end

class TestRenderer < Redcarpet::Render::HTML
attr_accessor :show_text
def initialize(render_options = {})
@show_text = render_options.delete(:show_text)
super(render_options)
end

def normal_text(text)
show_text ? "TEST #{text}" : "TEST"
end
end

class MarkerbTest < ActiveSupport::TestCase

test "plain text should be sent as a plain text" do
Expand All @@ -46,4 +58,18 @@ class MarkerbTest < ActiveSupport::TestCase
assert_equal "<p>Dual templates <strong>rocks</strong>!</p>",
email.parts[1].body.encoded.strip
end

test "with a custom renderer" do
Markerb.renderer = TestRenderer
email = Notifier.contact("you@example.com", :html)
assert_equal "text/html", email.mime_type
assert_equal "<p>TEST<strong>TEST</strong>TEST</p>", email.body.encoded.strip
end

test "with a custom renderer and options" do
Markerb.renderer = TestRenderer.new(:show_text => true)
email = Notifier.contact("you@example.com", :html)
assert_equal "text/html", email.mime_type
assert_equal "<p>TEST Dual templates <strong>TEST rocks</strong>TEST !</p>", email.body.encoded.strip
end
end

0 comments on commit 93b1e8b

Please sign in to comment.