merb-resque-mailer 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +63 -0
- data/lib/merb-resque-mailer.rb +44 -0
- data/spec/merb-resque-mailer_spec.rb +67 -0
- data/spec/spec_helper.rb +22 -0
- metadata +67 -0
data/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Merb Resque Mailer
|
|
2
|
+
==================
|
|
3
|
+
|
|
4
|
+
Plugin for Merb which allows putting mail deliveries onto [Resque](http://github.com/defunkt/resque) queue.
|
|
5
|
+
|
|
6
|
+
Usage
|
|
7
|
+
-----
|
|
8
|
+
|
|
9
|
+
Include Resque::Mailer module in your Merb::MailController subclass(es) like this:
|
|
10
|
+
|
|
11
|
+
class UserMailer < Merb::MailController
|
|
12
|
+
include Resque::Mailer
|
|
13
|
+
...
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
or directly in Merb::MailController class if you want to enable it for all mailers:
|
|
17
|
+
|
|
18
|
+
class Merb::MailController
|
|
19
|
+
include Resque::Mailer
|
|
20
|
+
...
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
Jobs are added to "mailer" queue so you should start at least one worker listening on "mailer" queue:
|
|
24
|
+
|
|
25
|
+
QUEUE=mailer rake merb_env resque:work
|
|
26
|
+
|
|
27
|
+
Be sure you have 'resque/tasks' required in your Rakefile (or somewhere in lib/tasks/), it's required for above task to work.
|
|
28
|
+
|
|
29
|
+
From now on all emails will be sent asynchronously using Resque worker(s).
|
|
30
|
+
|
|
31
|
+
Installation
|
|
32
|
+
------------
|
|
33
|
+
|
|
34
|
+
Gem is hosted on gemcutter.org, simply install it by:
|
|
35
|
+
|
|
36
|
+
gem install merb-resque-mailer
|
|
37
|
+
|
|
38
|
+
and require it in your app:
|
|
39
|
+
|
|
40
|
+
require "merb-resque-mailer"
|
|
41
|
+
|
|
42
|
+
If using bundler add it to Gemfile:
|
|
43
|
+
|
|
44
|
+
gem "merb-resque-mailer"
|
|
45
|
+
|
|
46
|
+
Configuration
|
|
47
|
+
-------------
|
|
48
|
+
|
|
49
|
+
You can configure for which environments you don't want to use Merb Resque Mailer by setting Resque::Mailer.excluded_environments option (by default :test env is excluded). If you want to exclude also :development env put following code somewhere in config/init.rb:
|
|
50
|
+
Resque::Mailer.excluded_environments = [:test, :development]
|
|
51
|
+
|
|
52
|
+
or
|
|
53
|
+
|
|
54
|
+
Resque::Mailer.excluded_environments << :development
|
|
55
|
+
|
|
56
|
+
If you want to put deliveries to other queue than default "mailer" you can set it like this:
|
|
57
|
+
|
|
58
|
+
Resque::Mailer.queue_name = "notifications"
|
|
59
|
+
|
|
60
|
+
Credits
|
|
61
|
+
-------
|
|
62
|
+
|
|
63
|
+
This piece of code was inspired by work of [Nick Plante](http://github.com/zapnap) who created [resque_mailer](http://github.com/zapnap/resque_mailer) for Rails' ActionMailer. Rewritten to work with Merb Mailer by [Marcin Kulik](http://github.com/sickill).
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'resque'
|
|
2
|
+
|
|
3
|
+
module Resque
|
|
4
|
+
module Mailer
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
attr_accessor :queue_name
|
|
8
|
+
attr_reader :excluded_environments
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.excluded_environments=(envs)
|
|
12
|
+
@excluded_environments = envs.map { |e| e.to_sym }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
self.queue_name = "mailer"
|
|
16
|
+
self.excluded_environments = [:test]
|
|
17
|
+
|
|
18
|
+
def self.included(base)
|
|
19
|
+
base.extend(ClassMethods)
|
|
20
|
+
base.class_eval do
|
|
21
|
+
alias_method :dispatch_and_deliver!, :dispatch_and_deliver
|
|
22
|
+
|
|
23
|
+
def dispatch_and_deliver(method, mail_params)
|
|
24
|
+
if ::Resque::Mailer.excluded_environments.include?(Merb.env.to_sym)
|
|
25
|
+
dispatch_and_deliver!(method, mail_params)
|
|
26
|
+
else
|
|
27
|
+
::Resque.enqueue(self.class, @params, method, mail_params)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
module ClassMethods
|
|
34
|
+
def queue
|
|
35
|
+
Resque::Mailer.queue_name
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def perform(params, method, mail_params)
|
|
39
|
+
new(Mash.new(params)).dispatch_and_deliver!(method.to_sym, mail_params)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
|
+
|
|
3
|
+
Resque::Mailer.excluded_environments = []
|
|
4
|
+
Resque::Mailer.queue_name = "foobar"
|
|
5
|
+
|
|
6
|
+
class TestMailer < Merb::MailController
|
|
7
|
+
include Resque::Mailer
|
|
8
|
+
|
|
9
|
+
ACTION_PARAMS = {}
|
|
10
|
+
MAILER_PARAMS = { :from => "jola@example.org", :to => "misio@example.org", :subject => "Friendship established" }
|
|
11
|
+
|
|
12
|
+
def friendship_confirmation
|
|
13
|
+
render_mail :text => "Mail content"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
delivery = lambda do
|
|
18
|
+
TestMailer.new(TestMailer::ACTION_PARAMS).dispatch_and_deliver(:friendship_confirmation, TestMailer::MAILER_PARAMS)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe TestMailer do
|
|
22
|
+
describe '#dispatch_and_deliver' do
|
|
23
|
+
before(:each) do
|
|
24
|
+
Resque.stub(:enqueue)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'should not deliver the email synchronously' do
|
|
28
|
+
lambda { delivery.call }.should_not change(Merb::Mailer.deliveries, :count)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'should place the deliver action on the Resque "mailer" queue' do
|
|
32
|
+
Resque.should_receive(:enqueue).with(TestMailer, TestMailer::ACTION_PARAMS, :friendship_confirmation, TestMailer::MAILER_PARAMS)
|
|
33
|
+
delivery.call
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it 'should not deliver through Resque for excluded environments' do
|
|
37
|
+
excluded_envs = ["test", :staging]
|
|
38
|
+
Resque::Mailer.excluded_environments = excluded_envs
|
|
39
|
+
|
|
40
|
+
excluded_envs.each do |env|
|
|
41
|
+
Merb.should_receive(:env).and_return(env.to_sym)
|
|
42
|
+
Resque.should_not_receive(:enqueue)
|
|
43
|
+
delivery.call
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
describe '#dispatch_and_deliver!' do
|
|
49
|
+
it 'should deliver the mail' do
|
|
50
|
+
lambda { delivery.call }.should change(Merb::Mailer.deliveries, :count).by(1)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
describe ".queue" do
|
|
55
|
+
it "should return default queue name" do
|
|
56
|
+
TestMailer.queue.should == "foobar"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe ".perform" do
|
|
61
|
+
it 'should perform a queued mailer job' do
|
|
62
|
+
lambda do
|
|
63
|
+
TestMailer.perform(TestMailer::ACTION_PARAMS, :friendship_confirmation, TestMailer::MAILER_PARAMS)
|
|
64
|
+
end.should change(Merb::Mailer.deliveries, :count).by(1)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
3
|
+
require 'spec'
|
|
4
|
+
require 'spec/autorun'
|
|
5
|
+
|
|
6
|
+
require 'merb-core'
|
|
7
|
+
require 'merb-mailer'
|
|
8
|
+
|
|
9
|
+
Merb::Config.use do |c|
|
|
10
|
+
c[:session_store] = :memory
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Merb.start :environment => 'test'
|
|
14
|
+
Merb.start_environment(:testing => true, :adapter => 'runner', :environment => 'test')
|
|
15
|
+
Merb::Mailer.delivery_method = :test_send
|
|
16
|
+
|
|
17
|
+
require 'merb-resque-mailer'
|
|
18
|
+
|
|
19
|
+
Spec::Runner.configure do |config|
|
|
20
|
+
config.include Merb::Test::RequestHelper
|
|
21
|
+
config.include Merb::Test::ControllerHelper
|
|
22
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: merb-resque-mailer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: "0.4"
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Marcin Kulik
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2010-01-22 00:00:00 +01:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: resque
|
|
17
|
+
type: :runtime
|
|
18
|
+
version_requirement:
|
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - ">="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: "0"
|
|
24
|
+
version:
|
|
25
|
+
description:
|
|
26
|
+
email: marcin.kulik@gmail.com
|
|
27
|
+
executables: []
|
|
28
|
+
|
|
29
|
+
extensions: []
|
|
30
|
+
|
|
31
|
+
extra_rdoc_files: []
|
|
32
|
+
|
|
33
|
+
files:
|
|
34
|
+
- lib/merb-resque-mailer.rb
|
|
35
|
+
- README.md
|
|
36
|
+
- spec/spec_helper.rb
|
|
37
|
+
- spec/merb-resque-mailer_spec.rb
|
|
38
|
+
has_rdoc: true
|
|
39
|
+
homepage: http://sickill.net
|
|
40
|
+
licenses: []
|
|
41
|
+
|
|
42
|
+
post_install_message:
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
|
|
45
|
+
require_paths:
|
|
46
|
+
- lib
|
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
version:
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: "0"
|
|
58
|
+
version:
|
|
59
|
+
requirements: []
|
|
60
|
+
|
|
61
|
+
rubyforge_project:
|
|
62
|
+
rubygems_version: 1.3.5
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 3
|
|
65
|
+
summary: Merb plugin for putting mail delivery jobs onto Resque queue
|
|
66
|
+
test_files: []
|
|
67
|
+
|