mallet 0.0.8 → 0.0.9

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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mallet (0.0.7)
4
+ mallet (0.0.9)
5
5
  actionmailer (~> 3.0)
6
6
  activerecord (~> 3.0)
7
7
  activesupport
@@ -45,11 +45,11 @@ GEM
45
45
  rails (>= 3.0.0)
46
46
  thor (>= 0.14.6)
47
47
  database_cleaner (0.9.1)
48
- delayed_job (3.0.5)
49
- activesupport (~> 3.0)
50
- delayed_job_active_record (0.4.3)
51
- activerecord (>= 2.1.0, < 4)
52
- delayed_job (~> 3.0)
48
+ delayed_job (4.0.0)
49
+ activesupport (>= 3.0, < 4.1)
50
+ delayed_job_active_record (4.0.0)
51
+ activerecord (>= 3.0, < 4.1)
52
+ delayed_job (>= 3.0, < 4.1)
53
53
  diff-lcs (1.2.1)
54
54
  erubis (2.7.0)
55
55
  hike (1.2.1)
@@ -115,7 +115,7 @@ GEM
115
115
  polyglot
116
116
  polyglot (>= 0.3.1)
117
117
  tzinfo (0.3.37)
118
- workflow (1.0.0)
118
+ workflow (1.1.0)
119
119
 
120
120
  PLATFORMS
121
121
  ruby
@@ -12,10 +12,12 @@ module Mallet
12
12
  'mallet_'
13
13
  end
14
14
 
15
- def self.mail(&block)
16
- generator = MailGeneration.new
17
- generator.instance_eval(&block)
15
+ def self.mail(params = {})
16
+ generator = MailGeneration.new
17
+ generator.for(params[:for])
18
+ generator.definition(params[:definition])
18
19
  generator.persist!
20
+ Hooks.on_create(generator.mail)
19
21
  generator.mail
20
22
  end
21
23
 
@@ -23,3 +25,4 @@ end
23
25
 
24
26
  require "mallet/mail"
25
27
  require "mallet/mail_generation"
28
+ require "mallet/hooks"
@@ -0,0 +1,27 @@
1
+ module Mallet
2
+
3
+ class Hooks
4
+
5
+ def self.on_create(mallet_mail)
6
+
7
+ end
8
+
9
+ def self.on_deliver(mallet_mail)
10
+
11
+ end
12
+
13
+ def self.on_error(mallet_mail)
14
+
15
+ end
16
+
17
+ def self.on_success(mallet_mail)
18
+
19
+ end
20
+
21
+ def self.on_bounce(mallet_mail)
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -4,19 +4,12 @@ module Mallet
4
4
  include ::Workflow
5
5
 
6
6
  attr_accessible :workflow_state
7
-
8
- #belongs_to :malletable, polymorphic: true
9
-
10
- scope :pending, where(workflow_state: 'pending')
11
- scope :sent, where(workflow_state: 'sent')
12
- scope :failed, where(workflow_state: 'failed')
13
7
  scope :for, lambda {|malletable| where(malletable_type: malletable.class.name, malletable_id: malletable.id)}
14
8
 
15
9
  validates_presence_of :definition
16
10
  validates_presence_of :malletable
17
11
 
18
12
  before_save :validate
19
- after_create :delayed_delivery
20
13
 
21
14
  workflow do
22
15
  state :pending do
@@ -25,7 +18,14 @@ module Mallet
25
18
  end
26
19
 
27
20
  state :failed
28
- state :sent
21
+
22
+ state :sent do
23
+ event :success, :transitions_to => :delivered
24
+ event :bounce, :transitions_to => :bounced
25
+ end
26
+
27
+ state :delivered
28
+ state :bounced
29
29
 
30
30
  after_transition do
31
31
  notify_observers "on_#{workflow_state}".to_sym
@@ -43,30 +43,35 @@ module Mallet
43
43
 
44
44
  def validate
45
45
  raise Exception unless DEFINITIONS.has_key?(definition)
