transitions 0.0.16 → 0.0.17
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/.gitignore +2 -0
- data/.rvmrc +1 -1
- data/CHANGELOG.md +4 -0
- data/README.rdoc +8 -9
- data/lib/active_model/transitions.rb +8 -5
- data/lib/transitions/version.rb +1 -1
- data/test/test_active_record.rb +8 -0
- data/test/test_event_being_fired.rb +1 -1
- metadata +15 -15
data/.gitignore
CHANGED
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm 1.9.
|
1
|
+
rvm 1.9.2-p290@transitions --create
|
data/CHANGELOG.md
CHANGED
data/README.rdoc
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
= Synopsis
|
6
6
|
|
7
|
-
|
7
|
+
<tt>transitions</tt> is a ruby state machine implementation.
|
8
8
|
|
9
9
|
= Installation
|
10
10
|
|
@@ -51,13 +51,12 @@ This goes into your Gemfile:
|
|
51
51
|
== Events
|
52
52
|
|
53
53
|
When you declare an event, say <tt>discontinue</tt>, two methods are declared for
|
54
|
-
you: <tt>discontinue</tt> and <tt>discontinue!</tt>. Both events will
|
55
|
-
|
56
|
-
bang(!)-version will call <tt>write_state</tt>.
|
54
|
+
you: <tt>discontinue</tt> and <tt>discontinue!</tt>. Both events will modify the <tt>state</tt> attribute on successful transition,
|
55
|
+
but only the bang(!)-version will call <tt>save!</tt>.
|
57
56
|
|
58
57
|
== Automatic scope generation
|
59
58
|
|
60
|
-
|
59
|
+
<tt>transitions</tt> will automatically generate scopes for you if you are using ActiveRecord and tell it to do so via the <tt>auto_scopes</tt> option:
|
61
60
|
|
62
61
|
Given a model like this:
|
63
62
|
|
@@ -78,7 +77,7 @@ you can use this feature a la:
|
|
78
77
|
>> Order.pick_line_items
|
79
78
|
=> [#<Order id: 3, state: "pick_line_items", description: nil, created_at: "2011-08-23 15:48:46", updated_at: "2011-08-23 15:48:46">]
|
80
79
|
|
81
|
-
== Using
|
80
|
+
== Using <tt>on_transition</tt>
|
82
81
|
|
83
82
|
Each event definition takes an optional "on_transition" argument, which allows you to execute methods on transition.
|
84
83
|
You can pass in a Symbol, a String, a Proc or an Array containing method names as Symbol or String like this:
|
@@ -87,9 +86,9 @@ You can pass in a Symbol, a String, a Proc or an Array containing method names a
|
|
87
86
|
transitions :to => :discontinued, :from => [:available, :out_of_stock], :on_transition => [:do_discontinue, :notify_clerk]
|
88
87
|
end
|
89
88
|
|
90
|
-
== Using
|
89
|
+
== Using <tt>success</tt>
|
91
90
|
|
92
|
-
In case you need to trigger a method call after a successful transition you can use
|
91
|
+
In case you need to trigger a method call after a successful transition you can use <tt>success</tt>:
|
93
92
|
|
94
93
|
event :discontinue, :success => :notfiy_admin do
|
95
94
|
transitions :to => :discontinued, :from => [:available, :out_of_stock]
|
@@ -124,7 +123,7 @@ the name of the timestamp column.
|
|
124
123
|
|
125
124
|
You can easily get a listing of all available states:
|
126
125
|
|
127
|
-
Order.available_states # Uses the
|
126
|
+
Order.available_states # Uses the <tt>default</tt> state machine
|
128
127
|
# => [:pick_line_items, :picking_line_items]
|
129
128
|
|
130
129
|
In case you have multiple state machines you can also pass the state machine name:
|
@@ -45,16 +45,19 @@ module ActiveModel
|
|
45
45
|
protected
|
46
46
|
|
47
47
|
def write_state(state_machine, state)
|
48
|
-
ivar = state_machine.current_state_variable
|
49
48
|
prev_state = current_state(state_machine.name)
|
50
|
-
|
51
|
-
self.state = state.to_s
|
49
|
+
write_state_without_persistence(state_machine, state)
|
52
50
|
save!
|
53
51
|
rescue ActiveRecord::RecordInvalid
|
54
|
-
|
55
|
-
instance_variable_set(ivar, prev_state)
|
52
|
+
write_state_without_persistence(state_machine, prev_state)
|
56
53
|
raise
|
57
54
|
end
|
55
|
+
|
56
|
+
def write_state_without_persistence(state_machine, state)
|
57
|
+
ivar = state_machine.current_state_variable
|
58
|
+
instance_variable_set(ivar, state)
|
59
|
+
self.state = state.to_s
|
60
|
+
end
|
58
61
|
|
59
62
|
def read_state(state_machine)
|
60
63
|
self.state && self.state.to_sym
|
data/lib/transitions/version.rb
CHANGED
data/test/test_active_record.rb
CHANGED
@@ -164,6 +164,14 @@ class TestActiveRecord < Test::Unit::TestCase
|
|
164
164
|
@light.update_attribute(:state, 'green')
|
165
165
|
assert @light.reload.green?, "reloaded state should come from database, not instance variable"
|
166
166
|
end
|
167
|
+
|
168
|
+
test "calling non-bang event updates state attribute" do
|
169
|
+
@light.reset!
|
170
|
+
assert @light.red?
|
171
|
+
@light.green_on
|
172
|
+
assert_equal "green", @light.state
|
173
|
+
assert_equal "red", @light.reload.state
|
174
|
+
end
|
167
175
|
|
168
176
|
end
|
169
177
|
|
@@ -10,7 +10,7 @@ class TestEventBeingFired < Test::Unit::TestCase
|
|
10
10
|
exception = assert_raise Transitions::InvalidTransition do
|
11
11
|
event.fire(obj)
|
12
12
|
end
|
13
|
-
assert_match /
|
13
|
+
assert_match /Cannot transition to default from running for `TestEventBeingFired::AnotherDummy/, exception.message
|
14
14
|
end
|
15
15
|
|
16
16
|
test "should return the state of the first matching transition it finds" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transitions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.17
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-05-02 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
17
|
-
requirement: &
|
17
|
+
requirement: &80694140 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '1'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *80694140
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: test-unit
|
28
|
-
requirement: &
|
28
|
+
requirement: &80692960 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '2.2'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *80692960
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: mocha
|
39
|
-
requirement: &
|
39
|
+
requirement: &80692570 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *80692570
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rake
|
50
|
-
requirement: &
|
50
|
+
requirement: &80682540 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *80682540
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: sqlite3
|
61
|
-
requirement: &
|
61
|
+
requirement: &80681530 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: '0'
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *80681530
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: activerecord
|
72
|
-
requirement: &
|
72
|
+
requirement: &80680830 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ~>
|
@@ -77,7 +77,7 @@ dependencies:
|
|
77
77
|
version: '3'
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *80680830
|
81
81
|
description: Lightweight state machine extracted from ActiveModel
|
82
82
|
email: timo.roessner@googlemail.com
|
83
83
|
executables: []
|
@@ -129,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
129
|
version: '0'
|
130
130
|
segments:
|
131
131
|
- 0
|
132
|
-
hash: -
|
132
|
+
hash: -1021171215
|
133
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
134
|
none: false
|
135
135
|
requirements:
|