triggerable 0.1.8 → 0.1.9

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: 4675b09599d91ea600d5b96c346746323a4c99b0
4
- data.tar.gz: 057af4afa9a42e2fa9a09eb02d3bf728d115f6ec
3
+ metadata.gz: 5cd47642923aee94d86401397eeff3693d8f2fd7
4
+ data.tar.gz: 372ca92aa502e5aea89488a98521564417443127
5
5
  SHA512:
6
- metadata.gz: 3df5712b4a6e059cfbd2a7e5c5f90d7cbbbe4941f2ee761d08c84a58b0bc6ac0dba60b2089c54cb724f24a0d180fc6a0c3f5f00b99b72e4fb1f21dc5979cd136
7
- data.tar.gz: 88c14b4067cba8a5816c1d93c2987b101631c7bc4869f9bba42e8ea757593adce708fedeb316e203b2bb2ae9c391eea449c5bc23b658e978ec1ed451bec2161c
6
+ metadata.gz: 40b665fd572e4e9eaa47494eec9be8dc725bca110ad32fdfc65412afe0a542342e8c12c88f441002980f3f3eb14a300db2cd9f90d95a5fc0ad6b38f1e3bc4134
7
+ data.tar.gz: 7fba9adbd6596dc8f3fdd1e600d704b078db034b76c213acc03575096262e9e62106c91e71cb5cbb2c0de29fa883d77c4ae52525eca49313b9865a9e4c2eaa84
data/README.md CHANGED
@@ -58,6 +58,18 @@ trigger on: :after_create, if: { or: [{ field1: '1' }, { field2: 1 }] }, ...
58
58
 
59
59
  Triggerable does not run automations by itself, you should call `Triggerable::Engine.run_automations(interval)` using any scheduling script. Interval is a time difference between calling the method (e.g. `1.hour`). *You should avoid situations when your interval is less then the time your automations need to complete!*
60
60
 
61
+ Automation calls action block for each found object, but it's possible to pass a relation to action block using `pass_relation` option:
62
+
63
+ ```ruby
64
+ class User < ActiveRecord::Base
65
+ automation if: {
66
+ created_at: { after: 24.hours }, confirmed: false
67
+ }, pass_relation: true do
68
+ each(&:send_confirmation_email)
69
+ end
70
+ end
71
+ ```
72
+
61
73
  If you have more complex condition or need to check associations (not supported in DSL now), you should use a lambda condition form:
62
74
 
63
75
  ```ruby
@@ -1,18 +1,23 @@
1
1
  module Triggerable
2
2
  module Rules
3
3
  class Automation < Rule
4
+ attr_accessor :pass_relation
5
+
6
+ def initialize model, options, block
7
+ super(model, options, block)
8
+ @pass_relation = options[:pass_relation]
9
+ end
10
+
4
11
  def execute!
5
12
  ids = ActiveRecord::Base.connection.execute(build_query).map { |r| r['id'] }
6
13
  models = model.where(id: ids)
7
14
 
8
15
  Triggerable::Engine.log(:debug, "#{desc}: processing #{models.count} object(s)")
9
16
 
10
- models.each do |object|
11
- begin
12
- actions.each {|a| a.run_for!(object, name)}
13
- rescue Exception => ex
14
- Triggerable::Engine.log(:error, "#{desc} failed with exception #{ex}")
15
- end
17
+ if @pass_relation
18
+ execute_on!(models)
19
+ else
20
+ models.each { |object| execute_on!(object) }
16
21
  end
17
22
  end
18
23
 
@@ -28,6 +33,14 @@ module Triggerable
28
33
 
29
34
  query
30
35
  end
36
+
37
+ def execute_on!(target)
38
+ begin
39
+ actions.each {|a| a.run_for!(target, name)}
40
+ rescue Exception => ex
41
+ Triggerable::Engine.log(:error, "#{desc} failed with exception #{ex}")
42
+ end
43
+ end
31
44
  end
32
45
  end
33
46
  end
@@ -1,3 +1,3 @@
1
1
  module Triggerable
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
@@ -194,4 +194,33 @@ describe 'Automations' do
194
194
  Triggerable::Engine.run_automations(15.minutes)
195
195
  expect(TestTask.count).to eq(2)
196
196
  end
197
+
198
+ it 'can pass relation to action block' do
199
+ constantize_time_now Time.utc 2012, 9, 1, 12, 00
200
+
201
+ TestTask.automation if: {
202
+ and: [
203
+ { updated_at: { after: 24.hours } },
204
+ { status: { is: :solved } },
205
+ { kind: { is: :service } }
206
+ ]
207
+ }, pass_relation: true do
208
+ raise 'error' if count < 2
209
+ update_all(kind: 'other')
210
+ end
211
+
212
+ task1 = TestTask.create
213
+ task2 = TestTask.create
214
+ task1.update_attributes status: 'solved', kind: 'service'
215
+ task2.update_attributes status: 'solved', kind: 'service'
216
+ expect(TestTask.count).to eq(2)
217
+
218
+ constantize_time_now Time.utc 2012, 9, 2, 12, 00
219
+ Triggerable::Engine.run_automations(1.hour)
220
+
221
+ task1.reload
222
+ expect(task1.kind).to eq('other')
223
+ task2.reload
224
+ expect(task2.kind).to eq('other')
225
+ end
197
226
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: triggerable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev