dallal 0.0.1 → 0.1.1
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.
- checksums.yaml +4 -4
- data/lib/dallal/events/event_subscriber.rb +0 -1
- data/lib/dallal/notification.rb +10 -3
- data/lib/dallal/version.rb +5 -1
- data/spec/dallal/events/observer_spec.rb +51 -2
- data/spec/dallal/notification_spec.rb +48 -3
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +25488 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 332a7870ab01d845102d4439b7687269a55c6446
|
4
|
+
data.tar.gz: 2b564c24671a2badf45e3ede17d55aa6165270b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cf61158702241cc1c7c1f2a26d2cef32f4f9cbff7db2b5ae9f605424c744084fe46c97ed926ac1ceb92df70ab684a26c566c6b43a7f9d0e2fb91a50b8fb02a3
|
7
|
+
data.tar.gz: cb56f9645b9a346a5c41e676f29b81aa8d6d85c87f747b1544628f3cbd19206482d6f3c8d2f02c3d6ad39488c09029546bfe4babbc86e679845bb307104f7226
|
data/lib/dallal/notification.rb
CHANGED
@@ -11,19 +11,25 @@ module Dallal
|
|
11
11
|
@notifiers = {}
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
# TODO Collection of targets is not supported
|
15
|
+
def notify *args, &block
|
16
|
+
notify_opts = args.extract_options!
|
17
|
+
return unless should_send?(notify_opts[:if])
|
18
|
+
|
19
|
+
@target = Array(args).flatten.compact.uniq
|
16
20
|
instance_eval(&block)
|
17
21
|
end
|
18
22
|
|
19
23
|
def with *args, &block
|
20
24
|
opts = args.extract_options!
|
25
|
+
# TODO Move if condition to notify method
|
21
26
|
if should_send?(opts[:if])
|
22
27
|
instance_eval(&block)
|
23
28
|
args.each { |arg| @notifiers[arg] = get_notifier(arg) }
|
24
29
|
end
|
25
30
|
end
|
26
31
|
|
32
|
+
# TODO fix this. This is a quick and dirty patch for email
|
27
33
|
def user
|
28
34
|
@target.first
|
29
35
|
end
|
@@ -43,6 +49,7 @@ module Dallal
|
|
43
49
|
opts[:persist].present?
|
44
50
|
end
|
45
51
|
|
52
|
+
# TODO Add support for multiple targets
|
46
53
|
def dispatch!
|
47
54
|
validate!
|
48
55
|
@notifiers.each { |_, n| n.notify! }
|
@@ -70,7 +77,7 @@ module Dallal
|
|
70
77
|
return true if condition.blank?
|
71
78
|
|
72
79
|
if condition.is_a?(Proc)
|
73
|
-
|
80
|
+
instance_exec(&condition)
|
74
81
|
else
|
75
82
|
_object.send(condition)
|
76
83
|
end
|
data/lib/dallal/version.rb
CHANGED
@@ -23,7 +23,7 @@ describe Dallal::Events::Observer do
|
|
23
23
|
expect(subject.callbacks.size).to eq 0
|
24
24
|
executed = false
|
25
25
|
|
26
|
-
subject.on :create, a:
|
26
|
+
subject.on :create, {a: 2, b: 2} do
|
27
27
|
executed = true
|
28
28
|
end
|
29
29
|
|
@@ -31,9 +31,36 @@ describe Dallal::Events::Observer do
|
|
31
31
|
expect(subject.callbacks.size).to eq 1
|
32
32
|
callback = subject.callbacks[0]
|
33
33
|
expect(callback[:on]).to eq([:create])
|
34
|
-
expect(callback[:opts]).to eq({a:
|
34
|
+
expect(callback[:opts]).to eq({a: 2, b: 2})
|
35
35
|
expect(callback[:block]).to be_a(Proc)
|
36
36
|
end
|
37
|
+
context 'multiple definitions' do
|
38
|
+
before { subject.instance_variable_set(:@__notification_callbacks, []) }
|
39
|
+
it 'append all definitions to callbacks' do
|
40
|
+
first_executed = false
|
41
|
+
second_executed = false
|
42
|
+
subject.on :create, a: 1 do
|
43
|
+
first_executed = true
|
44
|
+
end
|
45
|
+
|
46
|
+
subject.on :create, b: 2 do
|
47
|
+
second_executed = true
|
48
|
+
end
|
49
|
+
|
50
|
+
expect(first_executed).to be false
|
51
|
+
expect(second_executed).to be false
|
52
|
+
expect(subject.callbacks.size).to eq 2
|
53
|
+
first_callback = subject.callbacks.first
|
54
|
+
second_callback = subject.callbacks.last
|
55
|
+
expect(first_callback[:on]).to eq [:create]
|
56
|
+
expect(first_callback[:opts]).to eq a: 1
|
57
|
+
expect(first_callback[:block]).to be_a Proc
|
58
|
+
|
59
|
+
expect(second_callback[:on]).to eq [:create]
|
60
|
+
expect(second_callback[:opts]).to eq b: 2
|
61
|
+
expect(second_callback[:block]).to be_a Proc
|
62
|
+
end
|
63
|
+
end
|
37
64
|
end
|
38
65
|
|
39
66
|
describe '.create_notification' do
|
@@ -92,6 +119,28 @@ describe Dallal::Events::Observer do
|
|
92
119
|
expect(notification.user).to eq @user
|
93
120
|
end
|
94
121
|
end
|
122
|
+
|
123
|
+
context "mutliple events" do
|
124
|
+
before { subject.instance_variable_set(:@__notification_callbacks, []) }
|
125
|
+
it 'calls all events' do
|
126
|
+
first_executed = false
|
127
|
+
second_executed = false
|
128
|
+
|
129
|
+
subject.on :create, a: 1 do
|
130
|
+
first_executed = true
|
131
|
+
end
|
132
|
+
|
133
|
+
subject.on :create, b: 2 do
|
134
|
+
second_executed = true
|
135
|
+
end
|
136
|
+
|
137
|
+
notification = double('Dallal::Notification')
|
138
|
+
expect(Dallal::Notification).to receive(:new).with(event: :create, model_class: 'User', opts: { a: 1 }, _object: @user).and_return(notification)
|
139
|
+
expect(Dallal::Notification).to receive(:new).with(event: :create, model_class: 'User', opts: { b: 2 }, _object: @user).and_return(notification)
|
140
|
+
expect(notification).to receive(:dispatch!).exactly(2).times
|
141
|
+
subject.create_notification(id: @user.id, event: :create)
|
142
|
+
end
|
143
|
+
end
|
95
144
|
end
|
96
145
|
|
97
146
|
context 'when called with an event that is not defined' do
|
@@ -26,6 +26,7 @@ describe Dallal::Notification do
|
|
26
26
|
subject do
|
27
27
|
Dallal::Notification.new(event: :create, model_class: 'Post', opts: { a: 1 }, _object: @post)
|
28
28
|
end
|
29
|
+
|
29
30
|
it 'should get the target(s) and call the block' do
|
30
31
|
executed = false
|
31
32
|
subject.notify(@user) do
|
@@ -61,7 +62,41 @@ describe Dallal::Notification do
|
|
61
62
|
expect(subject.instance_variable_get(:@target)).to eq([@user])
|
62
63
|
expect(subject.instance_variable_get(:@notifiers)[:sms]).to be_a(Dallal::Notifiers::SmsNotifier)
|
63
64
|
end
|
65
|
+
|
66
|
+
it 'evaluates correctly when an if lambda returns true' do
|
67
|
+
subject.define_singleton_method('post') do
|
68
|
+
@post
|
69
|
+
end
|
70
|
+
allow(subject).to receive(:post).and_return(@post)
|
71
|
+
|
72
|
+
subject.notify(@user, if: -> (){ post.id.present?} ) do
|
73
|
+
with :email do
|
74
|
+
template :an_email_template
|
75
|
+
end
|
76
|
+
end
|
77
|
+
expect(subject.instance_variable_get(:@template_name)).to eq :an_email_template
|
78
|
+
expect(subject.instance_variable_get(:@notifiers)[:email]).to be_a(Dallal::Notifiers::EmailNotifier)
|
79
|
+
expect(subject.instance_variable_get(:@target)).to eq([@user])
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'does not call the block when if lambda evaluates to false' do
|
83
|
+
subject.define_singleton_method('post') do
|
84
|
+
@post
|
85
|
+
end
|
86
|
+
allow(subject).to receive(:post).and_return(@post)
|
87
|
+
|
88
|
+
subject.notify(@user, if: -> (){ post.id == -100} ) do
|
89
|
+
raise "BLock should not be executed"
|
90
|
+
with :email do
|
91
|
+
template :an_email_template
|
92
|
+
end
|
93
|
+
end
|
94
|
+
expect(subject.instance_variable_get(:@template_name)).to eq nil
|
95
|
+
expect(subject.instance_variable_get(:@notifiers)[:email]).to eq nil
|
96
|
+
expect(subject.instance_variable_get(:@target)).to eq nil
|
97
|
+
end
|
64
98
|
end
|
99
|
+
|
65
100
|
context "multiple notifiers" do
|
66
101
|
it '' do
|
67
102
|
subject.notify(@user) do
|
@@ -126,6 +161,17 @@ describe Dallal::Notification do
|
|
126
161
|
end
|
127
162
|
|
128
163
|
describe '#should_send?' do
|
164
|
+
subject do
|
165
|
+
Dallal::Notification.new(event: :create, model_class: 'Post', opts: { a: 1 }, _object: @post)
|
166
|
+
end
|
167
|
+
before do
|
168
|
+
subject.define_singleton_method('post') do
|
169
|
+
@post
|
170
|
+
end
|
171
|
+
allow(subject).to receive(:post).and_return(@post)
|
172
|
+
end
|
173
|
+
|
174
|
+
|
129
175
|
it 'return true on blank values' do
|
130
176
|
[nil, false, ''].each do |val|
|
131
177
|
expect(subject.send(:should_send?, val)).to be true
|
@@ -140,12 +186,11 @@ describe Dallal::Notification do
|
|
140
186
|
end
|
141
187
|
|
142
188
|
it 'evaluates a proc to object instance' do
|
143
|
-
_proc = ->
|
189
|
+
_proc = ->(){ post.id.present? }
|
144
190
|
|
145
|
-
subject._object = double("SomeObject")
|
146
191
|
result = double("Result")
|
147
192
|
|
148
|
-
expect(subject.
|
193
|
+
expect(subject.post).to receive_message_chain(:id, :present?).and_return(result)
|
149
194
|
expect(subject.send(:should_send?, _proc)).to eq result
|
150
195
|
end
|
151
196
|
end
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|