46
- raise Exception unless mailer_class.constantize.ancestors.include? ActionMailer::Base
47
- end
48
-
49
- def delayed_delivery
50
- delay.deliver!
51
46
  end
52
47
 
53
48
  def deliver
54
49
  validate
55
- mailer_class.constantize.send(mailer_method.to_sym, malletable).deliver
50
+ Hooks.on_deliver(self)
56
51
  end
57
52
 
58
- def self.deliver_all!
59
- pending.each(&:delayed_delivery)
53
+ def error
54
+ Hooks.on_error(self)
60
55
  end
61
56
 
62
- protected
57
+ def bounce
58
+ Hooks.on_bounce(self)
59
+ end
60
+
61
+ def bounce
62
+ Hooks.on_bounce(self)
63
+ end
63
64
 
64
- def mailer_class
65
- DEFINITIONS[definition]['mailer']
65
+ def success
66
+ Hooks.on_success(self)
66
67
  end
67
68
 
68
- def mailer_method
69
- DEFINITIONS[definition]['method']
69
+ def delayed_delivery
70
+ delay.deliver!
71
+ end
72
+
73
+ def self.deliver_all!
74
+ pending.each(&:delayed_delivery)
70
75
  end
71
76
 
72
77
  end
@@ -8,7 +8,7 @@ module Mallet
8
8
  self.mail = Mail.new
9
9
  end
10
10
 
11
- def to(malletable)
11
+ def for(malletable)
12
12
  mail.malletable_type = malletable.class.name
13
13
  mail.malletable_id = malletable.id
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module Mallet
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -14,12 +14,7 @@ describe Mallet::Mail do
14
14
  context "on creation" do
15
15
 
16
16
  before(:each) do
17
- job = @job
18
-
19
- @mail = Mallet::mail do
20
- to job
21
- definition 'welcome'
22
- end
17
+ @mail = Mallet::mail(for: @job, definition: 'welcome')
23
18
  end
24
19
 
25
20
  it "should default to a pending state" do
@@ -30,56 +25,53 @@ describe Mallet::Mail do
30
25
  @mail.malletable.should == @job
31
26
  end
32
27
 
33
- it "should identify the mailer class" do
34
- @mail.send(:mailer_class).should == "ActionMailer::Base"
28
+ end
29
+
30
+ context "on delivery" do
31
+
32
+ before(:each) do
33
+ @mail = Mallet::mail(for: @job, definition: 'welcome')
35
34
  end
36
35
 
37
- it "should identify the mailer method" do
38
- @mail.send(:mailer_method).should == "welcome"
36
+ it "should mark the mail as sent" do
37
+ @mail.deliver!
38
+ @mail.should be_sent
39
39
  end
40
40
 
41
- it "should trigger sending the mail via delayed job" do
42
- mock_mailer = mock(ActionMailer::Base)
43
- ActionMailer::Base.should_receive(:welcome).with(@job).and_return(mock_mailer)
44
- mock_mailer.should_receive(:deliver)
41
+ end
45
42
 
46
- Delayed::Job.count.should == 2
47
- Delayed::Job.last.invoke_job
48
- end
43
+ context "hooks" do
49
44
 
50
- it "should raise an exception if the mailer does not inherit from ActionMailer" do
51
- Mallet::DEFINITIONS['bad_definition'] = {
52
- 'mailer' => 'ActionMailer::Base2',
53
- 'method' => 'welcome'
54
- }
45
+ it "should call a hook on creation" do
46
+ Mallet::Hooks.should_receive(:on_create).once
55
47
 
56
- @mail.definition = 'bad_definition'
57
- expect { @mail.save! }.to raise_error
48
+ mail = Mallet::mail(for: @job, definition: 'welcome')
58
49
  end
59
50
 
60
- end
61
-
62
- context "on delivery" do
51
+ it "should call a hook when delivering" do
52
+ mail = Mallet::mail(for: @job, definition: 'welcome')
53
+ Mallet::Hooks.should_receive(:on_deliver).with(mail).once
54
+ mail.deliver!
55
+ end
63
56
 
