action_mailer_cache_delivery 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ ## Action Mailer Cache Delivery
2
+
3
+ [![Build Status](https://secure.travis-ci.org/p0deje/action_mailer_cache_delivery.png)](http://travis-ci.org/p0deje/action_mailer_cache_delivery)
4
+
5
+ Enhances ActionMailer to support the `:cache` delivery method, which behaves like `:test`, except that the deliveries are marshalled to a temporary cache file, thus making them available to other processes.
6
+
7
+ You'll want to use this gem if you're testing with Selenium (or any other tool which distinct processes).
8
+
9
+ ## Installation
10
+
11
+ Add to Gemfile
12
+
13
+ ```ruby
14
+ gem 'action_mailer_cache_delivery'
15
+ ```
16
+
17
+ Now run `bundle install`
18
+
19
+ ## Usage
20
+
21
+ Change delivery method in your `config/environments/test.rb`
22
+
23
+ ```ruby
24
+ config.action_mailer.delivery_method = :cache
25
+ ```
26
+
27
+ You can optionally specify location for cache file.
28
+
29
+ ```ruby
30
+ config.action_mailer.cache_settings = { :location => "#{Rails.root}/tmp/mail.cache" }
31
+ ```
32
+
33
+ To access the cached deliveries in another process, just do
34
+
35
+ ```ruby
36
+ ActionMailer::Base.cached_deliveries
37
+ ```
38
+
39
+ ## Use with parallel_tests
40
+
41
+ If you use [parallel_tests](https://github.com/grosser/parallel_tests "parallel_tests") to run your tests in parallel, you may get unexpected errors like `EOFError`. If so, make sure cache files differ for processes. Change you `config/environment/test.rb`
42
+
43
+ ```ruby
44
+ config.action_mailer.cache_settings = { :location => "#{Rails.root}/tmp/cache/action_mailer_cache_delivery#{ENV['TEST_ENV_NUMBER']}.cache" }
45
+ ```
46
+
47
+ ## Contributors
48
+
49
+ I tried to list there everyone who forked original version on GitHub. If you're not here, just send pull request.
50
+
51
+ * ngty (Ng Tze Yang)
52
+ * robinsp (Robin Spainhour)
53
+ * thibaudgg (Thibaud Guillaume-Gentil)
54
+ * liangzan (Wong Liang Zan)
55
+ * railssignals (Kurt Kowar)
56
+ * esdras (Esdras Mayrink)
57
+ * nicolasochem (Nicolas Ochem)
58
+ * reactualize
59
+ * ragaskar (Rajan Agaskar)
60
+ * marhan (Markus Hanses)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new :spec
5
+
6
+ task default: :spec
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
2
+ require 'action_mailer_cache_delivery/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'action_mailer_cache_delivery'
6
+ s.homepage = 'https://github.com/p0deje/action_mailer_cache_delivery'
7
+ s.version = ActionMailerCacheDelivery::VERSION
8
+
9
+ s.authors = 'Alex Rodionov'
10
+ s.email = 'p0deje@gmail.com'
11
+
12
+ s.summary = 'Cache delivery method for ActionMailer'
13
+ s.description = 'Cache delivery method for ActionMailer for testing emails with Selenium'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+
19
+ s.add_dependency 'actionmailer', '~> 3.0'
20
+
21
+ s.add_development_dependency 'rspec' , '~> 2.8'
22
+ s.add_development_dependency 'rake' , '~> 0.9'
23
+ end
@@ -0,0 +1,16 @@
1
+ require 'action_mailer_cache_delivery/version'
2
+ require 'action_mailer_cache_delivery/mail/cache_delivery'
3
+ require 'action_mailer_cache_delivery/action_mailer/base'
4
+ require 'action_mailer_cache_delivery/railtie' if defined?(Rails)
5
+
6
+ module ActionMailerCacheDelivery
7
+ class << self
8
+
9
+ def install
10
+ default_path = 'cache/action_mailer_cache_deliveries.cache'
11
+ ActionMailer::Base.add_delivery_method :cache, Mail::CacheDelivery,
12
+ location: defined?(Rails) ? "#{Rails.root}/tmp/#{default_path}" : "#{Dir.tmpdir}/#{default_path}"
13
+ end
14
+
15
+ end # << self
16
+ end # ActionMailerCacheDelivery
@@ -0,0 +1,31 @@
1
+ module ActionMailer
2
+ class Base
3
+ class << self
4
+
5
+ #
6
+ # Returns an array of delivered mails.
7
+ #
8
+ # @return [Array] array of mails (each mail is an instance of Mail.)
9
+ #
10
+ def cached_deliveries
11
+ File.open(cache_settings[:location], 'r') do |file|
12
+ Marshal.load(file)
13
+ end
14
+ end
15
+
16
+ #
17
+ # Clears delivered mails.
18
+ #
19
+ # It also cleans ActionMailer::Base.deliveries
20
+ #
21
+ def clear_cache
22
+ deliveries.clear
23
+
24
+ File.open(cache_settings[:location], 'w') do |file|
25
+ Marshal.dump(deliveries, file)
26
+ end
27
+ end
28
+
29
+ end # << self
30
+ end # Base
31
+ end # ActionMailer
@@ -0,0 +1,49 @@
1
+ module Mail
2
+ #
3
+ # Performs deliveries to temporary cache file, so mails can accessed from
4
+ # other processes.
5
+ #
6
+ # Default location of files is:
7
+ # - "tmp/cache/action_mailer_cache_deliveries.cache" if you use Rails
8
+ # - "/tmp/cache/action_mailer_cache_deliveries.cache" if you don't use Rails
9
+ #
10
+ # However, you can overwrite location in configuration:
11
+ #
12
+ # @example
13
+ # config.action_mailer.cache_settings = { location: "custom/path" }
14
+ #
15
+ class CacheDelivery
16
+
17
+ # @attr [Hash] settings Settings for CacheDelivery
18
+ attr_accessor :settings
19
+
20
+ # @api private
21
+ def initialize(settings)
22
+ @settings = settings
23
+
24
+ # create the cache directory if it doesn't exist
25
+ cache_dir = File.dirname(@settings[:location])
26
+ FileUtils.mkdir_p(cache_dir) unless File.directory?(cache_dir)
27
+ end
28
+
29
+ # @api private
30
+ def deliver!(mail)
31
+ # write empty array to cache file if doesn't exist
32
+ unless File.exists?(@settings[:location])
33
+ File.open(@settings[:location], 'w') do |file|
34
+ Marshal.dump([], file)
35
+ end
36
+ end
37
+
38
+ # get delivered mails
39
+ mails = ActionMailer::Base.cached_deliveries
40
+ # append new one
41
+ mails << mail
42
+ # write all emails to cache file
43
+ File.open(@settings[:location], 'w') do |file|
44
+ Marshal.dump(mails, file)
45
+ end
46
+ end
47
+
48
+ end # CacheDelivery
49
+ end # Mail
@@ -0,0 +1,16 @@
1
+ module ActionMailerCacheDelivery
2
+ class Railtie < Rails::Railtie
3
+
4
+ #
5
+ # Make settings available before configuration:
6
+ #
7
+ # @example
8
+ # config.action_mailer.delivery_method = :cache
9
+ # config.action_mailer.cache_settings = { location: "#{Rails.root}/tmp/mail.cache" }
10
+ #
11
+ config.before_configuration do
12
+ ActionMailerCacheDelivery.install
13
+ end
14
+
15
+ end # Railtie
16
+ end # ActionMailerCacheDelivery
@@ -0,0 +1,5 @@
1
+ module ActionMailerCacheDelivery
2
+
3
+ VERSION = '0.3.2'
4
+
5
+ end # ActionMailerCacheDelivery
@@ -0,0 +1,76 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe ActionMailerCacheDelivery do
4
+
5
+ context 'with Mailer' do
6
+ let(:mail) do
7
+ Mailer.test_mail
8
+ end
9
+
10
+ before(:each) do
11
+ ActionMailerCacheDelivery.install
12
+ ActionMailer::Base.delivery_method = :cache
13
+ end
14
+
15
+ describe 'clear_cache' do
16
+ it 'should clear cache file' do
17
+ mail.deliver
18
+ ActionMailer::Base.clear_cache
19
+ File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
20
+ Marshal.load(file)
21
+ end.should == []
22
+ end
23
+
24
+ it 'should clear ActionMailer::Base.deliveries' do
25
+ mail.deliver
26
+ ActionMailer::Base.clear_cache
27
+ ActionMailer::Base.deliveries.should == []
28
+ end
29
+ end
30
+
31
+ describe 'cached_deliveries' do
32
+ before(:each) do
33
+ ActionMailer::Base.clear_cache
34
+ end
35
+
36
+ it 'should return array' do
37
+ mail.deliver
38
+ ActionMailer::Base.cached_deliveries.should be_an(Array)
39
+ end
40
+
41
+ it 'should return all the sent emails' do
42
+ 5.times do
43
+ mail.deliver
44
+ end
45
+ ActionMailer::Base.cached_deliveries.length.should == 5
46
+ end
47
+ end
48
+ end
49
+
50
+ context 'without Mailer' do
51
+ describe 'extend ActionMailer' do
52
+ it 'should add cached_deliveries method to ActionMailer' do
53
+ ActionMailerCacheDelivery.install
54
+ ActionMailer::Base.should respond_to(:cached_deliveries)
55
+ end
56
+
57
+ it 'should add clear_cache method to ActionMailer' do
58
+ ActionMailerCacheDelivery.install
59
+ ActionMailer::Base.should respond_to(:clear_cache)
60
+ end
61
+ end
62
+
63
+ describe 'install' do
64
+ it 'should add :cache delivery method to ActionMailer' do
65
+ ActionMailerCacheDelivery.install
66
+ ActionMailer::Base.delivery_methods.should include(:cache)
67
+ end
68
+
69
+ it 'should set cache path to /tmp when Rails is not defined' do
70
+ ActionMailerCacheDelivery.install
71
+ ActionMailer::Base.delivery_method = :cache
72
+ ActionMailer::Base.cache_settings[:location].should == '/tmp/cache/action_mailer_cache_deliveries.cache'
73
+ end
74
+ end
75
+ end
76
+ end # ActionMailerCacheDelivery
@@ -0,0 +1,39 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Mail::CacheDelivery do
4
+ let(:mail) do
5
+ Mailer.test_mail
6
+ end
7
+
8
+ before(:each) do
9
+ ActionMailerCacheDelivery.install
10
+ ActionMailer::Base.delivery_method = :cache
11
+ ActionMailer::Base.clear_cache
12
+ end
13
+
14
+ describe 'settings' do
15
+ it 'should allow setting custom location' do
16
+ ActionMailer::Base.cache_settings = { location: '/tmp/mail.cache' }
17
+ mail.deliver
18
+ File.exists?('/tmp/mail.cache').should be_true
19
+ end
20
+ end
21
+
22
+ describe 'deliver' do
23
+ it 'should write mail to cache file' do
24
+ mail.deliver
25
+ File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
26
+ Marshal.load(file)
27
+ end.should == [mail]
28
+ end
29
+
30
+ it 'should append mail to cache file' do
31
+ 5.times do
32
+ mail.deliver
33
+ end
34
+ File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
35
+ Marshal.load(file)
36
+ end.length.should == 5
37
+ end
38
+ end
39
+ end # Mail::CachedDelivery
@@ -0,0 +1,11 @@
1
+ require 'action_mailer'
2
+ require 'action_mailer_cache_delivery'
3
+
4
+ #
5
+ # Simple mailer.
6
+ #
7
+ class Mailer < ActionMailer::Base
8
+ def test_mail
9
+ mail(from: 'from@test.com', to: 'to@test.com', subject: 'Test email')
10
+ end
11
+ end # Mailer
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_mailer_cache_delivery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Rodionov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionmailer
16
+ requirement: &12576240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *12576240
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &12575740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.8'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *12575740
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &12575280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '0.9'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *12575280
47
+ description: Cache delivery method for ActionMailer for testing emails with Selenium
48
+ email: p0deje@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .travis.yml
54
+ - Gemfile
55
+ - README.md
56
+ - Rakefile
57
+ - action_mailer_cache_delivery.gemspec
58
+ - lib/action_mailer_cache_delivery.rb
59
+ - lib/action_mailer_cache_delivery/action_mailer/base.rb
60
+ - lib/action_mailer_cache_delivery/mail/cache_delivery.rb
61
+ - lib/action_mailer_cache_delivery/railtie.rb
62
+ - lib/action_mailer_cache_delivery/version.rb
63
+ - spec/action_mailer_cache_delivery_spec.rb
64
+ - spec/cache_delivery_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: https://github.com/p0deje/action_mailer_cache_delivery
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.16
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Cache delivery method for ActionMailer
90
+ test_files:
91
+ - spec/action_mailer_cache_delivery_spec.rb
92
+ - spec/cache_delivery_spec.rb
93
+ - spec/spec_helper.rb
94
+ has_rdoc: