statesman 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f0139bf848dacbe81fcca09b0efcbe931d773125
4
- data.tar.gz: ef0b4400a4629ed4bebf641e5161e17b27901485
3
+ metadata.gz: 854b8cc89e069fa3322067960b77dd3cf82364bc
4
+ data.tar.gz: 8b9dd4292751c9323ebcf59b4ee9373ef9ed42eb
5
5
  SHA512:
6
- metadata.gz: 514b488888819643db51e1618b2109d6150aab9b4aa2bd7b5f21ac324523a0a4facc6a442e99ef7d486199bc140b9dc53eaa0d1ff0ffa5ed49fb988ac5d1dc06
7
- data.tar.gz: 293d1464c01cb538666dc1f84393054209cbd6f0f243e48945bcf8d023b9076b0767805dcb89cf9017b1be23be80bc314e8442fff8c4e6131c891cb103b1d16c
6
+ metadata.gz: 6df28629b85027c1a3d3ae6e04e676d6037f500ad9448a5f50afaf2ad21d3d7159537acbeb33786f99293ced225ed487fcfa897068adff2a10447df31ad333bc
7
+ data.tar.gz: 5300f0546e4929cb29b5ab4d86af48a1e9232a443e179c9cec180ee82109e481e34a317e43b3fe5f077bbd1320d3b027e8976bdcdf89dea36d7b8f11fc02d0fa
data/.travis.yml CHANGED
@@ -1,8 +1,11 @@
1
1
  rvm:
2
- - 2.1.0
2
+ - 2.1
3
3
  - 2.0.0
4
4
  - 1.9.3
5
5
  services: mongodb
6
6
  script:
7
7
  - bundle exec rubocop
8
8
  - bundle exec rake
9
+ env:
10
+ - "RAILS_VERSION=4.1.7"
11
+ - "RAILS_VERSION=4.2.0.rc2"
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in statesman.gemspec
4
3
  gemspec
4
+
5
+ gem "rails", "~> #{ENV["RAILS_VERSION"]}" if ENV["RAILS_VERSION"]
@@ -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 = transition_class.serialized_attributes.include?("metadata")
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
- reflections[transition_name].foreign_key
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
@@ -7,7 +7,7 @@ module Statesman
7
7
  attr_accessor :sort_key
8
8
  attr_accessor :metadata
9
9
 
10
- def initialize(to, sort_key, metadata = nil)
10
+ def initialize(to, sort_key, metadata = {})
11
11
  @created_at = Time.now
12
12
  @updated_at = Time.now
13
13
  @to_state = to
@@ -202,7 +202,7 @@ module Statesman
202
202
  @storage_adapter.last
203
203
  end
204
204
 
205
- def can_transition_to?(new_state, metadata = nil)
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 = nil)
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 = nil)
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 = nil)
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 = nil)
257
+ def trigger(event_name, metadata = {})
258
258
  self.trigger!(event_name, metadata)
259
259
  rescue TransitionFailedError, GuardFailedError
260
260
  false
@@ -1,3 +1,3 @@
1
1
  module Statesman
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -14,7 +14,7 @@ describe Statesman::Adapters::ActiveRecordQueries do
14
14
  end
15
15
 
16
16
  def self.initial_state
17
- MyStateMachine.initial_state
17
+ :initial
18
18
  end
19
19
  end
20
20
  end
@@ -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
- allow(MyActiveRecordModelTransition)
28
- .to receive_messages(serialized_attributes: {})
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
- allow(MyActiveRecordModelTransition)
46
- .to receive_messages(serialized_attributes: { 'metadata' => '' })
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, nil).and_return(true)
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, nil).and_return(true)
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.0.0
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-11-21 00:00:00.000000000 Z
12
+ date: 2014-12-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler