workflow 1.2.0 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,84 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper')
2
- require 'workflow'
3
- class MultipleWorkflowsTest < ActiveRecordTestCase
4
-
5
- test 'multiple workflows' do
6
-
7
- ActiveRecord::Schema.define do
8
- create_table :bookings do |t|
9
- t.string :title, :null => false
10
- t.string :workflow_state
11
- t.string :workflow_type
12
- end
13
- end
14
-
15
- exec "INSERT INTO bookings(title, workflow_state, workflow_type) VALUES('booking1', 'initial', 'workflow_1')"
16
- exec "INSERT INTO bookings(title, workflow_state, workflow_type) VALUES('booking2', 'initial', 'workflow_2')"
17
-
18
- class Booking < ActiveRecord::Base
19
-
20
- include Workflow
21
-
22
- def initialize_workflow
23
- # define workflow per object instead of per class
24
- case workflow_type
25
- when 'workflow_1'
26
- class << self
27
- workflow do
28
- state :initial do
29
- event :progress, :transitions_to => :last
30
- end
31
- state :last
32
- end
33
- end
34
- when 'workflow_2'
35
- class << self
36
- workflow do
37
- state :initial do
38
- event :progress, :transitions_to => :intermediate
39
- end
40
- state :intermediate
41
- state :last
42
- end
43
- end
44
- end
45
- end
46
-
47
- def metaclass; class << self; self; end; end
48
-
49
- def workflow_spec
50
- metaclass.workflow_spec
51
- end
52
-
53
- end
54
-
55
- booking1 = Booking.find_by_title('booking1')
56
- booking1.initialize_workflow
57
-
58
- booking2 = Booking.find_by_title('booking2')
59
- booking2.initialize_workflow
60
-
61
- assert booking1.initial?
62
- booking1.progress!
63
- assert booking1.last?, 'booking1 should transition to the "last" state'
64
-
65
- assert booking2.initial?
66
- booking2.progress!
67
- assert booking2.intermediate?, 'booking2 should transition to the "intermediate" state'
68
-
69
- assert booking1.workflow_spec, 'can access the individual workflow specification'
70
- assert_equal 2, booking1.workflow_spec.states.length
71
- assert_equal 3, booking2.workflow_spec.states.length
72
-
73
- # check persistence
74
- booking2reloaded = Booking.find_by_title('booking2')
75
- booking2reloaded.initialize_workflow
76
- assert booking2reloaded.intermediate?, 'persistence of workflow state does not work'
77
- end
78
-
79
- class Object
80
- # The hidden singleton lurks behind everyone
81
- def metaclass; class << self; self; end; end
82
- end
83
-
84
- end
@@ -1,32 +0,0 @@
1
- require 'test_helper'
2
- require 'workflow'
3
-
4
- class ComparableStatesOrder
5
- include Workflow
6
- workflow do
7
- state :submitted do
8
- event :accept, :transitions_to => :accepted, :meta => {:weight => 8} do |reviewer, args|
9
- end
10
- end
11
- state :accepted do
12
- event :ship, :transitions_to => :shipped
13
- end
14
- state :shipped
15
- end
16
- end
17
-
18
- class CompareStatesTest < Test::Unit::TestCase
19
-
20
- test 'compare states' do
21
- o = ComparableStatesOrder.new
22
- o.accept!
23
- assert_equal :accepted, o.current_state.name
24
- assert o.current_state == :accepted
25
- assert o.current_state < :shipped
26
- assert o.current_state > :submitted
27
- assert_raise ArgumentError do
28
- o.current_state > :unknown
29
- end
30
- end
31
-
32
- end
@@ -1,62 +0,0 @@
1
- require 'test_helper'
2
- require 'active_record'
3
- require 'logger'
4
- require 'sqlite3'
5
- require 'workflow'
6
- require 'mocha/setup'
7
- require 'stringio'
8
-
9
- ActiveRecord::Migration.verbose = false
10
-
11
- class PersistenceTestOrder < ActiveRecord::Base
12
- include Workflow
13
-
14
- workflow do
15
- state :submitted do
16
- event :accept, :transitions_to => :accepted, :meta => {:doc_weight => 8} do |reviewer, args|
17
- end
18
- end
19
- state :accepted do
20
- event :ship, :transitions_to => :shipped
21
- end
22
- state :shipped
23
- end
24
-
25
- attr_accessible :title # protecting all the other attributes
26
-
27
- end
28
-
29
- PersistenceTestOrder.logger = Logger.new(STDOUT) # active_record 2.3 expects a logger instance
30
- PersistenceTestOrder.logger.level = Logger::WARN # switch to Logger::DEBUG to see the SQL statements
31
-
32
- class PersistenceTest < ActiveRecordTestCase
33
-
34
- def setup
35
- super
36
-
37
- ActiveRecord::Schema.define do
38
- create_table :persistence_test_orders do |t|
39
- t.string :title, :null => false
40
- t.string :workflow_state
41
- end
42
- end
43
-
44
- exec "INSERT INTO persistence_test_orders(title, workflow_state) VALUES('order6', 'accepted')"
45
- end
46
-
47
- def assert_state(title, expected_state, klass = PersistenceTestOrder)
48
- o = klass.find_by_title(title)
49
- assert_equal expected_state, o.read_attribute(klass.workflow_column)
50
- o
51
- end
52
-
53
- test 'ensure other dirty attributes are not saved on state change' do
54
- o = assert_state 'order6', 'accepted'
55
- o.title = 'going to change the title'
56
- assert o.changed?
57
- o.ship!
58
- assert o.changed?, 'title should not be saved and the change still stay pending'
59
- end
60
-
61
- end
62
-
@@ -1,52 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper')
2
- require 'workflow'
3
-
4
- class OnErrorTest < Test::Unit::TestCase
5
- # A class that does not handle errors in an error block
6
- class NoErrorBlock
7
- include Workflow
8
- workflow do
9
- state :first do
10
- event :forward, :transitions_to => :second do
11
- raise "This is some random runtime error"
12
- end
13
- end
14
- state :second
15
- end
16
- end
17
-
18
- # A class that handles errors in an error block
19
- class ErrorBlock
20
- attr_reader :errors
21
-
22
- def initialize
23
- @errors = {}
24
- end
25
-
26
- include Workflow
27
- workflow do
28
- state :first do
29
- event :forward, :transitions_to => :second do
30
- raise "This is some random runtime error"
31
- end
32
- end
33
- state :second
34
- on_error { |error, from, to, event, *args| @errors.merge!({:error => error.class, :from => from, :to => to, :event => event, :args => args}) }
35
- end
36
- end
37
-
38
-
39
- test 'that an exception is raised if there is no associated on_error block' do
40
- flow = NoErrorBlock.new
41
- assert_raise( RuntimeError, "This is some random runtime error" ) { flow.forward! }
42
- assert_equal(true, flow.first?)
43
- end
44
-
45
- test 'that on_error block is called when an exception is raised and the transition is halted' do
46
- flow = ErrorBlock.new
47
- assert_nothing_raised { flow.forward! }
48
- assert_equal({:error => RuntimeError, :from=>:first, :to=>:second, :event=>:forward, :args=>[]}, flow.errors)
49
- # transition should not happen
50
- assert_equal(true, flow.first?)
51
- end
52
- end
@@ -1,37 +0,0 @@
1
- require 'workflow'
2
- class Article
3
- include Workflow
4
- workflow do
5
- state :new do
6
- event :submit, :transitions_to => :awaiting_review
7
- end
8
- state :awaiting_review do
9
- event :review, :transitions_to => :being_reviewed
10
- end
11
- state :being_reviewed do
12
- event :accept, :transitions_to => :accepted
13
- event :reject, :transitions_to => :rejected
14
- end
15
- state :accepted
16
- state :rejected
17
- end
18
- end
19
-
20
- article = Article.new
21
- article.accepted? # => false
22
- article.new? # => true
23
- article.submit!
24
- article.review!
25
-
26
- puts article.current_state # => being_reviewed
27
-
28
-
29
- class Article
30
- def reject
31
- puts "send email to the author here explaining the reason for the rejection"
32
- end
33
- end
34
-
35
- article.reject! # will cause a state transition, would persist the new
36
- # state (if inherited from ActiveRecord), and invoke the callback -
37
- # send email to the author.
@@ -1,39 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'active_record'
4
-
5
- class << Test::Unit::TestCase
6
- def test(name, &block)
7
- test_name = :"test_#{name.gsub(' ','_')}"
8
- raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name.to_s
9
- if block
10
- define_method test_name, &block
11
- else
12
- puts "PENDING: #{name}"
13
- end
14
- end
15
- end
16
-
17
- class ActiveRecordTestCase < Test::Unit::TestCase
18
- def exec(sql)
19
- ActiveRecord::Base.connection.execute sql
20
- end
21
-
22
- def setup
23
- ActiveRecord::Base.establish_connection(
24
- :adapter => "sqlite3",
25
- :database => ":memory:" #"tmp/test"
26
- )
27
-
28
- # eliminate ActiveRecord warning. TODO: delete as soon as ActiveRecord is fixed
29
- ActiveRecord::Base.connection.reconnect!
30
- end
31
-
32
- def teardown
33
- ActiveRecord::Base.connection.disconnect!
34
- end
35
-
36
- def default_test
37
- end
38
- end
39
-
@@ -1,54 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper')
2
- require 'workflow'
3
-
4
- class WithoutWorkflowTest < Test::Unit::TestCase
5
- class Article
6
- include Workflow
7
- workflow do
8
- state :new do
9
- event :submit, :transitions_to => :awaiting_review
10
- end
11
- state :awaiting_review do
12
- event :review, :transitions_to => :being_reviewed
13
- end
14
- state :being_reviewed do
15
- event :accept, :transitions_to => :accepted
16
- event :reject, :transitions_to => :rejected
17
- end
18
- state :accepted
19
- state :rejected
20
- end
21
- end
22
-
23
- def test_readme_example_article
24
- article = Article.new
25
- assert article.new?
26
- end
27
-
28
- test 'better error message on transitions_to typo' do
29
- assert_raise Workflow::WorkflowDefinitionError do
30
- Class.new do
31
- include Workflow
32
- workflow do
33
- state :new do
34
- event :event1, :transitionnn => :next # missing transitions_to target
35
- end
36
- state :next
37
- end
38
- end
39
- end
40
- end
41
-
42
- test 'check transition_to alias' do
43
- Class.new do
44
- include Workflow
45
- workflow do
46
- state :new do
47
- event :event1, :transition_to => :next
48
- end
49
- state :next
50
- end
51
- end
52
- end
53
- end
54
-
@@ -1,32 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'workflow/version'
5
-
6
- Gem::Specification.new do |gem|
7
- gem.name = "workflow"
8
- gem.version = Workflow::VERSION
9
- gem.authors = ["Vladimir Dobriakov"]
10
- gem.email = ["vladimir@geekq.net"]
11
- gem.description = " Workflow is a finite-state-machine-inspired API for modeling and interacting\n with what we tend to refer to as 'workflow'.\n\n * nice DSL to describe your states, events and transitions\n * robust integration with ActiveRecord and non relational data stores\n * various hooks for single transitions, entering state etc.\n * convenient access to the workflow specification: list states, possible events\n for particular state\n"
12
- gem.summary = %q{A replacement for acts_as_state_machine.}
13
- gem.homepage = "http://www.geekq.net/workflow/"
14
-
15
- gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
19
-
20
- gem.extra_rdoc_files = [
21
- "README.markdown"
22
- ]
23
-
24
- gem.add_development_dependency 'rdoc', [">= 3.12"]
25
- gem.add_development_dependency 'bundler', [">= 1.0.0"]
26
- gem.add_development_dependency 'activerecord'
27
- gem.add_development_dependency 'sqlite3'
28
- gem.add_development_dependency 'mocha'
29
- gem.add_development_dependency 'rake'
30
- gem.add_development_dependency 'ruby-graphviz', ['~> 1.0.0']
31
- end
32
-