Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial import

  • Loading branch information...
commit f1b1f4af2352f28d1def30cc63ba1ae1be208716 0 parents
@zapnap authored
3  .document
@@ -0,0 +1,3 @@
+README.rdoc
+lib/**/*.rb
+LICENSE
21 .gitignore
@@ -0,0 +1,21 @@
+## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+## PROJECT::SPECIFIC
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Nick Plante
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58 README.rdoc
@@ -0,0 +1,58 @@
+= ResqueMailer
+
+A gem plugin which allows messages prepared by ActionMailer to be delivered asynchronously.
+Assumes that you're using Resque (http://github.com/defunkt/resque) for your background jobs.
+
+== Usage
+
+Include Resque::Mailer in your ActionMailer subclass(es) like this:
+
+class MyMailer < ActionMailer::Base
+ include Resque::Mailer
+end
+
+Or if you want to always use asynchronous delivery by default, create an initializer in your
+Rails project or piggyback on your load_resque.rb initializer:
+
+class ActionMailer::Base
+ include Resque::Mailer
+end
+
+Now, when MyMailer.deliver_subject_email is called, an entry will be created in the job queue.
+Your Resque workers will be able to deliver this for you; the queue we're using is imaginatively
+named 'mailer'. Just make sure your workers know about it and are loading your environment:
+
+QUEUE=mailer rake environment resque:work
+
+Note that you can still have mail delivered synchronously by using the bang method variant:
+MyMailer.deliver_subject_email!
+
+== Installation
+
+Install it as a plugin or as a gem plugin.
+
+script/plugin install git://github.com/zapnap/resque_mailer.git
+
+config.gem 'resque_mailer'
+
+== Testing
+
+You don't want to be sending actual emails in the test environment, so you can configure the
+environments that should be excluded like so:
+
+# config/initializers/resque_mailer.rb
+Resque::Mailer.excluded_environments = [:test, :cucumber]
+
+== Note on Patches / Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Credits
+
+This work is essentially a forked version of delayed_job_mailer http://github.com/andersondias/delayed_job_mailer) by Anderson Dias
+(which in turn was inspired by Alexander Lang's workling_mailer). Enhanced and modified to work with Resque by Nick Plante.
47 Rakefile
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "resque_mailer"
+ gem.summary = %Q{Rails plugin for sending asynchronous email with ActionMailer and Resque}
+ gem.description = %Q{Rails plugin for sendings asynchronous email with ActionMailer and Resque}
+ gem.email = "nap@zerosum.org"
+ gem.homepage = "http://github.com/zapnap/resque_mailer"
+ gem.authors = ["Nick Plante"]
+ gem.add_development_dependency "rspec", ">= 1.2.9"
+ gem.add_development_dependency "resque", ">= 1.2.3"
+ gem.add_development_dependency "actionmailer", ">= 2.2.2"
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+Spec::Rake::SpecTask.new(:rcov) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
+end
+
+task :spec => :check_dependencies
+
+task :default => :spec
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "resque_mailer #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
41 lib/resque_mailer.rb
@@ -0,0 +1,41 @@
+module Resque
+ module Mailer
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def method_missing(method_symbol, *params)
+
+ if ::Resque::Mailer.excluded_environments &&
+ ::Resque::Mailer.excluded_environments.include?(::RAILS_ENV.to_sym)
+ return super(method_symbol, *params)
+ end
+
+ case method_symbol.id2name
+ when /^deliver_([_a-z]\w*)\!/ then super(method_symbol, *params)
+ when /^deliver_([_a-z]\w*)/ then ::Resque.enqueue(self, "#{method_symbol}!", *params)
+ else super(method_symbol, *params)
+ end
+ end
+
+ def queue
+ :mailer
+ end
+
+ def perform(cmd, *args)
+ send(cmd, *args)
+ end
+ end
+
+ def self.excluded_environments=(*environments)
+ @@excluded_environments = environments && environments.flatten.collect! { |env| env.to_sym }
+ end
+
+ def self.excluded_environments
+ @@excluded_environments ||= []
+ end
+
+ end
+end
1  rails/init.rb
@@ -0,0 +1 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '../lib', 'resque_mailer'))
76 spec/resque_mailer_spec.rb
@@ -0,0 +1,76 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+require 'rubygems'
+gem 'actionmailer', '>= 2.2.2'
+require 'action_mailer'
+
+ActionMailer::Base.delivery_method = :test
+
+class AsynchTestMailer < ActionMailer::Base
+ include Resque::Mailer
+
+ def test_mail(from, to)
+ @subject = 'subject'
+ @body = 'mail body'
+ @recipients = to
+ @from = from
+ @sent_on = Time.now
+ @headers = {}
+ end
+end
+
+describe AsynchTestMailer do
+ before do
+ Object.const_set 'RAILS_ENV', 'test' unless defined?(::RAILS_ENV)
+ end
+
+ describe 'deliver_test_mail' do
+ before(:each) do
+ @emails = ActionMailer::Base.deliveries
+ @emails.clear
+ @params = 'info@mogoterra.com', 'test@test.net'
+ Resque.stub(:enqueue)
+ end
+
+ it 'should not deliver the email synchronously' do
+ AsynchTestMailer.deliver_test_mail *@params
+ @emails.size.should == 0
+ end
+
+ it 'should place the deliver action one the Resque mailer queue' do
+ Resque.should_receive(:enqueue).with(AsynchTestMailer, 'deliver_test_mail!', *@params)
+ AsynchTestMailer.deliver_test_mail *@params
+ end
+
+ it 'should not send deliver action to queue for environments where asychronous delivery is disabled' do
+ excluded_environments = [:cucumber, :foo, 'bar']
+ ::Resque::Mailer.excluded_environments = excluded_environments
+
+ excluded_environments.each do |env|
+ Object.send :remove_const, 'RAILS_ENV'
+ Object.const_set 'RAILS_ENV', env.to_s
+
+ Resque.should_not_receive(:enqueue)
+ AsynchTestMailer.deliver_test_mail *@params
+ end
+ end
+ end
+
+ describe 'deliver_test_mail!' do
+ it 'should deliver the mail' do
+ emails = ActionMailer::Base.deliveries
+ emails.clear
+ AsynchTestMailer.deliver_test_mail! 'info@mogoterra.com', 'test@test.net'
+ emails.size.should == 1
+ end
+ end
+
+ it 'should have a queue' do
+ AsynchTestMailer.queue.should == :mailer
+ end
+
+ it 'should perform a queued mailer job' do
+ AsynchTestMailer.should_receive("deliver_test_mail!").with(1, { :foo => 'bar' })
+ AsynchTestMailer.perform("deliver_test_mail!", 1, { :foo => 'bar' })
+ end
+end
1  spec/spec.opts
@@ -0,0 +1 @@
+--color
9 spec/spec_helper.rb
@@ -0,0 +1,9 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'resque_mailer'
+require 'spec'
+require 'spec/autorun'
+
+Spec::Runner.configure do |config|
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.