email_preview 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,8 +3,9 @@
3
3
  preview emails within your web browser
4
4
 
5
5
  ## Features
6
- * preview HTML emails from within your web browser
6
+ * preview HTML or plain text emails from within your web browser
7
7
  * emails are reloaded with each view so you can tweak/save/refresh for instant verification
8
+ * integrates perfectly with existing test fixtures
8
9
  * convenient form to send any email preview directly to your *real* inbox
9
10
  * only exposes routes in development mode to prevent leaking into production mode
10
11
 
@@ -14,9 +15,9 @@ gem 'email_preview'
14
15
 
15
16
  ## Usage
16
17
 
17
-
18
18
  ```ruby
19
19
  # config/initializers/email_preview.rb
20
+ # basic example for previewing an email built manually
20
21
  EmailPreview.register 'simple example email' do
21
22
  Mail.new do
22
23
  to 'tom@example.com'
@@ -27,8 +28,8 @@ end
27
28
 
28
29
  EmailPreview.register 'multipart email (html + text)' do
29
30
  Mail.new do
30
- from 'mikel@test.lindsaar.net'
31
- to 'you@test.lindsaar.net'
31
+ from 'bob@example.com'
32
+ to 'jim@foobar.net'
32
33
  subject 'This is a test email'
33
34
 
34
35
  text_part do
@@ -41,8 +42,11 @@ EmailPreview.register 'multipart email (html + text)' do
41
42
  end
42
43
  end
43
44
 
45
+ # Rails ActionMailer Example
46
+ # each execution is wrapped with a transaction and
47
+ # rolled back after completed so there are no side-effects
44
48
  EmailPreview.register 'Rails ActionMailer User activation email' do
45
- u = User.new :email => 'foo@example.com'
49
+ u = User.create :email => 'foo@example.com'
46
50
  UserMailer.activation(u)
47
51
  end
48
52
  ```
@@ -50,13 +54,36 @@ end
50
54
  browse the list of registered emails and preview them in your browser at:
51
55
  http://localhost:3000/email_preview
52
56
 
