dallal 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef7931f202a80463bfca59f1e2d4a32b74cf7114
4
- data.tar.gz: 8c243ab925cdab39f98c364b92994bd260244da5
3
+ metadata.gz: 332a7870ab01d845102d4439b7687269a55c6446
4
+ data.tar.gz: 2b564c24671a2badf45e3ede17d55aa6165270b9
5
5
  SHA512:
6
- metadata.gz: 283eff9f49bacda0ce3a827515adc71c821b792b6bbd8a3e8c8a5264da389890feae70026fa9ad81f8335538ac59499fbf6b5be6e259bb5539ac6d5539804149
7
- data.tar.gz: 4258db0fff6f2ba0a416cbd9cdaec87a7cc0cac597cc8e9f6de030e4258ba985fed2aa713d25e52970cb46ea13354a1518768d79a63d5b159642bfddfe4fc1db
6
+ metadata.gz: 8cf61158702241cc1c7c1f2a26d2cef32f4f9cbff7db2b5ae9f605424c744084fe46c97ed926ac1ceb92df70ab684a26c566c6b43a7f9d0e2fb91a50b8fb02a3
7
+ data.tar.gz: cb56f9645b9a346a5c41e676f29b81aa8d6d85c87f747b1544628f3cbd19206482d6f3c8d2f02c3d6ad39488c09029546bfe4babbc86e679845bb307104f7226
@@ -14,7 +14,6 @@ module Dallal
14
14
 
15
15
  def execute
16
16
  if should_create_notifications?
17
- # Trigger an ActiJob for this notification
18
17
  DallalJob.perform_later payload[:class], payload[:id], payload[:event].to_s
19
18
  end
20
19
  end
@@ -11,19 +11,25 @@ module Dallal
11
11
  @notifiers = {}
12
12
  end
13
13
 
14
- def notify target, &block
15
- @target = Array(target).flatten.compact.uniq
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
- _object.instance_exec(&condition)
80
+ instance_exec(&condition)
74
81
  else
75
82
  _object.send(condition)
76
83
  end
@@ -1,3 +1,7 @@
1
1
  module Dallal
2
- VERSION = "0.0.1"
2
+ MAJOR = 0
3
+ MINOR = 1
4
+ PATCH = 1
5
+
6
+ VERSION = [MAJOR, MINOR, PATCH].compact.join(".")
3
7
  end
@@ -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: 1, b: 2 do
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: 1, b: 2})
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 = -> () { a_method }
189
+ _proc = ->(){ post.id.present? }
144
190
 
145
- subject._object = double("SomeObject")
146
191
  result = double("Result")
147
192
 
148
- expect(subject._object).to receive(:a_method).and_return(result)
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
Binary file