dallal 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/mailers/dallal/mailer.rb +1 -1
- data/lib/dallal/notification.rb +21 -30
- data/lib/dallal/notifications/base_notification.rb +25 -0
- data/lib/dallal/notifications/email_notification.rb +24 -0
- data/lib/dallal/notifications/sms_notification.rb +16 -0
- data/lib/dallal/notifiers/email_notifier.rb +0 -10
- data/lib/dallal/notifiers/notifier.rb +0 -8
- data/lib/dallal/notifiers/sms_notifier.rb +0 -1
- data/lib/dallal/version.rb +2 -2
- data/spec/dallal/notification_spec.rb +32 -25
- data/spec/dallal/notifiers/notifier_spec.rb +4 -12
- data/spec/dummy/app/views/dallal/mailer/posts/register.html.erb +1 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +27512 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53e11c8daaf54da4d759d04172b5e2ba5fcc4d1d
|
4
|
+
data.tar.gz: 69bb8738060d5c68b1ddc1c888a25aeaa873cbb2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a30fb2af03e8d7e81ce72b2cb7286a147b5191a3eeadbe26dfebf1ca83db9e52c477ecf58a4bce0b29edc773fbd0248f66b71420576168e88ba92d1455702a4
|
7
|
+
data.tar.gz: 7ae52241acef4c1e0af8c716499ac5899f3a9e534ad2222176c8ec85786b07fd12af54784c7a1073e2573a25c980a304479c35356e2b7a906fb9b724ab73ee16
|
@@ -13,7 +13,7 @@ module Dallal
|
|
13
13
|
{
|
14
14
|
from: address_format(@notification.from_email, @notification.from_name),
|
15
15
|
reply_to: address_format(@notification.from_email, @notification.from_name),
|
16
|
-
to: @notification.
|
16
|
+
to: @notification.target.email,
|
17
17
|
cc: nil,
|
18
18
|
subject: subject,
|
19
19
|
template_name: "#{object_plural_name}/#{@notification.template_name}"
|
data/lib/dallal/notification.rb
CHANGED
@@ -1,59 +1,49 @@
|
|
1
|
+
require 'dallal/notifications/email_notification'
|
2
|
+
require 'dallal/notifications/sms_notification'
|
3
|
+
|
1
4
|
module Dallal
|
2
5
|
class Notification
|
3
6
|
attr_accessor :event, :model_class, :opts, :_object
|
4
|
-
attr_reader :
|
7
|
+
attr_reader :targets
|
8
|
+
attr_reader :notifiers
|
5
9
|
|
6
10
|
def initialize args = {}
|
7
11
|
args.each do |k, v|
|
8
12
|
send("#{k}=", v)
|
9
13
|
end
|
10
14
|
|
11
|
-
@notifiers =
|
15
|
+
@notifiers = []
|
12
16
|
end
|
13
17
|
|
14
|
-
# TODO Collection of targets is not supported
|
15
18
|
def notify *args, &block
|
16
19
|
notify_opts = args.extract_options!
|
17
20
|
return unless should_send?(notify_opts[:if])
|
18
21
|
|
19
|
-
@
|
22
|
+
@targets = Array(args).flatten.compact.uniq
|
20
23
|
instance_eval(&block)
|
21
24
|
end
|
22
25
|
|
23
26
|
def with *args, &block
|
24
27
|
opts = args.extract_options!
|
25
|
-
|
26
|
-
if should_send?(opts[:if])
|
27
|
-
instance_eval(&block)
|
28
|
-
args.each { |arg| @notifiers[arg] = get_notifier(arg) }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# TODO fix this. This is a quick and dirty patch for email
|
33
|
-
def user
|
34
|
-
@target.first
|
35
|
-
end
|
36
|
-
|
37
|
-
# TODO !!! Watch out same payload for multiple notifers that
|
38
|
-
# require payload
|
39
|
-
def payload payload
|
40
|
-
@payload = payload
|
41
|
-
end
|
28
|
+
return unless should_send?(opts[:if])
|
42
29
|
|
43
|
-
|
44
|
-
|
45
|
-
|
30
|
+
# create a notifier for each target
|
31
|
+
@targets.each do |target|
|
32
|
+
args.each do |name|
|
33
|
+
notifier = get_notifier(name, target, &block)
|
34
|
+
@notifiers << notifier
|
35
|
+
end
|
36
|
+
end
|
46
37
|
end
|
47
38
|
|
48
39
|
def persist?
|
49
40
|
opts[:persist].present?
|
50
41
|
end
|
51
42
|
|
52
|
-
# TODO Add support for multiple targets
|
53
43
|
def dispatch!
|
54
44
|
validate!
|
55
|
-
@notifiers.each { |
|
56
|
-
@notifiers.each { |
|
45
|
+
@notifiers.each { |n| n.notify! }
|
46
|
+
@notifiers.each { |n| n.persist! } if persist?
|
57
47
|
end
|
58
48
|
|
59
49
|
private
|
@@ -68,9 +58,10 @@ module Dallal
|
|
68
58
|
end
|
69
59
|
end
|
70
60
|
|
71
|
-
def get_notifier(name)
|
72
|
-
|
73
|
-
|
61
|
+
def get_notifier(name, target, &block)
|
62
|
+
notification = "Dallal::Notifications::#{name.to_s.camelcase}Notification".constantize.new(self, target)
|
63
|
+
notification.instance_eval(&block)
|
64
|
+
notification.notifier
|
74
65
|
end
|
75
66
|
|
76
67
|
def should_send?(condition)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Dallal
|
2
|
+
module Notifications
|
3
|
+
class BaseNotification
|
4
|
+
attr_reader :target
|
5
|
+
|
6
|
+
def initialize(notification, target)
|
7
|
+
@notification = notification
|
8
|
+
@target = target
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(name, *args, &block)
|
12
|
+
if @notification.respond_to?(name)
|
13
|
+
@notification.send(name, *args, &block)
|
14
|
+
else
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def respond_to?(method_name, include_private=true)
|
20
|
+
@notification.respond_to?(method_name) || super
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'dallal/notifications/base_notification'
|
2
|
+
module Dallal
|
3
|
+
module Notifications
|
4
|
+
class EmailNotification < BaseNotification
|
5
|
+
attr_reader :from_name, :from_email
|
6
|
+
attr_reader :template_name
|
7
|
+
|
8
|
+
def initialize(notification, target)
|
9
|
+
super(notification, target)
|
10
|
+
|
11
|
+
@from_name = Dallal.configuration.from_name
|
12
|
+
@from_email = Dallal.configuration.from_email
|
13
|
+
end
|
14
|
+
|
15
|
+
def notifier
|
16
|
+
Dallal::Notifiers::EmailNotifier.new(self)
|
17
|
+
end
|
18
|
+
|
19
|
+
def template template
|
20
|
+
@template_name = template
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'dallal/notifications/base_notification'
|
2
|
+
module Dallal
|
3
|
+
module Notifications
|
4
|
+
class SmsNotification < BaseNotification
|
5
|
+
attr_reader :sms_payload
|
6
|
+
|
7
|
+
def payload payload
|
8
|
+
@sms_payload = payload
|
9
|
+
end
|
10
|
+
|
11
|
+
def notifier
|
12
|
+
Dallal::Notifiers::SmsNotifier.new(self)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -2,8 +2,6 @@ module Dallal
|
|
2
2
|
module Notifiers
|
3
3
|
class EmailNotifier < Notifier
|
4
4
|
def notify!
|
5
|
-
# TODO please pass a email notification instead
|
6
|
-
define_email_methods
|
7
5
|
mailer.notify(notification).deliver_now
|
8
6
|
end
|
9
7
|
|
@@ -12,14 +10,6 @@ module Dallal
|
|
12
10
|
|
13
11
|
private
|
14
12
|
|
15
|
-
def define_email_methods
|
16
|
-
[:from_email, :from_name].each do |name|
|
17
|
-
notification.define_singleton_method(name) do
|
18
|
-
Dallal.configuration.send(name)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
13
|
def mailer
|
24
14
|
Dallal::Mailer
|
25
15
|
end
|
@@ -10,14 +10,6 @@ module Dallal
|
|
10
10
|
@notification = notification
|
11
11
|
end
|
12
12
|
|
13
|
-
def self.email(notification)
|
14
|
-
Dallal::Notifiers::EmailNotifier.new(notification)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.sms(notification)
|
18
|
-
Dallal::Notifiers::SmsNotifier.new(notification)
|
19
|
-
end
|
20
|
-
|
21
13
|
def notify!(*args)
|
22
14
|
Notifier.api_not_implemented(self)
|
23
15
|
end
|
data/lib/dallal/version.rb
CHANGED
@@ -33,11 +33,11 @@ describe Dallal::Notification do
|
|
33
33
|
executed = true
|
34
34
|
end
|
35
35
|
expect(executed).to eq true
|
36
|
-
expect(subject.
|
36
|
+
expect(subject.targets).to eq [@user]
|
37
37
|
end
|
38
38
|
|
39
39
|
context 'notify with nested with block' do
|
40
|
-
it '
|
40
|
+
it 'calls with block' do
|
41
41
|
executed = false
|
42
42
|
subject.notify(@user) do
|
43
43
|
with :email do
|
@@ -46,9 +46,10 @@ describe Dallal::Notification do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
expect(executed).to be true
|
49
|
-
|
50
|
-
expect(
|
51
|
-
expect(
|
49
|
+
notifier = subject.notifiers.first
|
50
|
+
expect(notifier).to be_a(Dallal::Notifiers::EmailNotifier)
|
51
|
+
expect(notifier.notification.template_name).to eq :an_email_template
|
52
|
+
expect(subject.targets).to eq([@user])
|
52
53
|
end
|
53
54
|
|
54
55
|
it 'evaluates correctly an sms block' do
|
@@ -57,13 +58,13 @@ describe Dallal::Notification do
|
|
57
58
|
payload({ a:1, b: 2 })
|
58
59
|
end
|
59
60
|
end
|
60
|
-
|
61
|
-
expect(
|
62
|
-
expect(
|
63
|
-
expect(subject.
|
61
|
+
notifier = subject.notifiers.first
|
62
|
+
expect(notifier).to be_a(Dallal::Notifiers::SmsNotifier)
|
63
|
+
expect(notifier.notification.sms_payload).to eq({a: 1, b: 2})
|
64
|
+
expect(subject.targets).to eq([@user])
|
64
65
|
end
|
65
66
|
|
66
|
-
it '
|
67
|
+
it 'calls the block when an if lambda returns true' do
|
67
68
|
subject.define_singleton_method('post') do
|
68
69
|
@post
|
69
70
|
end
|
@@ -74,9 +75,12 @@ describe Dallal::Notification do
|
|
74
75
|
template :an_email_template
|
75
76
|
end
|
76
77
|
end
|
77
|
-
|
78
|
-
expect(subject.
|
79
|
-
|
78
|
+
|
79
|
+
expect(subject.notifiers.size).to eq 1
|
80
|
+
notifier = subject.notifiers.first
|
81
|
+
expect(notifier).to be_a Dallal::Notifiers::EmailNotifier
|
82
|
+
expect(notifier.notification.template_name).to eq :an_email_template
|
83
|
+
expect(subject.targets).to eq [@user]
|
80
84
|
end
|
81
85
|
|
82
86
|
it 'does not call the block when if lambda evaluates to false' do
|
@@ -88,17 +92,17 @@ describe Dallal::Notification do
|
|
88
92
|
subject.notify(@user, if: -> (){ post.id == -100} ) do
|
89
93
|
raise "BLock should not be executed"
|
90
94
|
with :email do
|
95
|
+
raise "BLock should not be executed"
|
91
96
|
template :an_email_template
|
92
97
|
end
|
93
98
|
end
|
94
|
-
expect(subject.
|
95
|
-
expect(subject.
|
96
|
-
expect(subject.instance_variable_get(:@target)).to eq nil
|
99
|
+
expect(subject.notifiers).to be_empty
|
100
|
+
expect(subject.targets).to be nil
|
97
101
|
end
|
98
102
|
end
|
99
103
|
|
100
104
|
context "multiple notifiers" do
|
101
|
-
it '' do
|
105
|
+
it 'fills properties correctly' do
|
102
106
|
subject.notify(@user) do
|
103
107
|
with :email do
|
104
108
|
template :email_template
|
@@ -107,10 +111,14 @@ describe Dallal::Notification do
|
|
107
111
|
payload a: 1, b: 2
|
108
112
|
end
|
109
113
|
end
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
expect(subject.
|
114
|
+
email_notifier = subject.notifiers.first
|
115
|
+
sms_notifier = subject.notifiers.last
|
116
|
+
|
117
|
+
expect(subject.notifiers.size).to eq 2
|
118
|
+
expect(email_notifier).to be_a(Dallal::Notifiers::EmailNotifier)
|
119
|
+
expect(email_notifier.notification.template_name).to eq :email_template
|
120
|
+
expect(sms_notifier).to be_a(Dallal::Notifiers::SmsNotifier)
|
121
|
+
expect(sms_notifier.notification.sms_payload).to eq(a: 1, b: 2)
|
114
122
|
end
|
115
123
|
end
|
116
124
|
end
|
@@ -124,10 +132,9 @@ describe Dallal::Notification do
|
|
124
132
|
template :a
|
125
133
|
end
|
126
134
|
end
|
127
|
-
expect(subject.
|
128
|
-
expect(subject.
|
129
|
-
expect(subject.
|
130
|
-
expect(subject.instance_variable_get(:@notifiers)[:email]).to_not receive(:persist!)
|
135
|
+
expect(subject.notifiers.size).to eq 1
|
136
|
+
expect(subject.notifiers.first).to receive(:notify!)
|
137
|
+
expect(subject.notifiers.first).to_not receive(:persist!)
|
131
138
|
|
132
139
|
subject.dispatch!
|
133
140
|
end
|
@@ -5,20 +5,12 @@ describe Dallal::Notifiers::Notifier do
|
|
5
5
|
subject { Dallal::Notifiers::Notifier.new(notification) }
|
6
6
|
it { expect(subject).to be_a Dallal::AbstractInterface }
|
7
7
|
|
8
|
-
|
9
|
-
it '
|
10
|
-
|
11
|
-
expect(result).to be_a(Dallal::Notifiers::SmsNotifier)
|
12
|
-
expect(result.notification).to eq notification
|
13
|
-
end
|
14
|
-
end
|
15
|
-
context '.email' do
|
16
|
-
it 'returns an email notifier' do
|
17
|
-
result = Dallal::Notifiers::Notifier.email(notification)
|
18
|
-
expect(result).to be_a(Dallal::Notifiers::EmailNotifier)
|
19
|
-
expect(result.notification).to eq notification
|
8
|
+
describe "#persist!" do
|
9
|
+
it 'should raise an error' do
|
10
|
+
expect{ subject.persist! }.to raise_error(Dallal::AbstractInterface::InterfaceNotImplementedError)
|
20
11
|
end
|
21
12
|
end
|
13
|
+
|
22
14
|
describe "#notify" do
|
23
15
|
it 'should raise an error' do
|
24
16
|
expect{ subject.notify! }.to raise_error(Dallal::AbstractInterface::InterfaceNotImplementedError)
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|