dj_mailer 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +2 -2
- data/README.md +15 -0
- data/Rakefile +1 -1
- data/contributors +2 -0
- data/features/dj_mailer_deliver.feature +17 -8
- data/features/readme.md +104 -0
- data/lib/dj_mailer.rb +13 -1
- data/lib/dj_mailer/version.rb +1 -1
- data/spec/lib/deliverable_spec.rb +12 -3
- data/spec/spec_helper.rb +25 -0
- metadata +6 -2
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2012
|
1
|
+
Copyright (c) 2012 Andrea Longhi
|
2
2
|
|
3
3
|
MIT License
|
4
4
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -36,6 +36,21 @@ Or install it yourself as:
|
|
36
36
|
gem install dj_mailer
|
37
37
|
```
|
38
38
|
|
39
|
+
## Configuration
|
40
|
+
|
41
|
+
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:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# config/initializers/dj_mailer.rb
|
45
|
+
|
46
|
+
DjMailer::Delayable.excluded_environments = [:test, :cucumber] # etc.
|
47
|
+
```
|
48
|
+
|
49
|
+
## Documentation
|
50
|
+
|
51
|
+
You can find some more documentation on the workings of the gem on relish:
|
52
|
+
https://www.relishapp.com/spaghetticode/dj-mailer/docs
|
53
|
+
|
39
54
|
|
40
55
|
## Tests
|
41
56
|
|
data/Rakefile
CHANGED
data/contributors
ADDED
@@ -1,14 +1,23 @@
|
|
1
|
-
Feature: deliver email asyncronously with delayed_job
|
2
|
-
|
3
|
-
As an action mailer 3 and delayed_job user
|
4
|
-
I want to deliver email asyncronously
|
1
|
+
Feature: deliver email asyncronously with delayed_job
|
2
|
+
|
3
|
+
As an action mailer 3 and *delayed_job* user
|
4
|
+
I want to deliver email asyncronously transparently
|
5
|
+
in order to avoid updating the existing syncronous codebase
|
5
6
|
|
6
7
|
This is the current delayed job recommended syntax to run asyncronous
|
7
8
|
email delivers:
|
8
|
-
|
9
|
-
|
9
|
+
|
10
|
+
without *delayed_job*:
|
11
|
+
|
12
|
+
Notifier.signup(@user).deliver
|
13
|
+
|
14
|
+
with *delayed_job*:
|
15
|
+
|
16
|
+
Notifier.delay.signup(@user)
|
17
|
+
|
10
18
|
The syntax is quite different, so if you have a big codebase that doesn't use
|
11
19
|
yet delayed job it could become rather nasty to replace all the deliver calls.
|
20
|
+
|
12
21
|
This gem allows you to keep the old interface and still get the delayed job
|
13
22
|
asyncronous functionality.
|
14
23
|
|
@@ -17,13 +26,13 @@ Scenario: Email is enqueued successfully
|
|
17
26
|
"""
|
18
27
|
class TestMailer < ActionMailer::Base
|
19
28
|
def test(recipient)
|
20
|
-
mail to: recipient, from: 'test@test.
|
29
|
+
mail to: recipient, from: 'test@test.it', subject: 'async email'
|
21
30
|
end
|
22
31
|
end
|
23
32
|
"""
|
24
33
|
When I try to send a test email with the following code:
|
25
34
|
"""
|
26
|
-
TestMailer.test('sender@test.com')
|
35
|
+
TestMailer.test('sender@test.com').deliver
|
27
36
|
"""
|
28
37
|
Then a delayed job object should be returned
|
29
38
|
And I should see a new delayed_job record in the table
|
data/features/readme.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# DjMailer
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/spaghetticode/dj_mailer.png)](http://travis-ci.org/spaghetticode/dj_mailer)
|
4
|
+
|
5
|
+
This gem allows you to send emails asyncronously via delayed_job_active_record
|
6
|
+
without modifying the existing codebase, keeping the standard action mailer
|
7
|
+
interface.
|
8
|
+
|
9
|
+
Currenlty delayed_job_active_record requires that you replace all your email
|
10
|
+
deliver calls as follows:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
# without delayed job
|
14
|
+
Notifier.signup(@user).deliver
|
15
|
+
|
16
|
+
# with delayed job
|
17
|
+
Notifier.delay.signup(@user)
|
18
|
+
```
|
19
|
+
|
20
|
+
By adding this gem in your gemfile all your current emails will be automatically
|
21
|
+
handled by delayed job without any interface change. At the moment you have
|
22
|
+
to setup the delayed job environment on your own running the migrations for the
|
23
|
+
db.
|
24
|
+
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
```ruby
|
30
|
+
gem 'dj_mailer'
|
31
|
+
```
|
32
|
+
And then execute: `bundle`
|
33
|
+
|
34
|
+
Or install it yourself as:
|
35
|
+
```bash
|
36
|
+
gem install dj_mailer
|
37
|
+
```
|
38
|
+
|
39
|
+
## Configuration
|
40
|
+
|
41
|
+
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:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# config/initializers/dj_mailer.rb
|
45
|
+
|
46
|
+
DjMailer::Delayable.excluded_environments = [:test, :cucumber] # etc.
|
47
|
+
```
|
48
|
+
|
49
|
+
## Documentation
|
50
|
+
|
51
|
+
You can find some more documentation on the workings of the gem on relish:
|
52
|
+
https://www.relishapp.com/spaghetticode/dj-mailer/docs
|
53
|
+
|
54
|
+
|
55
|
+
## Tests
|
56
|
+
|
57
|
+
to run the specs:
|
58
|
+
```bash
|
59
|
+
bundle exec rake spec
|
60
|
+
```
|
61
|
+
to run the cucumber features:
|
62
|
+
```bash
|
63
|
+
bundle exec rake cucumber
|
64
|
+
```
|
65
|
+
|
66
|
+
|
67
|
+
## Sample delay job migration
|
68
|
+
|
69
|
+
Just a reminder I have to add the generator (via rails you can simply run `rails generate delayed_job:active_record)`
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class CreateDelayedJobs < ActiveRecord::Migration
|
73
|
+
def self.up
|
74
|
+
create_table :delayed_jobs, :force => true do |table|
|
75
|
+
table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
|
76
|
+
table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
|
77
|
+
table.text :handler # YAML-encoded string of the object that will do work
|
78
|
+
table.text :last_error # reason for last failure (See Note below)
|
79
|
+
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
|
80
|
+
table.datetime :locked_at # Set when a client is working on this object
|
81
|
+
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
|
82
|
+
table.string :locked_by # Who is working on this object (if locked)
|
83
|
+
table.string :queue # The name of the queue this job is in
|
84
|
+
table.timestamps
|
85
|
+
end
|
86
|
+
|
87
|
+
add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.down
|
91
|
+
drop_table :delayed_jobs
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
|
97
|
+
## Contributing
|
98
|
+
|
99
|
+
1. Fork it
|
100
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
101
|
+
3. Add your feature tests to the rspec/cucumber test suite
|
102
|
+
4. Commit your changes (`git commit -am 'Added some feature'`)
|
103
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
104
|
+
6. Create new Pull Request
|
data/lib/dj_mailer.rb
CHANGED
@@ -15,7 +15,7 @@ module DjMailer
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def method_missing_with_delay(method, *args)
|
18
|
-
if respond_to?(method) and !caller.grep(
|
18
|
+
if !environment_excluded? && respond_to?(method) and !caller.grep(/delayed_job/).present?
|
19
19
|
enqueue_with_delayed_job(method, *args)
|
20
20
|
else
|
21
21
|
method_missing_without_delay(method, *args)
|
@@ -27,6 +27,18 @@ module DjMailer
|
|
27
27
|
dj_instance.extend Deliverable
|
28
28
|
end
|
29
29
|
end
|
30
|
+
|
31
|
+
def environment_excluded?
|
32
|
+
defined?(Rails) && ::DjMailer::Delayable.excluded_environments.include?(Rails.env.to_sym)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.excluded_environments=(*environments)
|
36
|
+
@@excluded_environments = environments && environments.flatten.collect! { |env| env.to_sym }
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.excluded_environments
|
40
|
+
@@excluded_environments ||= []
|
41
|
+
end
|
30
42
|
end
|
31
43
|
end
|
32
44
|
|
data/lib/dj_mailer/version.rb
CHANGED
@@ -21,21 +21,30 @@ module DjMailer
|
|
21
21
|
before {subject.should_receive(:respond_to?).and_return(true)}
|
22
22
|
|
23
23
|
context 'when the caller is delayed_job' do
|
24
|
-
it '
|
25
|
-
subject.stub(:caller => [
|
24
|
+
it 'delegates to the original method_missing' do
|
25
|
+
subject.stub(:caller => ["/gems/delayed_job-3.0.2/lib/delayed/performable_mailer.rb:6:in `perform'"])
|
26
26
|
subject.should_receive(:method_missing_without_delay)
|
27
27
|
subject.method_missing_with_delay(:responding)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
context 'when the caller is not delayed_job' do
|
32
|
-
it '
|
32
|
+
it 'equeues the email for late delivery' do
|
33
33
|
subject.stub(:caller => [])
|
34
34
|
subject.should_receive(:enqueue_with_delayed_job)
|
35
35
|
subject.method_missing_with_delay(:responding)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
it 'delegates to original method_missing if this environment is excluded' do
|
41
|
+
with_stub_const(:Rails, double(:env => 'test')) do
|
42
|
+
with_excluded_environments([:test]) do
|
43
|
+
subject.should_receive(:method_missing_without_delay)
|
44
|
+
subject.method_missing_with_delay(:responding)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
39
48
|
end
|
40
49
|
end
|
41
50
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,27 @@
|
|
1
1
|
require File.expand_path('../../lib/dj_mailer', __FILE__)
|
2
2
|
|
3
|
+
def with_stub_const(const, value)
|
4
|
+
if Object.const_defined?(const)
|
5
|
+
begin
|
6
|
+
@const = const
|
7
|
+
Object.const_set(const, value)
|
8
|
+
yield
|
9
|
+
ensure
|
10
|
+
Object.const_set(const, @const)
|
11
|
+
end
|
12
|
+
else
|
13
|
+
begin
|
14
|
+
Object.const_set(const, value)
|
15
|
+
yield
|
16
|
+
ensure
|
17
|
+
Object.send(:remove_const, const)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def with_excluded_environments(envs)
|
23
|
+
DjMailer::Delayable.excluded_environments = envs
|
24
|
+
yield
|
25
|
+
ensure
|
26
|
+
DjMailer::Delayable.excluded_environments = []
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dj_mailer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionmailer
|
@@ -170,8 +170,10 @@ files:
|
|
170
170
|
- LICENSE
|
171
171
|
- README.md
|
172
172
|
- Rakefile
|
173
|
+
- contributors
|
173
174
|
- dj_mailer.gemspec
|
174
175
|
- features/dj_mailer_deliver.feature
|
176
|
+
- features/readme.md
|
175
177
|
- features/step_definitions/.gitkeep
|
176
178
|
- features/step_definitions/dj_mailer_deliver_steps.rb
|
177
179
|
- features/support/.gitkeep
|
@@ -210,6 +212,7 @@ summary: Allows to send all ActionMailer 3 emails via delayed_job in a transpare
|
|
210
212
|
way
|
211
213
|
test_files:
|
212
214
|
- features/dj_mailer_deliver.feature
|
215
|
+
- features/readme.md
|
213
216
|
- features/step_definitions/.gitkeep
|
214
217
|
- features/step_definitions/dj_mailer_deliver_steps.rb
|
215
218
|
- features/support/.gitkeep
|
@@ -219,3 +222,4 @@ test_files:
|
|
219
222
|
- spec/lib/.gitkeep
|
220
223
|
- spec/lib/deliverable_spec.rb
|
221
224
|
- spec/spec_helper.rb
|
225
|
+
has_rdoc:
|