statesman 1.0.0 → 1.1.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/Gemfile +2 -1
- data/lib/statesman/adapters/active_record.rb +10 -1
- data/lib/statesman/adapters/active_record_queries.rb +2 -2
- data/lib/statesman/adapters/memory_transition.rb +1 -1
- data/lib/statesman/machine.rb +5 -5
- data/lib/statesman/version.rb +1 -1
- data/spec/statesman/adapters/active_record_queries_spec.rb +1 -1
- data/spec/statesman/adapters/active_record_spec.rb +17 -4
- data/spec/statesman/machine_spec.rb +12 -2
- data/spec/support/active_record.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 854b8cc89e069fa3322067960b77dd3cf82364bc
|
4
|
+
data.tar.gz: 8b9dd4292751c9323ebcf59b4ee9373ef9ed42eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6df28629b85027c1a3d3ae6e04e676d6037f500ad9448a5f50afaf2ad21d3d7159537acbeb33786f99293ced225ed487fcfa897068adff2a10447df31ad333bc
|
7
|
+
data.tar.gz: 5300f0546e4929cb29b5ab4d86af48a1e9232a443e179c9cec180ee82109e481e34a317e43b3fe5f077bbd1320d3b027e8976bdcdf89dea36d7b8f11fc02d0fa
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -7,7 +7,7 @@ module Statesman
|
|
7
7
|
attr_reader :parent_model
|
8
8
|
|
9
9
|
def initialize(transition_class, parent_model, observer)
|
10
|
-
serialized =
|
10
|
+
serialized = serialized?(transition_class)
|
11
11
|
column_type = transition_class.columns_hash['metadata'].sql_type
|
12
12
|
if !serialized && column_type != 'json'
|
13
13
|
raise UnserializedMetadataError,
|
@@ -75,6 +75,15 @@ module Statesman
|
|
75
75
|
def next_sort_key
|
76
76
|
(last && last.sort_key + 10) || 0
|
77
77
|
end
|
78
|
+
|
79
|
+
def serialized?(transition_class)
|
80
|
+
if ::ActiveRecord.gem_version >= Gem::Version.new('4.2.0.a')
|
81
|
+
transition_class.columns_hash["metadata"]
|
82
|
+
.cast_type.is_a?(::ActiveRecord::Type::Serialized)
|
83
|
+
else
|
84
|
+
transition_class.serialized_attributes.include?("metadata")
|
85
|
+
end
|
86
|
+
end
|
78
87
|
end
|
79
88
|
end
|
80
89
|
end
|
@@ -41,7 +41,7 @@ module Statesman
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def model_foreign_key
|
44
|
-
|
44
|
+
reflect_on_association(transition_name).foreign_key
|
45
45
|
end
|
46
46
|
|
47
47
|
def transition1_join
|
@@ -56,7 +56,7 @@ module Statesman
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def state_inclusion_where(states)
|
59
|
-
if initial_state.in?(states)
|
59
|
+
if initial_state.to_s.in?(states.map(&:to_s))
|
60
60
|
'transition1.to_state IN (?) OR ' \
|
61
61
|
'transition1.to_state IS NULL'
|
62
62
|
else
|
data/lib/statesman/machine.rb
CHANGED
@@ -202,7 +202,7 @@ module Statesman
|
|
202
202
|
@storage_adapter.last
|
203
203
|
end
|
204
204
|
|
205
|
-
def can_transition_to?(new_state, metadata =
|
205
|
+
def can_transition_to?(new_state, metadata = {})
|
206
206
|
validate_transition(from: current_state,
|
207
207
|
to: new_state,
|
208
208
|
metadata: metadata)
|
@@ -215,7 +215,7 @@ module Statesman
|
|
215
215
|
@storage_adapter.history
|
216
216
|
end
|
217
217
|
|
218
|
-
def transition_to!(new_state, metadata =
|
218
|
+
def transition_to!(new_state, metadata = {})
|
219
219
|
initial_state = current_state
|
220
220
|
new_state = new_state.to_s
|
221
221
|
|
@@ -228,7 +228,7 @@ module Statesman
|
|
228
228
|
true
|
229
229
|
end
|
230
230
|
|
231
|
-
def trigger!(event_name, metadata =
|
231
|
+
def trigger!(event_name, metadata = {})
|
232
232
|
transitions = self.class.events.fetch(event_name) do
|
233
233
|
raise Statesman::TransitionFailedError,
|
234
234
|
"Event #{event_name} not found"
|
@@ -248,13 +248,13 @@ module Statesman
|
|
248
248
|
callbacks.each { |cb| cb.call(@object, transition) }
|
249
249
|
end
|
250
250
|
|
251
|
-
def transition_to(new_state, metadata =
|
251
|
+
def transition_to(new_state, metadata = {})
|
252
252
|
self.transition_to!(new_state, metadata)
|
253
253
|
rescue TransitionFailedError, GuardFailedError
|
254
254
|
false
|
255
255
|
end
|
256
256
|
|
257
|
-
def trigger(event_name, metadata =
|
257
|
+
def trigger(event_name, metadata = {})
|
258
258
|
self.trigger!(event_name, metadata)
|
259
259
|
rescue TransitionFailedError, GuardFailedError
|
260
260
|
false
|
data/lib/statesman/version.rb
CHANGED
@@ -24,8 +24,12 @@ describe Statesman::Adapters::ActiveRecord do
|
|
24
24
|
allow(metadata_column).to receive_messages(sql_type: '')
|
25
25
|
allow(MyActiveRecordModelTransition).to receive_messages(columns_hash:
|
26
26
|
{ 'metadata' => metadata_column })
|
27
|
-
|
28
|
-
.to receive_messages(
|
27
|
+
if ::ActiveRecord.gem_version >= Gem::Version.new('4.2.0.a')
|
28
|
+
allow(metadata_column).to receive_messages(cast_type: '')
|
29
|
+
else
|
30
|
+
allow(MyActiveRecordModelTransition)
|
31
|
+
.to receive_messages(serialized_attributes: {})
|
32
|
+
end
|
29
33
|
end
|
30
34
|
|
31
35
|
it "raises an exception" do
|
@@ -42,8 +46,17 @@ describe Statesman::Adapters::ActiveRecord do
|
|
42
46
|
allow(metadata_column).to receive_messages(sql_type: 'json')
|
43
47
|
allow(MyActiveRecordModelTransition).to receive_messages(columns_hash:
|
44
48
|
{ 'metadata' => metadata_column })
|
45
|
-
|
46
|
-
.
|
49
|
+
if ::ActiveRecord.gem_version >= Gem::Version.new('4.2.0.a')
|
50
|
+
serialized_type = ::ActiveRecord::Type::Serialized.new(
|
51
|
+
'', ::ActiveRecord::Coders::JSON
|
52
|
+
)
|
53
|
+
expect(metadata_column)
|
54
|
+
.to receive(:cast_type)
|
55
|
+
.and_return(serialized_type)
|
56
|
+
else
|
57
|
+
expect(MyActiveRecordModelTransition)
|
58
|
+
.to receive_messages(serialized_attributes: { 'metadata' => '' })
|
59
|
+
end
|
47
60
|
end
|
48
61
|
|
49
62
|
it "raises an exception" do
|
@@ -527,6 +527,11 @@ describe Statesman::Machine do
|
|
527
527
|
expect(instance.history.first.metadata).to eq(meta)
|
528
528
|
end
|
529
529
|
|
530
|
+
it "sets an empty hash as the metadata if not specified" do
|
531
|
+
instance.transition_to!(:y)
|
532
|
+
expect(instance.history.first.metadata).to eq({})
|
533
|
+
end
|
534
|
+
|
530
535
|
it "returns true" do
|
531
536
|
expect(instance.transition_to!(:y)).to be_truthy
|
532
537
|
end
|
@@ -541,7 +546,7 @@ describe Statesman::Machine do
|
|
541
546
|
|
542
547
|
it "passes the object to the guard" do
|
543
548
|
expect(guard_cb).to receive(:call).once
|
544
|
-
.with(my_model, instance.last_transition,
|
549
|
+
.with(my_model, instance.last_transition, {}).and_return(true)
|
545
550
|
instance.transition_to!(:y)
|
546
551
|
end
|
547
552
|
end
|
@@ -703,6 +708,11 @@ describe Statesman::Machine do
|
|
703
708
|
expect(instance.history.first.metadata).to eq(meta)
|
704
709
|
end
|
705
710
|
|
711
|
+
it "sets an empty hash as the metadata if not specified" do
|
712
|
+
instance.trigger!(:event_1)
|
713
|
+
expect(instance.history.first.metadata).to eq({})
|
714
|
+
end
|
715
|
+
|
706
716
|
it "returns true" do
|
707
717
|
expect(instance.trigger!(:event_1)).to eq(true)
|
708
718
|
end
|
@@ -719,7 +729,7 @@ describe Statesman::Machine do
|
|
719
729
|
|
720
730
|
it "passes the object to the guard" do
|
721
731
|
expect(guard_cb).to receive(:call).once
|
722
|
-
.with(my_model, instance.last_transition,
|
732
|
+
.with(my_model, instance.last_transition, {}).and_return(true)
|
723
733
|
instance.trigger!(:event_1)
|
724
734
|
end
|
725
735
|
end
|
@@ -32,7 +32,7 @@ class CreateMyActiveRecordModelMigration < ActiveRecord::Migration
|
|
32
32
|
def change
|
33
33
|
create_table :my_active_record_models do |t|
|
34
34
|
t.string :current_state
|
35
|
-
t.timestamps
|
35
|
+
t.timestamps(null: false)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -45,7 +45,7 @@ class CreateMyActiveRecordModelTransitionMigration < ActiveRecord::Migration
|
|
45
45
|
t.integer :my_active_record_model_id
|
46
46
|
t.integer :sort_key
|
47
47
|
t.text :metadata
|
48
|
-
t.timestamps
|
48
|
+
t.timestamps(null: false)
|
49
49
|
end
|
50
50
|
|
51
51
|
add_index :my_active_record_model_transitions, :sort_key, unique: true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: statesman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harry Marr
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-12-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|