grimen-delayed_job_mailer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Anderson Dias
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,56 @@
1
+ DelayedJobMailer
2
+ ==============
3
+
4
+ This plugin provides a module which - when included into an ActionMailer subclass - pushes all emails that would
5
+ normally be delivered synchronously into a queue for asynchronous processing. For queuing it relies on the delayed_job
6
+ plugin (http://github.com/tobi/delayed_job).
7
+
8
+ How it works
9
+ ============
10
+
11
+ The plugin provides a module Delayed::Mailer. To make a mailer use a queue simply include it into the class like this:
12
+
13
+ class MyMailer < ActionMailer::Base
14
+ include Delayed::Mailer
15
+ end
16
+
17
+ From now on all MyMailer.deliver_whatever_email calls create an entry delayed_job queue.
18
+ If you still want to deliver mail sycnhronously add a bang to the method call: MyMailer.deliver_whatever_email!
19
+
20
+ To set asynchronous mailing as project default, you need to create an initializer file as follows:
21
+
22
+ # config/initializers/delayed_mailer.rb
23
+ class ActionMailer::Base
24
+ include Delayed::Mailer
25
+ end
26
+
27
+ Installation
28
+ ============
29
+
30
+ script/plugin install git://github.com/andersondias/workling_mailer.git
31
+
32
+ Configuration
33
+ =============
34
+
35
+ Delayed e-mails is an awesome thing in production environments, but for e-mail specs/tests in testing environments it can be a mess causing specs/tests to fail because the e-mail haven't been sent directly. Therefore you can configure what environments that should be excluded like so:
36
+
37
+ # config/initializers/delayed_mailer.rb
38
+
39
+ Delayed::Mailer.excluded_environments = [:test, :cucumber] # etc.
40
+
41
+ ...
42
+
43
+ Credits
44
+ =======
45
+
46
+ This plugin is a delayed job plugin based on langalex workling_mailer
47
+ plugin that can be found at http://github.com/langalex/workling_mailer
48
+
49
+ Contact
50
+ =======
51
+
52
+ Copyright (c) 2009 Anderson Dias, released under the MIT license
53
+
54
+ Email: andersondaraujo[at]gmail.com
55
+ Twitter: extendsmymind
56
+ Blog: http://extendsmymind.wordpress.com
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ # coding: utf-8
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+
5
+ gem 'rspec-rails', '>= 1.0.0'
6
+ require 'spec/rake/spectask'
7
+
8
+ NAME = "delayed_job_mailer"
9
+ SUMMARY = %Q{Send emails asynchronously using delayed_job.}
10
+ HOMEPAGE = "http://github.com/andersondias/#{NAME}"
11
+ AUTHOR = "Anderson Dias"
12
+ EMAIL = "andersondaraujo@gmail.com"
13
+ SUPPORT_FILES = %w(README)
14
+
15
+ begin
16
+ gem 'technicalpickles-jeweler', '>= 1.2.1'
17
+ require 'jeweler'
18
+ Jeweler::Tasks.new do |gem|
19
+ gem.name = NAME
20
+ gem.summary = SUMMARY
21
+ gem.description = SUMMARY
22
+ gem.homepage = HOMEPAGE
23
+ gem.author = AUTHOR
24
+ gem.email = EMAIL
25
+
26
+ gem.require_paths = %w{lib}
27
+ gem.files = SUPPORT_FILES << %w(MIT-LICENSE Rakefile) << Dir.glob(File.join('{generators,lib,test,rails}', '**', '*'))
28
+ gem.executables = %w()
29
+ gem.extra_rdoc_files = SUPPORT_FILES
30
+ end
31
+ rescue LoadError
32
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
33
+ end
34
+
35
+ desc %Q{Default: Run specs for "#{NAME}".}
36
+ task :default => :spec
37
+
38
+ SPEC_FILES = Rake::FileList[File.join('spec', '**', '*_spec.rb')]
39
+
40
+ desc %Q{Run specs for "#{NAME}".}
41
+ Spec::Rake::SpecTask.new do |t|
42
+ t.spec_files = SPEC_FILES
43
+ t.spec_opts = ['-c']
44
+ end
45
+
46
+ desc %Q{Generate code coverage for "#{NAME}".}
47
+ Spec::Rake::SpecTask.new(:coverage) do |t|
48
+ t.spec_files = SPEC_FILES
49
+ t.rcov = true
50
+ t.rcov_opts = ['--exclude', 'spec,/var/lib/gems']
51
+ end
52
+
53
+ desc %Q{Generate documentation for "#{NAME}".}
54
+ Rake::RDocTask.new(:rdoc) do |rdoc|
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = NAME
57
+ rdoc.options << '--line-numbers' << '--inline-source' << '--charset=UTF-8'
58
+ rdoc.rdoc_files.include(SUPPORT_FILES)
59
+ rdoc.rdoc_files.include(File.join('lib', '**', '*.rb'))
60
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+
3
+ module Delayed
4
+ module Mailer
5
+
6
+ def self.included(base)
7
+ base.class_eval do
8
+ class << self
9
+ def method_missing(method_symbol, *params)
10
+ if ::Delayed::Mailer.excluded_environments &&
11
+ ::Delayed::Mailer.excluded_environments.include?(::RAILS_ENV.to_sym)
12
+ return super(method_symbol, *params)
13
+ end
14
+
15
+ case method_symbol.id2name
16
+ when /^deliver_([_a-z]\w*)\!/ then super(method_symbol, *params)
17
+ when /^deliver_([_a-z]\w*)/ then self.send_later("#{method_symbol}!", *params)
18
+ else super(method_symbol, *params)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.excluded_environments=(*environments)
25
+ @@excluded_environments = environments && environments.flatten.collect! { |env| env.to_sym }
26
+ end
27
+
28
+ def self.excluded_environments
29
+ @@excluded_environments ||= []
30
+ end
31
+ end
32
+
33
+ end
34
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # coding: utf-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__), *%w(lib .. delayed_job_mailer))
@@ -0,0 +1,69 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ require 'rubygems'
5
+ gem 'actionmailer', '>= 1.0.0'
6
+ require 'actionmailer'
7
+
8
+ ActionMailer::Base.delivery_method = :test
9
+
10
+ class AsynchTestMailer < ActionMailer::Base
11
+ include Delayed::Mailer
12
+
13
+ def test_mail(from, to)
14
+ @subject = 'subject'
15
+ @body = 'mail body'
16
+ @recipients = to
17
+ @from = from
18
+ @sent_on = Time.now
19
+ @headers = {}
20
+ end
21
+ end
22
+
23
+ describe AsynchTestMailer do
24
+ before do
25
+ # We need a default environment...
26
+ Object.const_set 'RAILS_ENV', 'test' unless defined?(::RAILS_ENV)
27
+ end
28
+
29
+ describe 'deliver_test_mail' do
30
+ before(:each) do
31
+ @emails = ActionMailer::Base.deliveries
32
+ @emails.clear
33
+ @params = 'noreply@autoki.de', 'joe@doe.com'
34
+ AsynchTestMailer.stub(:send_later)
35
+ end
36
+
37
+ it 'should not deliver the email at this moment' do
38
+ AsynchTestMailer.deliver_test_mail *@params
39
+ @emails.size.should == 0
40
+ end
41
+
42
+ it 'should send deliver action to delayed job list' do
43
+ AsynchTestMailer.should_receive(:send_later).with('deliver_test_mail!', *@params)
44
+ AsynchTestMailer.deliver_test_mail *@params
45
+ end
46
+
47
+ it 'should not send deliver action to delayed job list for environments where delayed job mailer is disabled' do
48
+ excluded_environments = [:cucumber, :foo, 'bar']
49
+ ::Delayed::Mailer.excluded_environments = excluded_environments
50
+
51
+ excluded_environments.each do |env|
52
+ Object.send :remove_const, 'RAILS_ENV'
53
+ Object.const_set 'RAILS_ENV', env.to_s
54
+
55
+ AsynchTestMailer.should_not_receive(:send_later).with('deliver_test_mail!', *@params)
56
+ AsynchTestMailer.deliver_test_mail *@params
57
+ end
58
+ end
59
+ end
60
+
61
+ describe 'deliver_test_mail!' do
62
+ it 'should deliver the mail' do
63
+ emails = ActionMailer::Base.deliveries
64
+ emails.clear
65
+ AsynchTestMailer.deliver_test_mail! 'noreply@autoki.de', 'joe@doe.com'
66
+ emails.size.should == 1
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ $: << File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'rubygems'
5
+ require 'spec'
6
+ require 'delayed_job_mailer'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grimen-delayed_job_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anderson Dias
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Send emails asynchronously using delayed_job.
17
+ email: andersondaraujo@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - MIT-LICENSE
24
+ - README
25
+ - Rakefile
26
+ - lib/delayed_job_mailer.rb
27
+ - rails/init.rb
28
+ files:
29
+ - MIT-LICENSE
30
+ - README
31
+ - Rakefile
32
+ - lib/delayed_job_mailer.rb
33
+ - rails/init.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/andersondias/delayed_job_mailer
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: Send emails asynchronously using delayed_job.
60
+ test_files:
61
+ - spec/delayed_job_mailer_spec.rb
62
+ - spec/spec_helper.rb