railsware-workflow 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
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
@@ -0,0 +1,37 @@
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.
@@ -0,0 +1,39 @@
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
+
@@ -0,0 +1,54 @@
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
+
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "railsware-workflow"
6
+ s.version = "0.8.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Vladimir Dobriakov"]
9
+ s.email = ["vladimir@geekq.net"]
10
+ s.homepage = "https://github.com/railsware/workflow"
11
+ s.summary = "A replacement for acts_as_state_machine."
12
+ s.description = <<-EOS
13
+ Workflow is a finite-state-machine-inspired API for modeling and interacting
14
+ with what we tend to refer to as 'workflow'.
15
+
16
+ * nice DSL to describe your states, events and transitions
17
+ * robust integration with ActiveRecord and non relational data stores
18
+ * various hooks for single transitions, entering state etc.
19
+ * convenient access to the workflow specification: list states, possible events
20
+ for particular state
21
+ EOS
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1 @@
1
+ Dir["#{File.dirname(__FILE__)}/lib/*.rb"].each { |rb| require rb }
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railsware-workflow
3
+ version: !ruby/object:Gem::Version
4
+ hash: 61
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 8
9
+ - 1
10
+ version: 0.8.1
11
+ platform: ruby
12
+ authors:
13
+ - Vladimir Dobriakov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-15 00:00:00 Z
19
+ dependencies: []
20
+
21
+ 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"
22
+ email:
23
+ - vladimir@geekq.net
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - MIT-LICENSE
33
+ - README.markdown
34
+ - Rakefile
35
+ - VERSION
36
+ - lib/workflow.rb
37
+ - test/advanced_hooks_and_validation_test.rb
38
+ - test/before_transition_test.rb
39
+ - test/couchtiny_example.rb
40
+ - test/main_test.rb
41
+ - test/multiple_workflows_test.rb
42
+ - test/readme_example.rb
43
+ - test/test_helper.rb
44
+ - test/without_active_record_test.rb
45
+ - workflow.gemspec
46
+ - workflow.rb
47
+ homepage: https://github.com/railsware/workflow
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.7.2
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A replacement for acts_as_state_machine.
80
+ test_files:
81
+ - test/advanced_hooks_and_validation_test.rb
82
+ - test/before_transition_test.rb
83
+ - test/couchtiny_example.rb
84
+ - test/main_test.rb
85
+ - test/multiple_workflows_test.rb
86
+ - test/readme_example.rb
87
+ - test/test_helper.rb
88
+ - test/without_active_record_test.rb
89
+ has_rdoc: