aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/attachment_to_html/attachment_to_html.rb6
-rw-r--r--lib/attachment_to_html/template.html.erb3
-rw-r--r--lib/attachment_to_html/view.rb14
-rw-r--r--spec/lib/attachment_to_html/attachment_to_html_spec.rb5
4 files changed, 26 insertions, 2 deletions
diff --git a/lib/attachment_to_html/attachment_to_html.rb b/lib/attachment_to_html/attachment_to_html.rb
index ca899221b..2f7c08264 100644
--- a/lib/attachment_to_html/attachment_to_html.rb
+++ b/lib/attachment_to_html/attachment_to_html.rb
@@ -17,7 +17,11 @@ module AttachmentToHTML
view = View.new(adapter)
view.wrapper = 'wrapper_google_embed' if adapter.is_a?(Adapters::GoogleDocsViewer)
- view.render
+ view.render do
+ opts.fetch(:content_for, []).each do |k,v|
+ inject_content(k) { v }
+ end
+ end
end
private
diff --git a/lib/attachment_to_html/template.html.erb b/lib/attachment_to_html/template.html.erb
index 9d3068ce2..38286a5f9 100644
--- a/lib/attachment_to_html/template.html.erb
+++ b/lib/attachment_to_html/template.html.erb
@@ -2,12 +2,15 @@
<html>
<head>
<title><%= title %></title>
+ <%= content_for(:head_suffix) %>
</head>
<body>
+ <%= content_for(:body_prefix) %>
<div id="<%= wrapper %>">
<div id="view-html-content">
<%= body %>
</div>
</div>
+ <%= content_for(:body_suffix) %>
</body>
</html>
diff --git a/lib/attachment_to_html/view.rb b/lib/attachment_to_html/view.rb
index 5cdd3823b..e6991d44e 100644
--- a/lib/attachment_to_html/view.rb
+++ b/lib/attachment_to_html/view.rb
@@ -19,9 +19,21 @@ module AttachmentToHTML
super(File.read(template))
end
- def render
+ def render(&block)
+ instance_eval(&block) if block_given?
result(binding)
end
+ def content_for(area)
+ send(area) if respond_to?(area)
+ end
+
+ private
+
+ def inject_content(area, &block)
+ instance_variable_set("@#{ area }".to_sym, block.call)
+ self.class.send(:attr_accessor, area)
+ end
+
end
end
diff --git a/spec/lib/attachment_to_html/attachment_to_html_spec.rb b/spec/lib/attachment_to_html/attachment_to_html_spec.rb
index 41e1d67e5..1cf7debb7 100644
--- a/spec/lib/attachment_to_html/attachment_to_html_spec.rb
+++ b/spec/lib/attachment_to_html/attachment_to_html_spec.rb
@@ -18,6 +18,11 @@ describe AttachmentToHTML do
to_html(attachment).should == expected
end
+ it 'passes content injections options when rendering the result' do
+ html = to_html(attachment, :content_for => { :body_prefix => '<p>prefix</p>' })
+ html.should include('<p>prefix</p>')
+ end
+
it 'accepts a hash of options to pass to the adapter' do
options = { :wrapper => 'wrap' }
AttachmentToHTML::Adapters::Text.should_receive(:new).with(attachment, options).and_call_original