mallet 0.0.5 → 0.0.6
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/lib/generators/active_record/templates/migration.rb +1 -2
- data/lib/mallet/mail.rb +13 -2
- data/lib/mallet/mail_generation.rb +2 -14
- data/lib/mallet/version.rb +1 -1
- data/spec/internal/db/mallet_test.sqlite +0 -0
- data/spec/internal/db/schema.rb +1 -2
- data/spec/lib/mallet/mail_spec.rb +23 -32
- metadata +1 -1
@@ -2,8 +2,7 @@ class CreateMalletTables < ActiveRecord::Migration
|
|
2
2
|
def self.change
|
3
3
|
|
4
4
|
create_table "mallet_mails", :force => true do |t|
|
5
|
-
t.string "
|
6
|
-
t.string "mailer_method"
|
5
|
+
t.string "definition"
|
7
6
|
t.string "malletable_type"
|
8
7
|
t.string "malletable_id"
|
9
8
|
t.string "workflow_state"
|
data/lib/mallet/mail.rb
CHANGED
@@ -12,8 +12,7 @@ module Mallet
|
|
12
12
|
scope :failed, where(workflow_state: 'failed')
|
13
13
|
scope :for, lambda {|malletable| where(malletable_type: malletable.class.name, malletable_id: malletable.id)}
|
14
14
|
|
15
|
-
validates_presence_of :
|
16
|
-
validates_presence_of :mailer_method
|
15
|
+
validates_presence_of :definition
|
17
16
|
validates_presence_of :malletable
|
18
17
|
|
19
18
|
before_save :validate
|
@@ -34,6 +33,7 @@ module Mallet
|
|
34
33
|
end
|
35
34
|
|
36
35
|
def validate
|
36
|
+
raise Exception unless DEFINITIONS.has_key?(definition)
|
37
37
|
raise Exception unless mailer_class.constantize.ancestors.include? ActionMailer::Base
|
38
38
|
end
|
39
39
|
|
@@ -42,6 +42,7 @@ module Mallet
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def deliver
|
45
|
+
validate
|
45
46
|
mailer_class.constantize.send mailer_method.to_sym, malletable
|
46
47
|
end
|
47
48
|
|
@@ -49,6 +50,16 @@ module Mallet
|
|
49
50
|
pending.each(&:delayed_delivery)
|
50
51
|
end
|
51
52
|
|
53
|
+
protected
|
54
|
+
|
55
|
+
def mailer_class
|
56
|
+
DEFINITIONS[definition]['mailer']
|
57
|
+
end
|
58
|
+
|
59
|
+
def mailer_method
|
60
|
+
DEFINITIONS[definition]['method']
|
61
|
+
end
|
62
|
+
|
52
63
|
end
|
53
64
|
|
54
65
|
end
|
@@ -13,25 +13,13 @@ module Mallet
|
|
13
13
|
mail.malletable_id = malletable.id
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
mail.
|
18
|
-
end
|
19
|
-
|
20
|
-
def method(method)
|
21
|
-
mail.mailer_method = method
|
16
|
+
def definition(name)
|
17
|
+
mail.definition = name
|
22
18
|
end
|
23
19
|
|
24
20
|
def persist!
|
25
21
|
mail.save!
|
26
22
|
end
|
27
|
-
|
28
|
-
def definition(name)
|
29
|
-
(DEFINITIONS[name] || {}).each do |key, value|
|
30
|
-
if respond_to?(key.to_sym)
|
31
|
-
send key.to_sym, value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
23
|
|
36
24
|
end
|
37
25
|
|
data/lib/mallet/version.rb
CHANGED
Binary file
|
data/spec/internal/db/schema.rb
CHANGED
@@ -4,6 +4,11 @@ describe Mallet::Mail do
|
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
@job = Delayed::Job.create!
|
7
|
+
|
8
|
+
Mallet::DEFINITIONS['welcome'] = {
|
9
|
+
'mailer' => 'ActionMailer::Base',
|
10
|
+
'method' => 'welcome'
|
11
|
+
}
|
7
12
|
end
|
8
13
|
|
9
14
|
context "on creation" do
|
@@ -13,8 +18,7 @@ describe Mallet::Mail do
|
|
13
18
|
|
14
19
|
@mail = Mallet::mail do
|
15
20
|
to job
|
16
|
-
|
17
|
-
method 'welcome'
|
21
|
+
definition 'welcome'
|
18
22
|
end
|
19
23
|
end
|
20
24
|
|
@@ -26,6 +30,14 @@ describe Mallet::Mail do
|
|
26
30
|
@mail.malletable.should == @job
|
27
31
|
end
|
28
32
|
|
33
|
+
it "should identify the mailer class" do
|
34
|
+
@mail.send(:mailer_class).should == "ActionMailer::Base"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should identify the mailer method" do
|
38
|
+
@mail.send(:mailer_method).should == "welcome"
|
39
|
+
end
|
40
|
+
|
29
41
|
it "should trigger sending the mail via delayed job" do
|
30
42
|
Delayed::Job.count.should == 2
|
31
43
|
ActionMailer::Base.should_receive(:welcome).with(@job)
|
@@ -33,29 +45,13 @@ describe Mallet::Mail do
|
|
33
45
|
end
|
34
46
|
|
35
47
|
it "should raise an exception if the mailer does not inherit from ActionMailer" do
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
context "creating a mallet mail" do
|
43
|
-
|
44
|
-
it "should allow a definition to be specified" do
|
45
|
-
Mallet::DEFINITIONS['audit'] = {
|
46
|
-
'mailer' => 'ActionMailer::Base',
|
48
|
+
Mallet::DEFINITIONS['bad_definition'] = {
|
49
|
+
'mailer' => 'ActionMailer::Base2',
|
47
50
|
'method' => 'welcome'
|
48
51
|
}
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
@mail = Mallet::mail do
|
53
|
-
to job
|
54
|
-
definition 'audit'
|
55
|
-
end
|
56
|
-
|
57
|
-
@mail.mailer_class.should == 'ActionMailer::Base'
|
58
|
-
@mail.mailer_method.should == 'welcome'
|
53
|
+
@mail.definition = 'bad_definition'
|
54
|
+
expect { @mail.save! }.to raise_error
|
59
55
|
end
|
60
56
|
|
61
57
|
end
|
@@ -67,8 +63,7 @@ describe Mallet::Mail do
|
|
67
63
|
|
68
64
|
@mail = Mallet::mail do
|
69
65
|
to job
|
70
|
-
|
71
|
-
method 'welcome'
|
66
|
+
definition 'welcome'
|
72
67
|
end
|
73
68
|
|
74
69
|
@job = job
|
@@ -91,8 +86,7 @@ describe Mallet::Mail do
|
|
91
86
|
expect {
|
92
87
|
Mallet::mail do
|
93
88
|
to job
|
94
|
-
|
95
|
-
method 'welcome'
|
89
|
+
definition 'welcome'
|
96
90
|
end
|
97
91
|
}.to change{ Mallet::Mail.pending.count }.by(1)
|
98
92
|
end
|
@@ -102,8 +96,7 @@ describe Mallet::Mail do
|
|
102
96
|
|
103
97
|
mail = Mallet::mail do
|
104
98
|
to job
|
105
|
-
|
106
|
-
method 'welcome'
|
99
|
+
definition 'welcome'
|
107
100
|
end
|
108
101
|
|
109
102
|
expect {mail.error!}.to change{ Mallet::Mail.failed.count }.by(1)
|
@@ -114,8 +107,7 @@ describe Mallet::Mail do
|
|
114
107
|
|
115
108
|
mail = Mallet::mail do
|
116
109
|
to job
|
117
|
-
|
118
|
-
method 'welcome'
|
110
|
+
definition 'welcome'
|
119
111
|
end
|
120
112
|
|
121
113
|
mail.should_receive(:deliver)
|
@@ -128,8 +120,7 @@ describe Mallet::Mail do
|
|
128
120
|
expect{
|
129
121
|
mail = Mallet::mail do
|
130
122
|
to job
|
131
|
-
|
132
|
-
method 'welcome'
|
123
|
+
definition 'welcome'
|
133
124
|
end
|
134
125
|
}.to change{ Mallet::Mail.for(job).count }.by(1)
|
135
126
|
end
|