aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/attachment_to_html/attachment_to_html.rb13
-rw-r--r--spec/lib/attachment_to_html/attachment_to_html_spec.rb30
2 files changed, 31 insertions, 12 deletions
diff --git a/lib/attachment_to_html/attachment_to_html.rb b/lib/attachment_to_html/attachment_to_html.rb
index 104dc13e2..8675329c4 100644
--- a/lib/attachment_to_html/attachment_to_html.rb
+++ b/lib/attachment_to_html/attachment_to_html.rb
@@ -10,14 +10,15 @@ module AttachmentToHTML
def to_html(attachment, opts = {})
adapter = adapter_for(attachment).new(attachment, opts)
- html = HTML.new(adapter)
- if html.success?
- html
- else
- fallback = fallback_adapter_for(attachment).new(attachment, opts)
- HTML.new(fallback)
+ unless adapter.success?
+ adapter = fallback_adapter_for(attachment).new(attachment, opts)
end
+
+ view = View.new(adapter)
+ view.wrapper = 'wrapper_google_embed' if adapter.is_a?(Adapters::GoogleDocsViewer)
+
+ view.render
end
private
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 f7df06f87..41e1d67e5 100644
--- a/spec/lib/attachment_to_html/attachment_to_html_spec.rb
+++ b/spec/lib/attachment_to_html/attachment_to_html_spec.rb
@@ -12,11 +12,12 @@ describe AttachmentToHTML do
to_html(attachment)
end
- it 'returns the results in a HTML class' do
- expected = AttachmentToHTML::Adapters::Text.new(attachment).to_html
- to_html(attachment).should be_instance_of(AttachmentToHTML::HTML)
+ it 'renders the attachment as html' do
+ adapter = AttachmentToHTML::Adapters::Text.new(attachment)
+ expected = AttachmentToHTML::View.new(adapter).render
+ to_html(attachment).should == expected
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
@@ -25,9 +26,9 @@ describe AttachmentToHTML do
it 'converts an attachment that has an adapter, fails to convert, but has a google viewer' do
attachment = FactoryGirl.build(:pdf_attachment)
- AttachmentToHTML::HTML.any_instance.stub(:success?).and_return(false)
+ AttachmentToHTML::Adapters::PDF.any_instance.stub(:success?).and_return(false)
AttachmentToHTML::Adapters::PDF.should_receive(:new).with(attachment, {}).and_call_original
- AttachmentToHTML::Adapters::GoogleDocsViewer.should_receive(:new).with(attachment, {})
+ AttachmentToHTML::Adapters::GoogleDocsViewer.should_receive(:new).with(attachment, {}).and_call_original
to_html(attachment)
end
@@ -43,6 +44,23 @@ describe AttachmentToHTML do
to_html(attachment)
end
+ describe 'when wrapping the content' do
+
+ it 'uses a the default wrapper' do
+ attachment = FactoryGirl.build(:pdf_attachment)
+ to_html(attachment).should include(%Q(<div id="wrapper">))
+ end
+
+ it 'uses a custom wrapper for GoogleDocsViewer attachments' do
+ attachment = FactoryGirl.build(:pdf_attachment)
+ # TODO: Add a document that will always render in a
+ # GoogleDocsViewer for testing
+ AttachmentToHTML::Adapters::PDF.any_instance.stub(:success?).and_return(false)
+ to_html(attachment).should include(%Q(<div id="wrapper_google_embed">))
+ end
+
+ end
+
end
end