53
- by default the email_preview feature is only available in development mode. to turn on globally use:
57
+ ![screenshot](https://img.skitch.com/20110608-p2mck7sahpu3h8uit7akq487w2.jpg)
58
+
59
+ ### (optional) Group related emails using the :category option:
60
+
61
+ When you have lots of emails in your app, it's useful to group them into
62
+ related topics.
63
+
64
+ ```ruby
65
+ # config/initializers/email_preview.rb
66
+ EmailPreview.register 'User activation', :category => :user do
67
+ u = User.new :email => 'foo@example.com'
68
+ UserMailer.activation(u)
69
+ end
70
+ EmailPreview.register 'Blog posted', :category => :blog do
71
+ b = Blog.new :title => 'foo'
72
+ BlogMailer.posted(b)
73
+ end
74
+ ```
75
+
76
+ ### (optional) expose to production environment
77
+
78
+ By default the email_preview feature is only available in development mode.
79
+ To make it available to other environments use:
54
80
 
55
81
  ```ruby
56
82
  # config/initializers/email_preview.rb
57
83
  EmailPreview.allowed_environments << 'production'
58
84
  ```
59
85
 
86
+
60
87
  ## Contributing
61
88
 
62
89
  * Fork the project
@@ -11,10 +11,10 @@ class EmailPreviewController < ApplicationController
11
11
  end
12
12
  def preview
13
13
  @part = if request.format == 'html'
14
- @parts.detect {|p| p.content_type && p.content_type.include?('text/html') }
15
- else
16
- @parts.detect {|p| p.content_type && p.content_type.include?('text/plain') }
17
- end
14
+ @parts.detect {|p| p.content_type && p.content_type.include?('text/html') }
15
+ else
16
+ @parts.detect {|p| p.content_type && p.content_type.include?('text/plain') }
17
+ end
18
18
  @part ||= @parts.first
19
19
  render :text => @part.body.to_s
20
20
  end
@@ -1,4 +1,9 @@
1
1
  <style>
2
+ h2, ul, li { margin:0; padding:0; }
3
+
4
+ h2 {
5
+ margin: 5px;
6
+ }
2
7
  .headers {
3
8
  color: #333;
4
9
  }
@@ -27,6 +32,13 @@
27
32
  .deliver_form input {
28
33
  width: 250px;
29
34
  }
35
+
36
+ ul {
37
+ list-style-type: none;
38
+ }
39
+ li {
40
+ margin-left: 10px
41
+ }
30
42
  </style>
31
43
  <%= form_tag deliver_email_preview_path(params[:id]), :class => 'deliver_form' do %>
32
44
  <%= text_field_tag :to, '', :placeholder => 'Send this email to me@yourcompany.com' %>
@@ -1,5 +1,22 @@
1
- <ul>
2
- <% EmailPreview.emails.each_index do |index| %>
3
- <li><%= link_to EmailPreview.emails[index][:description], email_preview_path(index), :target => 'show' %>
4
- <% end%>
5
- </ul>
1
+ <style type='text/css'>
2
+ ul, li, h3 { margin: 0; padding: 0; }
3
+
4
+ ul {
5
+ list-style-type: none;
6
+ }
7
+ li {
8
+ margin-left: 10px;
9
+ }
10
+ h3 {
11
+ margin: 5px;
12
+ }
13
+ </style>
14
+ <% EmailPreview.categories.each do |category| %>
15
+ <h3><%= category %></h3>
16
+ <ul>
17
+ <% EmailPreview.registry.values.sort_by {|o| o[:description]}.each do |options| %>
18
+ <% next unless options[:category] == category %>
19
+ <li><%= link_to options[:description], email_preview_path(options[:key]), :target => 'show' %>
20
+ <% end%>
21
+ </ul>
22
+ <% end %>
@@ -3,7 +3,7 @@
3
3
  <head>
4
4
  <title>Email Preview</title>
5
5
  </head>
6
- <frameset rows="300px,*">
6
+ <frameset rows="250px,*">
7
7
  <frame src="<%= details_email_preview_path(params[:id]) %>" name="details" />
8
8
  <frame name="preview" />
9
9
  </frameset>
@@ -1,3 +1,3 @@
1
1
  module EmailPreview
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/email_preview.rb CHANGED
@@ -3,17 +3,25 @@ require 'email_preview/engine' if defined?(Rails)
3
3
 
4
4
  module EmailPreview
5
5
  class << self
6
- attr_accessor :emails
6
+ attr_accessor :registry
7
7
  attr_accessor :allowed_environments
8
8
  attr_accessor :transactional
9
9
 
10
- def register(description, &block)
11
- self.emails << {:description => description, :block => block }
10
+ def register(description, options={}, &block)
11
+ key = description.hash.abs
12
+ options[:key] = key
13
+ options[:category] ||= 'General'
14
+ options[:description] ||= description
15
+ options[:block] ||= block
16
+ self.registry[key] = options
12
17
  end
13
- def preview(index)
18
+ def categories
19
+ self.registry.values.collect {|f| f[:category] }.uniq
20
+ end
21
+ def preview(key)
14
22
  mail = nil
15
23
  ActiveRecord::Base.transaction do
16
- mail = self.emails[index.to_i][:block].call
24
+ mail = self.registry[key.to_i][:block].call
17
25
  raise ActiveRecord::Rollback, "EmailPreview rollback" if EmailPreview.transactional?
18
26
  end
19
27
  mail
@@ -24,8 +32,8 @@ module EmailPreview
24
32
  end
25
33
  end
26
34
 
27
- # initialize to empty array
28
- EmailPreview.emails = []
35
+ # initialize registry
36
+ EmailPreview.registry = {}
29
37
 
30
38
  # default to only run in development and test environment
31
39
  EmailPreview.allowed_environments = %w{ development test }
@@ -13,7 +13,7 @@ class TestEmailPreview < Test::Unit::TestCase
13
13
  end
14
14
  end
15
15
  should "have one entry in the configuration" do
16
- assert_equal 1, EmailPreview.emails.length
16
+ assert_equal 1, EmailPreview.registry.keys.length
17
17
  end
18
18
  end
19
19
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_preview
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ - 0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Sonnek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-08 00:00:00 Z
18
+ date: 2011-06-10 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: mail