-
Notifications
You must be signed in to change notification settings - Fork 13
/
multiple_workflows_test.rb
84 lines (67 loc) · 2.37 KB
/
multiple_workflows_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require File.join(File.dirname(__FILE__), 'test_helper')
require 'workflow'
class MultipleWorkflowsTest < ActiveRecordTestCase
test 'multiple workflows' do
ActiveRecord::Schema.define do
create_table :bookings do |t|
t.string :title, :null => false
t.string :workflow_state
t.string :workflow_type
end
end
exec "INSERT INTO bookings(title, workflow_state, workflow_type) VALUES('booking1', 'initial', 'workflow_1')"
exec "INSERT INTO bookings(title, workflow_state, workflow_type) VALUES('booking2', 'initial', 'workflow_2')"
class Booking < ActiveRecord::Base
include WorkflowActiverecord
def initialize_workflow
# define workflow per object instead of per class
case workflow_type
when 'workflow_1'
class << self
workflow do
state :initial do
event :progress, :transitions_to => :last
end
state :last
end
end
when 'workflow_2'
class << self
workflow do
state :initial do
event :progress, :transitions_to => :intermediate
end
state :intermediate
state :last
end
end
end
end
def metaclass; class << self; self; end; end
def workflow_spec
metaclass.workflow_spec
end
end
booking1 = Booking.find_by_title('booking1')
booking1.initialize_workflow
booking2 = Booking.find_by_title('booking2')
booking2.initialize_workflow
assert booking1.initial?
booking1.progress!
assert booking1.last?, 'booking1 should transition to the "last" state'
assert booking2.initial?
booking2.progress!
assert booking2.intermediate?, 'booking2 should transition to the "intermediate" state'
assert booking1.workflow_spec, 'can access the individual workflow specification'
assert_equal 2, booking1.workflow_spec.states.length
assert_equal 3, booking2.workflow_spec.states.length
# check persistence
booking2reloaded = Booking.find_by_title('booking2')
booking2reloaded.initialize_workflow
assert booking2reloaded.intermediate?, 'persistence of workflow state does not work'
end
class Object
# The hidden singleton lurks behind everyone
def metaclass; class << self; self; end; end
end
end