transitions 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/CHANGELOG.md +4 -0
- data/README.rdoc +6 -2
- data/lib/transitions.rb +24 -22
- data/lib/transitions/machine.rb +2 -2
- data/lib/transitions/version.rb +1 -1
- data/test/state_transition/test_state_transition_on_transition_callback.rb +18 -13
- metadata +3 -3
data/.travis.yml
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm: 1.9.
|
1
|
+
rvm: 1.9.3
|
data/CHANGELOG.md
CHANGED
data/README.rdoc
CHANGED
@@ -51,7 +51,11 @@ If this is not the case for you you have to add
|
|
51
51
|
|
52
52
|
whereever you load your dependencies in your application.
|
53
53
|
|
54
|
-
<b>
|
54
|
+
<b>Know limitations:</b>
|
55
|
+
|
56
|
+
* You can only use one state machine per model. While in theory you can define two or more, this won't work as you would expect. Not supporting this was intentional, if you're interested in the ratione look up version 1.0.0 in the CHANGELOG.
|
57
|
+
|
58
|
+
* Use symbols, not strings for declaring the state machine. Using strings is *not* supported as is using whitespace in names (because `transitions` possibly generates methods out of this).
|
55
59
|
|
56
60
|
=== Features
|
57
61
|
|
@@ -105,7 +109,7 @@ In case you need to trigger a method call after a successful transition you can
|
|
105
109
|
In addition to just specify the method name on the record as a symbol you can pass a lambda to
|
106
110
|
perfom some more complex success callbacks:
|
107
111
|
|
108
|
-
event :discontinue, :success => lambda { |order
|
112
|
+
event :discontinue, :success => lambda { |order| AdminNotifier.notify_about_discontinued_order(order) } do
|
109
113
|
transitions :to => :discontinued, :from => [:available, :out_of_stock]
|
110
114
|
end
|
111
115
|
|
data/lib/transitions.rb
CHANGED
@@ -57,30 +57,32 @@ module Transitions
|
|
57
57
|
base.extend(ClassMethods)
|
58
58
|
end
|
59
59
|
|
60
|
-
|
61
|
-
def current_state(new_state = nil, persist = false)
|
60
|
+
def update_current_state(new_state, persist = false)
|
62
61
|
sm = self.class.get_state_machine
|
63
62
|
ivar = sm.current_state_variable
|
64
|
-
if
|
65
|
-
if persist
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
if respond_to?(:write_state_without_persistence)
|
70
|
-
write_state_without_persistence(new_state)
|
71
|
-
end
|
72
|
-
|
73
|
-
instance_variable_set(ivar, new_state)
|
74
|
-
else
|
75
|
-
instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar)
|
76
|
-
value = instance_variable_get(ivar)
|
77
|
-
return value if value
|
78
|
-
|
79
|
-
if respond_to?(:read_state)
|
80
|
-
value = instance_variable_set(ivar, read_state)
|
81
|
-
end
|
82
|
-
|
83
|
-
value || sm.initial_state
|
63
|
+
if ::Transitions.active_record_descendant?(self.class)
|
64
|
+
write_state(new_state) if persist
|
65
|
+
write_state_without_persistence(new_state) # TODO This seems like a duplicate, `write_new` already calls `write_state_without_persistence`.
|
84
66
|
end
|
67
|
+
|
68
|
+
instance_variable_set(ivar, new_state)
|
69
|
+
end
|
70
|
+
|
71
|
+
def current_state
|
72
|
+
sm = self.class.get_state_machine
|
73
|
+
ivar = sm.current_state_variable
|
74
|
+
|
75
|
+
value = instance_variable_get(ivar)
|
76
|
+
return value if value
|
77
|
+
|
78
|
+
if ::Transitions.active_record_descendant?(self.class)
|
79
|
+
value = instance_variable_set(ivar, read_state)
|
80
|
+
end
|
81
|
+
|
82
|
+
!(value.nil? || value.empty?) ? value : sm.initial_state
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.active_record_descendant?(klazz)
|
86
|
+
defined?(ActiveRecord::Base) && klazz < ActiveRecord::Base
|
85
87
|
end
|
86
88
|
end
|
data/lib/transitions/machine.rb
CHANGED
@@ -39,7 +39,7 @@ module Transitions
|
|
39
39
|
@initial_state = options[:initial] if options.key?(:initial)
|
40
40
|
@auto_scopes = options[:auto_scopes]
|
41
41
|
instance_eval(&block) if block
|
42
|
-
include_scopes if @auto_scopes &&
|
42
|
+
include_scopes if @auto_scopes && ::Transitions.active_record_descendant?(klass)
|
43
43
|
self
|
44
44
|
end
|
45
45
|
|
@@ -54,7 +54,7 @@ module Transitions
|
|
54
54
|
record.send(:event_fired, record.current_state, new_state, event)
|
55
55
|
end
|
56
56
|
|
57
|
-
record.
|
57
|
+
record.update_current_state(new_state, persist)
|
58
58
|
@events[event].success.call(record) if @events[event].success
|
59
59
|
return true
|
60
60
|
else
|
data/lib/transitions/version.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class Truck
|
4
4
|
include Transitions
|
5
|
+
attr_reader :test_recorder
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@test_recorder = []
|
9
|
+
end
|
5
10
|
|
6
11
|
state_machine do
|
7
12
|
state :parked
|
@@ -18,26 +23,26 @@ class Car
|
|
18
23
|
end
|
19
24
|
|
20
25
|
%w!start_engine loosen_handbrake push_gas_pedal!.each do |m|
|
21
|
-
define_method(m){}
|
26
|
+
define_method(m){ @test_recorder << m }
|
22
27
|
end
|
23
28
|
end
|
24
29
|
|
25
30
|
class TestStateTransitionCallbacks < Test::Unit::TestCase
|
26
|
-
def setup
|
27
|
-
@car = Car.new
|
28
|
-
end
|
29
|
-
|
30
31
|
test "should execute callback defined via 'on_transition'" do
|
31
|
-
|
32
|
-
|
32
|
+
truck = Truck.new
|
33
|
+
truck.expects(:start_engine)
|
34
|
+
truck.turn_key!
|
33
35
|
end
|
34
36
|
|
35
37
|
test "should execute multiple callbacks defined via 'on_transition' in the same order they were defined" do
|
36
|
-
|
38
|
+
# This test requires some explanation: We started out with something like this:
|
39
|
+
# truck.expects(:start_engine).in_sequence(on_transition_sequence)
|
40
|
+
# Which, after a while (don't ask me why) caused some weird problems and seemed to fail randomly.
|
41
|
+
# Hence the workaround below.
|
42
|
+
|
43
|
+
truck = Truck.new
|
37
44
|
|
38
|
-
|
39
|
-
|
40
|
-
@car.expects(:push_gas_pedal).in_sequence(on_transition_sequence)
|
41
|
-
@car.start_driving!
|
45
|
+
truck.start_driving!
|
46
|
+
assert_equal truck.test_recorder, [:start_engine, :loosen_handbrake, :push_gas_pedal].map(&:to_s)
|
42
47
|
end
|
43
48
|
end
|
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.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-08
|
13
|
+
date: 2012-10-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -179,7 +179,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
179
179
|
version: '0'
|
180
180
|
segments:
|
181
181
|
- 0
|
182
|
-
hash: -
|
182
|
+
hash: -696557581
|
183
183
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
184
184
|
none: false
|
185
185
|
requirements:
|