Permalink
Browse files

Merge branch 'release/1.0.0rc1'

  • Loading branch information...
2 parents 97d8b27 + f20281c commit 551c1a78599dd6ce749b6690eb02899f9bf30d02 @mjonuschat committed Nov 15, 2011
View
@@ -0,0 +1,7 @@
+.bundle/
+log/*.log
+pkg/
+spec/dummy/db/*.sqlite3
+spec/dummy/log/*.log
+spec/dummy/tmp/
+Gemfile.lock
View
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+# Declare your gem's dependencies in qu_mailer.gemspec.
+# Bundler will treat runtime dependencies like base dependencies, and
+# development dependencies will be added by default to the :development group.
+gemspec
View
@@ -0,0 +1,20 @@
+Copyright 2011 YOURNAME
+
+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.
View
@@ -0,0 +1,90 @@
+# Qu::Mailer
+
+A gem plugin which allows messages prepared by ActionMailer to be delivered
+asynchronously. Assumes you're using [Qu](https://github.com/bkeepers/qu)
+for your background jobs.
+
+Note that Qu::Mailer only work with Rails 3.x.
+
+## Usage
+
+Include Qu::Mailer in your ActionMailer subclass(es) like this:
+
+ class MyMailer < ActionMailer::Base
+ include Qu::Mailer
+ end
+
+Now, when @MyMailer.subject_email(params).deliver@ is called, an entry
+will be created in the job queue. Your Qu workers will be able to deliver
+this message for you. The queue we're using is imaginatively named **mailer**,
+so just make sure your workers know about it and are loading your environment:
+
+ bundle exec rake environment qu:work QUEUES=mailer
+
+Note that you can still have mail delivered synchronously by using the bang
+method variant:
+
+ MyMailer.subject_email(params).deliver!
+
+Oh, by the way. Don't forget that your async mailer jobs will be processed by
+a separate worker. This means that you should resist the temptation to pass
+database-backed objects as parameters in your mailer and instead pass record
+identifiers. Then, in your delivery method, you can look up the record from
+the id and use it as needed.
+
+If you want to set a different default queue name for your mailer, you can
+change the @default_queue_name@ property like so:
+
+ # config/initializers/qu_mailer.rb
+ Qu::Mailer.default_queue_name = 'application_specific_mailer'
+
+This is useful when you are running more than one application using
+Qu::Maile in a shared environment. You will need to use the new queue
+name when starting your workers.
+
+ bundle exec rake environment qu:work QUEUES=application_specific_mailer
+
+## Qu::Mailer as a Project Default
+
+If you have a variety of mailers in your application and want all of them to use
+Qu::Mailer by default, you can subclass ActionMailer::Base and have your
+other mailers inherit from an AsyncMailer:
+
+ # config/initializers/qu_mailer.rb
+ class BackgroundMailer < ActionMailer::Base
+ include Qu::Mailer
+ end
+
+ # app/mailers/example_mailer.rb
+ class ExampleMailer < AsyncMailer
+ def say_hello(user)
+ # ...
+ end
+ end
+
+## Installation
+
+ # Rails 3.x: add it to your Gemfile
+ gem 'qu-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/qu_mailer.rb
+ Qu::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 a port of [ResqueMailer](https://github.com/zapnap/resque_mailer) by Nick Plante.
+Modified to work with the Qu queueing library by Morton Jonuschat.
View
@@ -0,0 +1,37 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'QuMailer'
+ rdoc.options << '--line-numbers'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+
+
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+
+task :default => :test
View
@@ -0,0 +1 @@
+require 'qu/mailer'
View
@@ -0,0 +1,63 @@
+module Qu
+ module Mailer
+ autoload :MessageDecoy, 'qu/mailer/message_decoy'
+
+ class << self
+ attr_accessor :default_queue_name, :default_queue_target
+ attr_reader :excluded_environments
+
+ def excluded_environments=(envs)
+ @excluded_environments = [*envs].map { |e| e.to_sym }
+ end
+
+ def included(base)
+ base.extend(ClassMethods)
+ end
+ end
+
+ # Defaults
+ self.default_queue_target = ::Qu
+ self.default_queue_name = 'mailer'
+ self.excluded_environments = [ :test ]
+
+ module ClassMethods
+ def current_env
+ ::Rails.env
+ end
+
+ def method_missing(method_name, *args)
+ return super if environment_excluded?
+
+ if action_methods.include?(method_name.to_s)
+ MessageDecoy.new(self, method_name, *args)
+ else
+ super
+ end
+ end
+
+ def perform(action, *args)
+ self.send(:new, action, *args).message.deliver
+ end
+
+ def environment_excluded?
+ !ActionMailer::Base.perform_deliveries || excluded_environment?(current_env)
+ end
+
+ def queue
+ ::Qu::Mailer.default_queue_name
+ end
+
+ def qu
+ ::Qu::Mailer.default_queue_target
+ end
+
+ def excluded_environment?(name)
+ ::Qu::Mailer.excluded_environments && ::Qu::Mailer.excluded_environments.include?(name.to_sym)
+ end
+
+ def deliver?
+ true
+ end
+ end
+ end
+end
@@ -0,0 +1,33 @@
+module Qu
+ module Mailer
+ class MessageDecoy
+ def initialize(mailer_class, method_name, *args)
+ @mailer_class = mailer_class
+ @method_name = method_name
+ *@args = *args
+ end
+
+ def qu
+ ::Qu::Mailer.default_queue_target
+ end
+
+ def actual_message
+ @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
+ end
+
+ def deliver
+ if @mailer_class.deliver?
+ qu.enqueue(@mailer_class, @method_name, *@args)
+ end
+ end
+
+ def deliver!
+ actual_message.deliver!
+ end
+
+ def method_missing(method_name, *args)
+ actual_message.send(method_name, *args)
+ end
+ end
+ end
+end
View
@@ -0,0 +1,5 @@
+module Qu
+ module Mailer
+ VERSION = "1.0.0rc1"
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :qu_mailer do
+# # Task goes here
+# end
View
@@ -0,0 +1,24 @@
+$:.push File.expand_path("../lib", __FILE__)
+
+# Maintain your gem's version:
+require "qu/mailer/version"
+
+# Describe your gem and declare its dependencies:
+Gem::Specification.new do |s|
+ s.name = "qu-mailer"
+ s.version = Qu::Mailer::VERSION
+ s.authors = ["Morton Jonuschat"]
+ s.email = ["yabawock@gmail.com"]
+ s.homepage = "https://github.com/yabawock/qu-mailer"
+ s.summary = "Asynchronous email sending with ActionMailer and Qu"
+ s.description = "QuMailer is a Ruby on Rails 3.x plugin for sending asynchronous emails with ActionMailer an Qu."
+
+ s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
+ s.test_files = Dir["spec/**/*"]
+
+ s.add_dependency "actionmailer", ">= 3.0"
+ s.add_dependency "qu", ">= 0.1.3"
+
+ s.add_development_dependency "rspec", "~> 2.7.0"
+ s.add_development_dependency "rake"
+end
View
@@ -0,0 +1,81 @@
+require 'spec_helper'
+
+describe Qu::Mailer do
+ let(:qu) { FakeQu }
+
+ before do
+ Qu::Mailer.default_queue_target = qu
+ Qu::Mailer.stub(:success!)
+ TestMailer.stub(:current_env => :test)
+ end
+
+ describe "backend" do
+ it "allows overriding the default queue target for test purposes" do
+ Qu::Mailer.default_queue_target = FakeQu
+ TestMailer.qu.should == FakeQu
+ end
+ end
+
+ it 'should use "mailer" as the default queue name' do
+ TestMailer.queue.should == 'mailer'
+ end
+
+ it 'should allow overriding the default queue name' do
+ Qu::Mailer.default_queue_name = 'postal'
+ TestMailer.queue.should == 'postal'
+ end
+
+ describe '#deliver' do
+ before(:all) do
+ @delivery = lambda { TestMailer.test_mail(TestMailer::MAIL_PARAMS).deliver }
+ end
+
+ it 'should not perform synchronous deliveries' do
+ lambda { @delivery.call }.should_not change(ActionMailer::Base.deliveries, :size)
+ end
+
+ it 'should place the deliver action on the Qu "mailer" queue' do
+ qu.should_receive(:enqueue).with(TestMailer, :test_mail, TestMailer::MAIL_PARAMS)
+ @delivery.call
+ end
+
+ context "with the the current environment excluded" do
+ it 'should not deliver asynchronously' do
+ Qu::Mailer.stub(:excluded_environments).and_return([:custom])
+ TestMailer.should_receive(:current_env).and_return(:custom)
+ qu.should_not_receive(:enqueue)
+ @delivery.call
+ end
+ end
+
+ it 'should not invoke the body method more than once' do
+ Qu::Mailer.should_not_receive(:success!)
+ TestMailer.test_mail(TestMailer::MAIL_PARAMS).deliver
+ end
+ end
+
+ describe '#deliver!' do
+ it 'should deliver the email sychronously' do
+ lambda { TestMailer.test_mail(TestMailer::MAIL_PARAMS).deliver! }.should change(ActionMailer::Base.deliveries, :size).by(1)
+ end
+ end
+
+ describe '#perform' do
+ it 'should trigger delivery of a queued job' do
+ lambda { TestMailer.perform(:test_mail, TestMailer::MAIL_PARAMS) }.should change(ActionMailer::Base.deliveries, :size).by(1)
+ end
+ end
+
+ describe 'original mail methods' do
+ it 'should be preserved' do
+ TestMailer.test_mail(TestMailer::MAIL_PARAMS).subject.should == 'Subject'
+ TestMailer.test_mail(TestMailer::MAIL_PARAMS).from.should include('jane.doe@example.com')
+ TestMailer.test_mail(TestMailer::MAIL_PARAMS).to.should include('john.doe@example.com')
+ end
+
+ it 'should render the body before queuing' do
+ Qu::Mailer.should_receive(:success!).once
+ TestMailer.test_mail(TestMailer::MAIL_PARAMS).subject
+ end
+ end
+end
View
@@ -0,0 +1,12 @@
+# Configure Rails Environment
+ENV["RAILS_ENV"] = "test"
+
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+require 'action_mailer'
+require 'qu-mailer'
+require 'rspec/autorun'
+
+# Load support files
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1 @@
+ActionMailer::Base.delivery_method = :test
@@ -0,0 +1 @@
+Qu::Mailer.excluded_environments = []
Oops, something went wrong.

0 comments on commit 551c1a7

Please sign in to comment.