email_preview 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,9 @@
1
1
  class EmailPreviewController < ApplicationController
2
2
  unloadable
3
+ layout nil
4
+
3
5
  before_filter :enforce_allowed_environments
4
6
  before_filter :build_email, :only => [:show, :deliver, :details, :preview]
5
- layout nil
6
7
 
7
8
  def deliver
8
9
  @mail.to params[:to]
@@ -23,6 +24,7 @@ class EmailPreviewController < ApplicationController
23
24
  raise "'#{Rails.env}' is not in the supported list of environments from EmailPreview.allowed_environments: #{EmailPreview.allowed_environments.inspect}" unless EmailPreview.allowed_environments.include?(Rails.env)
24
25
  end
25
26
  def build_email
27
+ @fixture = EmailPreview.registry[params[:id]]
26
28
  @mail = EmailPreview.preview params[:id]
27
29
  @parts = @mail.multipart? ? @mail.parts : [@mail]
28
30
  end
@@ -1,3 +1,3 @@
1
1
  module EmailPreview
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/email_preview.rb CHANGED
@@ -1,14 +1,17 @@
1
1
  require 'mail'
2
2
  require 'email_preview/engine' if defined?(Rails)
3
+ require 'active_support/secure_random'
3
4
 
4
5
  module EmailPreview
5
6
  class << self
6
7
  attr_accessor :registry
7
8
  attr_accessor :allowed_environments
8
9
  attr_accessor :transactional
10
+ attr_accessor :logger
11
+ attr_accessor :before_preview_hook
9
12
 
10
13
  def register(description, options={}, &block)
11
- key = description.hash.abs
14
+ key = self.registry.keys.length + 1 #ActiveSupport::SecureRandom.hex
12
15
  options[:key] = key
13
16
  options[:category] ||= 'General'
14
17
  options[:description] ||= description
@@ -19,6 +22,7 @@ module EmailPreview
19
22
  self.registry.values.collect {|f| f[:category] }.uniq
20
23
  end
21
24
  def preview(key)
25
+ EmailPreview.before_preview_hook.call
22
26
  mail = nil
23
27
  ActiveRecord::Base.transaction do
24
28
  mail = self.registry[key.to_i][:block].call
@@ -29,6 +33,26 @@ module EmailPreview
29
33
  def transactional?
30
34
  !!self.transactional
31
35
  end
36
+ def before_preview(&block)
37
+ @before_preview_hook = block
38
+ end
39
+ end
40
+
41
+ class Fixture
42
+ attr_accessor :id, :category, :description, :callback
43
+ def preview
44
+ @mail ||= self.callback.call
45
+ end
46
+ def preview_with_transaction
47
+ return preview_without_transaction unless EmailPreview.transactional?
48
+ mail = nil
49
+ ActiveRecord::Base.transaction do
50
+ mail = preview_without_transaction
51
+ raise ActiveRecord::Rollback, "EmailPreview rollback"
52
+ end
53
+ mail
54
+ end
55
+ # alias_method_chain :preview, :transaction
32
56
  end
33
57
  end
34
58
 
@@ -40,3 +64,9 @@ EmailPreview.allowed_environments = %w{ development test }
40
64
 
41
65
  # default to rollback transactions after previewing email
42
66
  EmailPreview.transactional = true
67
+
68
+ EmailPreview.logger = Logger.new(STDOUT)
69
+
70
+ # default before_preview hook to noop
71
+ EmailPreview.before_preview do
72
+ 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: 19
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 1.1.0
10
+ version: 1.2.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-10 00:00:00 Z
18
+ date: 2011-06-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: mail