minimal_state_machine 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +2 -0
- data/README.md +2 -2
- data/lib/generators/minimal_state_machine/minimal_state_machine_generator.rb +3 -3
- data/lib/generators/minimal_state_machine/templates/{create_msm_states.rb → 001_create_msm_states.rb} +0 -0
- data/lib/minimal_state_machine.rb +8 -14
- data/lib/minimal_state_machine/version.rb +1 -1
- data/minimal_state_machine.gemspec +2 -0
- data/spec/minimal_state_machine_spec.rb +4 -12
- data/spec/spec_helper.rb +10 -13
- metadata +42 -4
- data/db/.gitkeep +0 -0
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# MinimalStateMachine
|
1
|
+
# MinimalStateMachine [](https://travis-ci.org/issuehunter/minimal_state_machine)
|
2
2
|
|
3
3
|
A state machine for ActiveRecord implemented with the state pattern
|
4
4
|
|
@@ -66,7 +66,7 @@ issue.state
|
|
66
66
|
=> #<IssueState::Closed id: 1, type: "IssueState::Closed", state_machine_id: 1, state_machine_type: "Issue", created_at: "2013-01-03 19:13:31", updated_at: "2013-01-03 19:13:31">
|
67
67
|
```
|
68
68
|
|
69
|
-
If you try to change the state to a non valid state
|
69
|
+
If you try to change the state to a non valid state record validation will fail with *invalid transition* error.
|
70
70
|
|
71
71
|
If you try to set the state to a non declared state a `MinimalStateMachine::InvalidStateError` will be raised.
|
72
72
|
|
@@ -14,14 +14,14 @@ class MinimalStateMachineGenerator < Rails::Generators::Base
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def create_migration_files
|
17
|
-
create_migration_file_if_not_exist 'create_msm_states'
|
17
|
+
create_migration_file_if_not_exist 'create_msm_states', '001'
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
21
21
|
|
22
|
-
def create_migration_file_if_not_exist(file_name)
|
22
|
+
def create_migration_file_if_not_exist(file_name, version)
|
23
23
|
unless self.class.migration_exists?(File.dirname(File.expand_path("db/migrate/#{file_name}")), file_name)
|
24
|
-
migration_template "#{file_name}.rb", "db/migrate/#{file_name}.rb"
|
24
|
+
migration_template "#{version}_#{file_name}.rb", "db/migrate/#{file_name}.rb"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
File without changes
|
@@ -11,6 +11,10 @@ module MinimalStateMachine
|
|
11
11
|
after_initialize :set_initial_state, :if => proc { state.nil? }
|
12
12
|
after_save :destroy_previous_state, :if => proc { previous_state && previous_state != state }
|
13
13
|
|
14
|
+
validate do
|
15
|
+
self.errors.add(:state, 'invalid transition') if previous_state && !previous_state.class.valid_transition_states.include?(state_name)
|
16
|
+
end
|
17
|
+
|
14
18
|
attr_accessor :previous_state
|
15
19
|
|
16
20
|
def self.states
|
@@ -21,12 +25,8 @@ module MinimalStateMachine
|
|
21
25
|
|
22
26
|
def state_name=(state_name)
|
23
27
|
raise InvalidStateError unless self.class.states.keys.map(&:to_s).include?(state_name)
|
24
|
-
|
25
|
-
|
26
|
-
self.state = self.class.states[state_name.to_sym].new
|
27
|
-
else
|
28
|
-
transition_to state_name
|
29
|
-
end
|
28
|
+
|
29
|
+
transition_to(state_name)
|
30
30
|
end
|
31
31
|
|
32
32
|
def state_name
|
@@ -35,15 +35,9 @@ module MinimalStateMachine
|
|
35
35
|
|
36
36
|
private
|
37
37
|
|
38
|
-
class InvalidTransitionError < StandardError; end
|
39
|
-
|
40
38
|
def transition_to(state_name)
|
41
|
-
|
42
|
-
|
43
|
-
self.state = self.class.states[state_name.to_sym].new
|
44
|
-
else
|
45
|
-
raise InvalidTransitionError
|
46
|
-
end
|
39
|
+
self.previous_state = state
|
40
|
+
self.state = self.class.states[state_name.to_sym].new
|
47
41
|
end
|
48
42
|
|
49
43
|
def set_initial_state
|
@@ -1,15 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'msm_states'")
|
4
|
-
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'state_machines'")
|
5
|
-
ActiveRecord::Base.connection.create_table(:msm_states) do |t|
|
6
|
-
t.string :type
|
7
|
-
t.references :state_machine, :polymorphic => true
|
8
|
-
t.timestamps
|
9
|
-
end
|
10
|
-
|
11
|
-
ActiveRecord::Base.connection.create_table(:state_machines)
|
12
|
-
|
13
3
|
class StateMachine < ActiveRecord::Base
|
14
4
|
include MinimalStateMachine
|
15
5
|
|
@@ -63,8 +53,10 @@ describe StateMachine do
|
|
63
53
|
MinimalStateMachine::State.count.should == 1
|
64
54
|
end
|
65
55
|
|
66
|
-
it '
|
67
|
-
|
56
|
+
it 'adds error if the new state is not among the allowed transition states' do
|
57
|
+
@state_machine.state_name = 'solved'
|
58
|
+
@state_machine.should_not be_valid
|
59
|
+
@state_machine.errors[:state].should include('invalid transition')
|
68
60
|
end
|
69
61
|
|
70
62
|
it 'raises an invalid state error if the state assigned in not among the allowed states' do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,20 +1,17 @@
|
|
1
1
|
require 'minimal_state_machine'
|
2
2
|
require 'debugger'
|
3
3
|
|
4
|
-
|
5
|
-
ActiveRecord::Base.establish_connection(
|
6
|
-
:adapter => "sqlite3",
|
7
|
-
:database => "#{root}/db/minimal_state_machine.sqlite3"
|
8
|
-
)
|
4
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
ActiveRecord::Base.connection.begin_db_transaction
|
14
|
-
end
|
6
|
+
ActiveRecord::Migration.verbose = false
|
7
|
+
ActiveRecord::Migrator.up("lib/generators/minimal_state_machine/templates")
|
8
|
+
ActiveRecord::Migration.create_table :state_machines
|
15
9
|
|
16
|
-
|
17
|
-
|
18
|
-
ActiveRecord::Base.
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.around do |example|
|
12
|
+
ActiveRecord::Base.transaction do
|
13
|
+
example.run
|
14
|
+
raise ActiveRecord::Rollback
|
15
|
+
end
|
19
16
|
end
|
20
17
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimal_state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -75,6 +75,38 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: sqlite3
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
78
110
|
description: A state machine for activerecord implemented with the state pattern
|
79
111
|
email:
|
80
112
|
- matteodepalo@gmail.com
|
@@ -84,13 +116,13 @@ extra_rdoc_files: []
|
|
84
116
|
files:
|
85
117
|
- .gitignore
|
86
118
|
- .rspec
|
119
|
+
- .travis.yml
|
87
120
|
- Gemfile
|
88
121
|
- LICENSE.txt
|
89
122
|
- README.md
|
90
123
|
- Rakefile
|
91
|
-
- db/.gitkeep
|
92
124
|
- lib/generators/minimal_state_machine/minimal_state_machine_generator.rb
|
93
|
-
- lib/generators/minimal_state_machine/templates/
|
125
|
+
- lib/generators/minimal_state_machine/templates/001_create_msm_states.rb
|
94
126
|
- lib/minimal_state_machine.rb
|
95
127
|
- lib/minimal_state_machine/state.rb
|
96
128
|
- lib/minimal_state_machine/version.rb
|
@@ -109,12 +141,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
141
|
- - ! '>='
|
110
142
|
- !ruby/object:Gem::Version
|
111
143
|
version: '0'
|
144
|
+
segments:
|
145
|
+
- 0
|
146
|
+
hash: 3458422209847177818
|
112
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
148
|
none: false
|
114
149
|
requirements:
|
115
150
|
- - ! '>='
|
116
151
|
- !ruby/object:Gem::Version
|
117
152
|
version: '0'
|
153
|
+
segments:
|
154
|
+
- 0
|
155
|
+
hash: 3458422209847177818
|
118
156
|
requirements: []
|
119
157
|
rubyforge_project:
|
120
158
|
rubygems_version: 1.8.24
|
data/db/.gitkeep
DELETED
File without changes
|