-
Notifications
You must be signed in to change notification settings - Fork 13
/
advanced_hooks_and_validation_test.rb
122 lines (104 loc) · 4.12 KB
/
advanced_hooks_and_validation_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require File.join(File.dirname(__FILE__), 'test_helper')
$VERBOSE = false
require 'active_record'
require 'sqlite3'
require 'workflow'
ActiveRecord::Migration.verbose = false
# Transition based validation
# ---------------------------
# If you are using ActiveRecord you might want to define different validations
# for different transitions. There is a `validates_presence_of` hook that let's
# you specify the attributes that need to be present for an successful transition.
# If the object is not valid at the end of the transition event the transition
# is halted and a TransitionHalted exception is thrown.
#
# Here is a sample that illustrates how to use the presence validation:
# (use case suggested by http://github.com/southdesign)
class Article < ActiveRecord::Base
include WorkflowActiverecord
workflow do
state :new do
event :accept, :transitions_to => :accepted, :meta => {:validates_presence_of => [:title, :body]}
event :reject, :transitions_to => :rejected
end
state :accepted do
event :blame, :transitions_to => :blamed, :meta => {:validates_presence_of => [:title, :body, :blame_reason]}
event :delete, :transitions_to => :deleted
end
state :rejected do
event :delete, :transitions_to => :deleted
end
state :blamed do
event :delete, :transitions_to => :deleted
end
state :deleted do
event :accept, :transitions_to => :accepted
end
on_transition do |from, to, triggering_event, *event_args|
if self.class.superclass.to_s.split("::").first == "ActiveRecord"
singleton = class << self; self end
validations = Proc.new {}
meta = Article.workflow_spec.states[from].events[triggering_event].first.meta
fields_to_validate = meta[:validates_presence_of]
if fields_to_validate
validations = Proc.new {
Array(fields_to_validate).each do |attribute|
value = self.send(:read_attribute_for_validation, attribute)
errors.add(attribute, :blank) if value.blank?
end
}
end
singleton.send :define_method, :validate_for_transition, &validations
validate_for_transition
halt! "Event[#{triggering_event}]'s transitions_to[#{to}] is not valid." unless self.errors.empty?
end
end
end
end
class AdvancedHooksAndValidationTest < ActiveRecordTestCase
def setup
super
ActiveRecord::Schema.define do
create_table :articles do |t|
t.string :title
t.string :body
t.string :blame_reason
t.string :reject_reason
t.string :workflow_state
end
end
exec "INSERT INTO articles(title, body, blame_reason, reject_reason, workflow_state) VALUES('new1', NULL, NULL, NULL, 'new')"
exec "INSERT INTO articles(title, body, blame_reason, reject_reason, workflow_state) VALUES('new2', 'some content', NULL, NULL, 'new')"
exec "INSERT INTO articles(title, body, blame_reason, reject_reason, workflow_state) VALUES('accepted1', 'some content', NULL, NULL, 'accepted')"
end
def assert_state(title, expected_state, klass = Order)
o = klass.find_by_title(title)
assert_equal expected_state, o.read_attribute(klass.workflow_column)
o
end
test 'deny transition from new to accepted because of the missing presence of the body' do
a = Article.find_by_title('new1');
assert_raises Workflow::TransitionHalted do
a.accept!
end
assert_state 'new1', 'new', Article
end
test 'allow transition from new to accepted because body is present this time' do
a = Article.find_by_title('new2');
assert a.accept!
assert_state 'new2', 'accepted', Article
end
test 'allow transition from accepted to blamed because of a blame_reason' do
a = Article.find_by_title('accepted1');
a.blame_reason = "Provocant thesis"
assert a.blame!
assert_state 'accepted1', 'blamed', Article
end
test 'deny transition from accepted to blamed because of no blame_reason' do
a = Article.find_by_title('accepted1');
assert_raises Workflow::TransitionHalted do
assert a.blame!
end
assert_state 'accepted1', 'accepted', Article
end
end