64
- before(:each) do
65
- job = @job
66
-
67
- @mail = Mallet::mail do
68
- to job
69
- definition 'welcome'
70
- end
71
-
72
- @job = job
57
+ it "should call a hook on error" do
58
+ mail = Mallet::mail(for: @job, definition: 'welcome')
59
+ Mallet::Hooks.should_receive(:on_error).with(mail).once
60
+ mail.error!
73
61
  end
74
62
 
75
- it "should mark the mail as sent" do
76
- mock_mailer = mock(ActionMailer::Base)
77
- ActionMailer::Base.should_receive(:welcome).with(@job).and_return(mock_mailer)
78
- mock_mailer.should_receive(:deliver)
63
+ it "should call a hook when delivered successfully" do
64
+ mail = Mallet::mail(for: @job, definition: 'welcome')
65
+ mail.update_column(:workflow_state, 'sent')
66
+ Mallet::Hooks.should_receive(:on_success).with(mail).once
67
+ mail.success!
68
+ end
79
69
 
80
- Delayed::Job.last.invoke_job
81
- @mail.reload
82
- @mail.should be_sent
70
+ it "should call a hook when bounced" do
71
+ mail = Mallet::mail(for: @job, definition: 'welcome')
72
+ mail.update_column(:workflow_state, 'sent')
73
+ Mallet::Hooks.should_receive(:on_bounce).with(mail).once
74
+ mail.bounce!
83
75
  end
84
76
 
85
77
  end
@@ -87,48 +79,21 @@ describe Mallet::Mail do
87
79
  context "filtering mails" do
88
80
 
89
81
  it "should filter by pending" do
90
- job = @job
91
-
92
- expect {
93
- Mallet::mail do
94
- to job
95
- definition 'welcome'
96
- end
97
- }.to change{ Mallet::Mail.pending.count }.by(1)
82
+ expect { Mallet::mail(for: @job, definition: 'welcome') }.to change{ Mallet::Mail.with_pending_state.count }.by(1)
98
83
  end
99
84
 
100
85
  it "should filter by sent" do
101
- job = @job
102
-
103
- mail = Mallet::mail do
104
- to job
105
- definition 'welcome'
106
- end
107
-
108
- expect {mail.error!}.to change{ Mallet::Mail.failed.count }.by(1)
86
+ mail = Mallet::mail(for: @job, definition: 'welcome')
87
+ expect {mail.deliver!}.to change{ Mallet::Mail.with_sent_state.count }.by(1)
109
88
  end
110
89
 
111
90
  it "should filter by failed" do
112
- job = @job
113
-
114
- mail = Mallet::mail do
115
- to job
116
- definition 'welcome'
117
- end
118
-
119
- mail.should_receive(:deliver)
120
- expect {mail.deliver!}.to change{ Mallet::Mail.sent.count }.by(1)
91
+ mail = Mallet::mail(for: @job, definition: 'welcome')
92
+ expect {mail.error!}.to change{ Mallet::Mail.with_failed_state.count }.by(1)
121
93
  end
122
94
 
123
95
  it "should filter by malletable item" do
124
- job = @job
125
-
126
- expect{
127
- mail = Mallet::mail do
128
- to job
129
- definition 'welcome'
130
- end
131
- }.to change{ Mallet::Mail.for(job).count }.by(1)
96
+ expect{ Mallet::mail(for: @job, definition: 'welcome') }.to change{ Mallet::Mail.for(@job).count }.by(1)
132
97
  end
133
98
 
134
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mallet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-05-17 00:00:00.000000000 Z
13
+ date: 2014-02-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -141,6 +141,7 @@ files:
141
141
  - lib/generators/active_record/mallet_generator.rb
142
142
  - lib/generators/active_record/templates/migration.rb
143
143
  - lib/mallet.rb
144
+ - lib/mallet/hooks.rb
144
145
  - lib/mallet/mail.rb
145
146
  - lib/mallet/mail_generation.rb
146
147
  - lib/mallet/version.rb