triggerable 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -4
- data/lib/triggerable.rb +9 -8
- data/lib/triggerable/actions/action.rb +18 -0
- data/lib/triggerable/actions/lambda_action.rb +28 -0
- data/lib/triggerable/conditions/condition.rb +33 -31
- data/lib/triggerable/conditions/field/exists.rb +14 -8
- data/lib/triggerable/conditions/field/field_condition.rb +27 -21
- data/lib/triggerable/conditions/field/in.rb +18 -12
- data/lib/triggerable/conditions/field/or_equal_to.rb +13 -11
- data/lib/triggerable/conditions/lambda_condition.rb +14 -8
- data/lib/triggerable/conditions/method_condition.rb +13 -7
- data/lib/triggerable/conditions/predicate/and.rb +10 -4
- data/lib/triggerable/conditions/predicate/or.rb +13 -4
- data/lib/triggerable/conditions/predicate/predicate_condition.rb +34 -32
- data/lib/triggerable/conditions/schedule/after.rb +17 -15
- data/lib/triggerable/conditions/schedule/before.rb +17 -15
- data/lib/triggerable/conditions/schedule/schedule_condition.rb +20 -18
- data/lib/triggerable/engine.rb +33 -20
- data/lib/triggerable/rules/automation.rb +31 -11
- data/lib/triggerable/rules/rule.rb +21 -9
- data/lib/triggerable/rules/trigger.rb +19 -9
- data/lib/triggerable/version.rb +1 -1
- data/spec/conditions_spec.rb +13 -13
- data/spec/integration/actions_spec.rb +9 -5
- data/spec/integration/automations_spec.rb +20 -20
- data/spec/integration/conditions_spec.rb +2 -2
- data/spec/integration/short_syntax_spec.rb +1 -1
- data/spec/scopes_spec.rb +10 -10
- data/triggerable.gemspec +1 -1
- metadata +4 -3
- data/lib/triggerable/actions.rb +0 -41
@@ -2,18 +2,20 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe 'Actions' do
|
4
4
|
before(:each) do
|
5
|
-
Engine.clear
|
5
|
+
Triggerable::Engine.clear
|
6
6
|
TestTask.destroy_all
|
7
7
|
end
|
8
8
|
|
9
|
-
class CreateFollowUp < Triggerable::Action
|
10
|
-
def run_for!
|
9
|
+
class CreateFollowUp < Triggerable::Actions::Action
|
10
|
+
def run_for! task, rule_name
|
11
11
|
TestTask.create kind: 'follow up'
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'custom action' do
|
16
|
-
TestTask.trigger on: :after_update,
|
16
|
+
TestTask.trigger on: :after_update,
|
17
|
+
if: { status: 'solved' },
|
18
|
+
do: :create_follow_up
|
17
19
|
|
18
20
|
task = TestTask.create
|
19
21
|
expect(TestTask.count).to eq(1)
|
@@ -24,7 +26,9 @@ describe 'Actions' do
|
|
24
26
|
end
|
25
27
|
|
26
28
|
it 'custom action chain' do
|
27
|
-
TestTask.trigger on: :after_update,
|
29
|
+
TestTask.trigger on: :after_update,
|
30
|
+
if: { status: 'solved' },
|
31
|
+
do: [:create_follow_up, :create_follow_up]
|
28
32
|
|
29
33
|
task = TestTask.create
|
30
34
|
expect(TestTask.count).to eq(1)
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe 'Automations' do
|
4
4
|
before(:each) do
|
5
|
-
Engine.clear
|
5
|
+
Triggerable::Engine.clear
|
6
6
|
TestTask.destroy_all
|
7
7
|
end
|
8
8
|
|
@@ -19,11 +19,11 @@ describe 'Automations' do
|
|
19
19
|
expect(TestTask.count).to eq(1)
|
20
20
|
|
21
21
|
constantize_time_now Time.utc 2012, 9, 1, 20, 00
|
22
|
-
Engine.run_automations(1.hour)
|
22
|
+
Triggerable::Engine.run_automations(1.hour)
|
23
23
|
expect(TestTask.count).to eq(1)
|
24
24
|
|
25
25
|
constantize_time_now Time.utc 2012, 9, 2, 12, 00
|
26
|
-
Engine.run_automations(1.hour)
|
26
|
+
Triggerable::Engine.run_automations(1.hour)
|
27
27
|
|
28
28
|
expect(TestTask.count).to eq(2)
|
29
29
|
expect(TestTask.all.last.kind).to eq('follow up')
|
@@ -42,11 +42,11 @@ describe 'Automations' do
|
|
42
42
|
expect(TestTask.count).to eq(1)
|
43
43
|
|
44
44
|
constantize_time_now Time.utc 2012, 9, 1, 15, 00
|
45
|
-
Engine.run_automations(1.hour)
|
45
|
+
Triggerable::Engine.run_automations(1.hour)
|
46
46
|
expect(TestTask.count).to eq(1)
|
47
47
|
|
48
48
|
constantize_time_now Time.utc 2012, 9, 1, 18, 00
|
49
|
-
Engine.run_automations(1.hour)
|
49
|
+
Triggerable::Engine.run_automations(1.hour)
|
50
50
|
expect(TestTask.count).to eq(2)
|
51
51
|
expect(TestTask.all.last.kind).to eq('follow up')
|
52
52
|
end
|
@@ -64,11 +64,11 @@ describe 'Automations' do
|
|
64
64
|
expect(TestTask.count).to eq(1)
|
65
65
|
|
66
66
|
constantize_time_now Time.utc 2012, 9, 1, 12, 29
|
67
|
-
Engine.run_automations(30.minutes)
|
67
|
+
Triggerable::Engine.run_automations(30.minutes)
|
68
68
|
expect(TestTask.count).to eq(1)
|
69
69
|
|
70
70
|
constantize_time_now Time.utc 2012, 9, 1, 12, 30
|
71
|
-
Engine.run_automations(30.minutes)
|
71
|
+
Triggerable::Engine.run_automations(30.minutes)
|
72
72
|
|
73
73
|
expect(TestTask.count).to eq(2)
|
74
74
|
expect(TestTask.all.last.kind).to eq('follow up')
|
@@ -86,11 +86,11 @@ describe 'Automations' do
|
|
86
86
|
expect(TestTask.count).to eq(1)
|
87
87
|
|
88
88
|
constantize_time_now Time.utc 2012, 9, 1, 14, 31
|
89
|
-
Engine.run_automations(30.minutes)
|
89
|
+
Triggerable::Engine.run_automations(30.minutes)
|
90
90
|
expect(TestTask.count).to eq(1)
|
91
91
|
|
92
92
|
constantize_time_now Time.utc 2012, 9, 1, 15, 03
|
93
|
-
Engine.run_automations(30.minutes)
|
93
|
+
Triggerable::Engine.run_automations(30.minutes)
|
94
94
|
expect(TestTask.count).to eq(2)
|
95
95
|
expect(TestTask.all.last.kind).to eq('follow up')
|
96
96
|
end
|
@@ -108,11 +108,11 @@ describe 'Automations' do
|
|
108
108
|
expect(TestTask.count).to eq(1)
|
109
109
|
|
110
110
|
constantize_time_now Time.utc 2012, 9, 1, 12, 00
|
111
|
-
Engine.run_automations(4.hours)
|
111
|
+
Triggerable::Engine.run_automations(4.hours)
|
112
112
|
expect(TestTask.count).to eq(1)
|
113
113
|
|
114
114
|
constantize_time_now Time.utc 2012, 9, 1, 16, 00
|
115
|
-
Engine.run_automations(4.hours)
|
115
|
+
Triggerable::Engine.run_automations(4.hours)
|
116
116
|
|
117
117
|
expect(TestTask.count).to eq(2)
|
118
118
|
expect(TestTask.all.last.kind).to eq('follow up')
|
@@ -130,11 +130,11 @@ describe 'Automations' do
|
|
130
130
|
expect(TestTask.count).to eq(1)
|
131
131
|
|
132
132
|
constantize_time_now Time.utc 2012, 9, 1, 04, 05
|
133
|
-
Engine.run_automations(2.hour)
|
133
|
+
Triggerable::Engine.run_automations(2.hour)
|
134
134
|
expect(TestTask.count).to eq(1)
|
135
135
|
|
136
136
|
constantize_time_now Time.utc 2012, 9, 1, 10, 01
|
137
|
-
Engine.run_automations(2.hour)
|
137
|
+
Triggerable::Engine.run_automations(2.hour)
|
138
138
|
expect(TestTask.count).to eq(2)
|
139
139
|
expect(TestTask.all.last.kind).to eq('follow up')
|
140
140
|
end
|
@@ -152,21 +152,21 @@ describe 'Automations' do
|
|
152
152
|
expect(TestTask.count).to eq(1)
|
153
153
|
|
154
154
|
constantize_time_now Time.utc 2012, 9, 1, 11, 46
|
155
|
-
Engine.run_automations(15.minutes)
|
155
|
+
Triggerable::Engine.run_automations(15.minutes)
|
156
156
|
expect(TestTask.count).to eq(1)
|
157
157
|
|
158
158
|
constantize_time_now Time.utc 2012, 9, 1, 13, 46
|
159
|
-
Engine.run_automations(15.minutes)
|
159
|
+
Triggerable::Engine.run_automations(15.minutes)
|
160
160
|
expect(TestTask.count).to eq(1)
|
161
161
|
|
162
162
|
constantize_time_now Time.utc 2012, 9, 1, 14, 02
|
163
|
-
Engine.run_automations(15.minutes)
|
163
|
+
Triggerable::Engine.run_automations(15.minutes)
|
164
164
|
|
165
165
|
expect(TestTask.count).to eq(2)
|
166
166
|
expect(TestTask.all.last.kind).to eq('follow up')
|
167
167
|
|
168
168
|
constantize_time_now Time.utc 2012, 9, 1, 14, 17
|
169
|
-
Engine.run_automations(15.minutes)
|
169
|
+
Triggerable::Engine.run_automations(15.minutes)
|
170
170
|
expect(TestTask.count).to eq(2)
|
171
171
|
end
|
172
172
|
|
@@ -182,16 +182,16 @@ describe 'Automations' do
|
|
182
182
|
expect(TestTask.count).to eq(1)
|
183
183
|
|
184
184
|
constantize_time_now Time.utc 2012, 9, 1, 10, 05
|
185
|
-
Engine.run_automations(15.minutes)
|
185
|
+
Triggerable::Engine.run_automations(15.minutes)
|
186
186
|
expect(TestTask.count).to eq(1)
|
187
187
|
|
188
188
|
constantize_time_now Time.utc 2012, 9, 1, 13, 30
|
189
|
-
Engine.run_automations(15.minutes)
|
189
|
+
Triggerable::Engine.run_automations(15.minutes)
|
190
190
|
expect(TestTask.count).to eq(2)
|
191
191
|
expect(TestTask.all.last.kind).to eq('follow up')
|
192
192
|
|
193
193
|
constantize_time_now Time.utc 2012, 9, 1, 15, 32
|
194
|
-
Engine.run_automations(15.minutes)
|
194
|
+
Triggerable::Engine.run_automations(15.minutes)
|
195
195
|
expect(TestTask.count).to eq(2)
|
196
196
|
end
|
197
197
|
end
|
data/spec/scopes_spec.rb
CHANGED
@@ -19,37 +19,37 @@ describe 'Scopes' do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'is' do
|
22
|
-
ids = model_ids_for(Conditions::Is, 1)
|
22
|
+
ids = model_ids_for(Triggerable::Conditions::Is, 1)
|
23
23
|
expect(ids).to eq([@m1.id, @m3.id])
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'greater than' do
|
27
|
-
ids = model_ids_for(Conditions::GreaterThan, 1)
|
27
|
+
ids = model_ids_for(Triggerable::Conditions::GreaterThan, 1)
|
28
28
|
expect(ids).to eq([@m2.id])
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'less than' do
|
32
|
-
ids = model_ids_for(Conditions::LessThan, 2)
|
32
|
+
ids = model_ids_for(Triggerable::Conditions::LessThan, 2)
|
33
33
|
expect(ids).to eq([@m1.id, @m3.id])
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'is not' do
|
37
|
-
ids = model_ids_for(Conditions::IsNot, 2)
|
37
|
+
ids = model_ids_for(Triggerable::Conditions::IsNot, 2)
|
38
38
|
expect(ids).to eq([@m1.id, @m3.id])
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'greater than or equal' do
|
42
|
-
ids = model_ids_for(Conditions::LessThanOrEqualTo, 2)
|
42
|
+
ids = model_ids_for(Triggerable::Conditions::LessThanOrEqualTo, 2)
|
43
43
|
expect(ids).to eq([@m1.id, @m2.id, @m3.id])
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'less than or equal' do
|
47
|
-
ids = model_ids_for(Conditions::GreaterThanOrEqualTo, 1)
|
47
|
+
ids = model_ids_for(Triggerable::Conditions::GreaterThanOrEqualTo, 1)
|
48
48
|
expect(ids).to eq([@m1.id, @m2.id, @m3.id])
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'in' do
|
52
|
-
ids = model_ids_for(Conditions::In, [1, 2])
|
52
|
+
ids = model_ids_for(Triggerable::Conditions::In, [1, 2])
|
53
53
|
expect(ids).to eq([@m1.id, @m2.id, @m3.id])
|
54
54
|
end
|
55
55
|
end
|
@@ -57,7 +57,7 @@ describe 'Scopes' do
|
|
57
57
|
context 'predicates' do
|
58
58
|
def model_ids_for condition_class, conditions
|
59
59
|
and_condition = condition_class.new([])
|
60
|
-
and_condition.conditions = [Conditions::Is.new(:integer_field, 1), Conditions::Is.new(:string_field, 'c')]
|
60
|
+
and_condition.conditions = [Triggerable::Conditions::Is.new(:integer_field, 1), Triggerable::Conditions::Is.new(:string_field, 'c')]
|
61
61
|
|
62
62
|
table = Arel::Table.new(:parent_models)
|
63
63
|
query = table.where(and_condition.scope(table)).project(Arel.sql('id')).to_sql
|
@@ -66,12 +66,12 @@ describe 'Scopes' do
|
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'and' do
|
69
|
-
ids = model_ids_for Conditions::And, [Conditions::Is.new(:integer_field, 1), Conditions::Is.new(:string_field, 'c')]
|
69
|
+
ids = model_ids_for Triggerable::Conditions::And, [Triggerable::Conditions::Is.new(:integer_field, 1), Triggerable::Conditions::Is.new(:string_field, 'c')]
|
70
70
|
expect(ids).to eq([@m3.id])
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'or' do
|
74
|
-
ids = model_ids_for Conditions::Or, [Conditions::Is.new(:integer_field, 1), Conditions::Is.new(:string_field, 'c')]
|
74
|
+
ids = model_ids_for Triggerable::Conditions::Or, [Triggerable::Conditions::Is.new(:integer_field, 1), Triggerable::Conditions::Is.new(:string_field, 'c')]
|
75
75
|
expect(ids).to eq([@m1.id, @m3.id])
|
76
76
|
end
|
77
77
|
end
|
data/triggerable.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec
|
18
|
+
spec.test_files = Dir['spec/**/*.rb']
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: triggerable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -124,7 +124,8 @@ files:
|
|
124
124
|
- Rakefile
|
125
125
|
- lib/.DS_Store
|
126
126
|
- lib/triggerable.rb
|
127
|
-
- lib/triggerable/actions.rb
|
127
|
+
- lib/triggerable/actions/action.rb
|
128
|
+
- lib/triggerable/actions/lambda_action.rb
|
128
129
|
- lib/triggerable/conditions/condition.rb
|
129
130
|
- lib/triggerable/conditions/field/exists.rb
|
130
131
|
- lib/triggerable/conditions/field/field_condition.rb
|
data/lib/triggerable/actions.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
module Triggerable
|
2
|
-
class Action
|
3
|
-
def self.build source
|
4
|
-
if source.is_a?(Proc)
|
5
|
-
[LambdaAction.new(source)]
|
6
|
-
else
|
7
|
-
Array(source).map do |source|
|
8
|
-
descendant = descendants.find { |d| d == source.to_s.camelize.constantize }
|
9
|
-
descendant.new if descendant.present?
|
10
|
-
end.compact
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def run_for!(trigger_name, object); end
|
15
|
-
end
|
16
|
-
|
17
|
-
class LambdaAction < Action
|
18
|
-
def initialize block
|
19
|
-
@block = block
|
20
|
-
end
|
21
|
-
|
22
|
-
def run_for! object, trigger_name
|
23
|
-
proc = @block
|
24
|
-
object.instance_eval do
|
25
|
-
change_whodunnit = trigger_name.present? && defined?(PaperTrail)
|
26
|
-
old_whodunnit = nil
|
27
|
-
|
28
|
-
if change_whodunnit
|
29
|
-
old_whodunnit = PaperTrail.whodunnit
|
30
|
-
PaperTrail.whodunnit = trigger_name
|
31
|
-
end
|
32
|
-
|
33
|
-
begin
|
34
|
-
instance_exec(&proc)
|
35
|
-
ensure
|
36
|
-
PaperTrail.whodunnit = old_whodunnit if change_whodunnit
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|