aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/attachment_to_html/attachment_to_html.rb1
-rw-r--r--lib/attachment_to_html/template.html.erb13
-rw-r--r--lib/attachment_to_html/view.rb27
-rw-r--r--spec/fixtures/files/attachment_to_html/alternative_template.html.erb2
-rw-r--r--spec/lib/attachment_to_html/view_spec.rb145
5 files changed, 188 insertions, 0 deletions
diff --git a/lib/attachment_to_html/attachment_to_html.rb b/lib/attachment_to_html/attachment_to_html.rb
index 5f63661b4..104dc13e2 100644
--- a/lib/attachment_to_html/attachment_to_html.rb
+++ b/lib/attachment_to_html/attachment_to_html.rb
@@ -1,4 +1,5 @@
require 'html'
+require 'view'
Dir[File.dirname(__FILE__) + '/adapters/*.rb'].each do |file|
require file
diff --git a/lib/attachment_to_html/template.html.erb b/lib/attachment_to_html/template.html.erb
new file mode 100644
index 000000000..9d3068ce2
--- /dev/null
+++ b/lib/attachment_to_html/template.html.erb
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title><%= title %></title>
+</head>
+<body>
+ <div id="<%= wrapper %>">
+ <div id="view-html-content">
+ <%= body %>
+ </div>
+ </div>
+</body>
+</html>
diff --git a/lib/attachment_to_html/view.rb b/lib/attachment_to_html/view.rb
new file mode 100644
index 000000000..5cdd3823b
--- /dev/null
+++ b/lib/attachment_to_html/view.rb
@@ -0,0 +1,27 @@
+module AttachmentToHTML
+ class View < ERB
+
+ def self.template
+ @template || "#{ File.dirname(__FILE__) }/template.html.erb"
+ end
+
+ def self.template=(path)
+ @template = path
+ end
+
+ attr_accessor :title, :body, :template, :wrapper
+
+ def initialize(adapter, opts = {})
+ self.title = adapter.title
+ self.body = adapter.body
+ self.template = opts.fetch(:template, self.class.template)
+ self.wrapper = opts.fetch(:wrapper, 'wrapper')
+ super(File.read(template))
+ end
+
+ def render
+ result(binding)
+ end
+
+ end
+end
diff --git a/spec/fixtures/files/attachment_to_html/alternative_template.html.erb b/spec/fixtures/files/attachment_to_html/alternative_template.html.erb
new file mode 100644
index 000000000..024565d5a
--- /dev/null
+++ b/spec/fixtures/files/attachment_to_html/alternative_template.html.erb
@@ -0,0 +1,2 @@
+<h1><%= @title %></h1>
+<div><%= @body %></div> \ No newline at end of file
diff --git a/spec/lib/attachment_to_html/view_spec.rb b/spec/lib/attachment_to_html/view_spec.rb
new file mode 100644
index 000000000..65eff4cad
--- /dev/null
+++ b/spec/lib/attachment_to_html/view_spec.rb
@@ -0,0 +1,145 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe AttachmentToHTML::View do
+
+ let(:adapter) do
+ OpenStruct.new(
+ :body => '<p>hello</p>',
+ :title => 'An attachment.txt',
+ :success? => true)
+ end
+
+ let(:view) { AttachmentToHTML::View.new(adapter) }
+
+ let(:default_template) do
+ "#{ Rails.root }/lib/attachment_to_html/template.html.erb"
+ end
+
+ describe '.template' do
+
+ after(:each) do
+ AttachmentToHTML::View.template = nil
+ end
+
+ it 'has a default template location' do
+ AttachmentToHTML::View.template.should == default_template
+ end
+
+ end
+
+ describe '.template=' do
+
+ after(:each) do
+ AttachmentToHTML::View.template = nil
+ end
+
+ it 'allows a global template to be set' do
+ template = file_fixture_name('attachment_to_html/alternative_template.html.erb')
+ AttachmentToHTML::View.template = template
+ AttachmentToHTML::View.template.should == template
+ end
+
+ end
+
+ describe :new do
+
+ it 'sets the title on initialization' do
+ view.title.should == adapter.title
+ end
+
+ it 'sets the body on initialization' do
+ view.body.should == adapter.body
+ end
+
+ it 'sets a default template if none is specified' do
+ view.template.should == default_template
+ end
+
+ it 'allows a template to be set through an option' do
+ template = file_fixture_name('attachment_to_html/alternative_template.html.erb')
+ opts = { :template => template }
+ view = AttachmentToHTML::View.new(adapter, opts)
+ view.template.should == template
+ end
+
+ end
+
+ describe :title= do
+
+ it 'allows the title to be set' do
+ view.title = adapter.title
+ view.title.should == adapter.title
+ end
+
+ end
+
+ describe :body= do
+
+ it 'allows the body to be set' do
+ view.body = adapter.body
+ view.body.should == adapter.body
+ end
+
+ end
+
+ describe :template= do
+
+ it 'allows the template to be set' do
+ template = file_fixture_name('attachment_to_html/alternative_template.html.erb')
+ view.template = template
+ view.template.should == template
+ end
+
+ end
+
+ describe :wrapper do
+
+ it 'is set to wrapper by default' do
+ view.wrapper.should == 'wrapper'
+ end
+
+ end
+
+ describe :wrapper= do
+
+ it 'allows the wrapper div to be customised' do
+ view.wrapper = 'wrap'
+ view.wrapper.should == 'wrap'
+ end
+
+ end
+
+ # Need to remove all whitespace to assert equal because
+ # ERB adds additional indentation after ERB tags
+ describe :render do
+
+ it 'renders the contents in to the template' do
+ view.wrapper = 'wrap'
+ expected = <<-HTML
+<!DOCTYPE html>
+<html>
+<head>
+ <title>An attachment.txt</title>
+</head>
+<body>
+ <div id="wrap">
+ <div id="view-html-content">
+ <p>hello</p>
+ </div>
+ </div>
+</body>
+</html>
+ HTML
+
+ view.render.gsub(/\s+/, '').should == expected.gsub(/\s+/, '')
+ end
+
+ it 'allows the dynamic injection of content' do
+ content = %Q(<meta charset="utf-8">)
+ result = view.render { inject_content(:head_suffix) { content } }
+ result.should include(content)
+ end
+
+ end
+
+end