state_manager 0.3.5 → 0.4.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: 0419accf4a65819e6bd75028b092a9888a36e691
4
- data.tar.gz: a0565779df5cc5b9446a8ecca9774360050956ef
3
+ metadata.gz: 8b15ad84b96d1dad7c55974b0471fa4be5a09566
4
+ data.tar.gz: 92fe664907bc65765d071c5e81eebf866d3e08d3
5
5
  SHA512:
6
- metadata.gz: 23dbd0c988675d0a84913c4882632ca8fc44c529a1694a43ad3ec91892e3b1495a2aeeb49538e9e7c8a162c788d65afa0a899664cab2f635ff1f76779fec10fc
7
- data.tar.gz: a8576af247fd9ee9401198d64573d3e867b807251887d4893dca15d7916324a44e595b214a2505c183db7519dea5deb9d5244d41afd25462d60d1b4b67227700
6
+ metadata.gz: 1ef18c91dc474166265143cb2e72a20c35c02a1fa324b1b56556fe07a39065a8f36f3d14c47189fca6b2506c7e23b8b8a074d88261b48682f98003d82cd198db
7
+ data.tar.gz: b6db67cb4441b593453c8c323bfb6d9aa4bfa4e27f48e5e626f79d7179e327c24bac64d1ab3011f33f402b247892f1e3a0fbe77c6613beb76d0990d94fd53554
@@ -19,11 +19,11 @@ module StateManager
19
19
  def _validate_states
20
20
  self.validate_states!
21
21
  end
22
-
22
+
23
23
  module ClassMethods
24
24
  def state_manager_added(property, klass, options)
25
25
  class_eval do
26
- klass.specification.states.keys.each do |state|
26
+ klass.specification.states.each do |state, specification_klass|
27
27
  # The connection might not be ready when defining this code is
28
28
  # reached so we wrap in a lamda.
29
29
  scope state, lambda {
@@ -31,12 +31,12 @@ module StateManager
31
31
  table = conn.quote_table_name table_name
32
32
  column = conn.quote_column_name klass._state_property
33
33
  namespaced_col = "#{table}.#{column}"
34
- query = "#{namespaced_col} = ? OR #{namespaced_col} LIKE ?"
35
- like_term = "#{state.to_s}.%"
36
- where(query, state, like_term)
34
+ states = [state].concat specification_klass.specification.descendant_names.map{|s| "#{state}.#{s}"}
35
+ query = "#{namespaced_col} in (?)"
36
+ where(query, states)
37
37
  }
38
38
  end
39
-
39
+
40
40
  after_initialize do
41
41
  self.state_managers ||= {}
42
42
  end
@@ -49,7 +49,7 @@ module StateManager
49
49
  after_commit(:on => :update) { state_managers.values.map(&:after_commit) }
50
50
  before_save { state_managers.values.map(&:before_save) }
51
51
  after_save { state_managers.values.map(&:after_save) }
52
- end
52
+ end
53
53
  end
54
54
  end
55
55
 
@@ -73,7 +73,7 @@ module StateManager
73
73
 
74
74
  def run_after_callbacks(*args)
75
75
  end
76
-
76
+
77
77
  end
78
78
  end
79
79
 
@@ -101,10 +101,10 @@ module StateManager
101
101
 
102
102
  _run_after_callbacks(*transition)
103
103
  end
104
-
104
+
105
105
  def after_commit
106
106
  transitions = self.uncommitted_transitions.dup
107
-
107
+
108
108
  self.uncommitted_transitions.clear
109
109
 
110
110
  transitions.each{ |t| run_commit_callbacks(*t) }
@@ -122,7 +122,7 @@ module StateManager
122
122
  def persist_state
123
123
  resource.save!
124
124
  end
125
-
125
+
126
126
  def perform_initial_transition?
127
127
  !current_state || resource.new_record?
128
128
  end
@@ -18,6 +18,15 @@ module StateManager
18
18
  self.states = source.states.dup
19
19
  self.events = source.events.dup
20
20
  end
21
+
22
+ def descendant_names
23
+ res = []
24
+ states.each do |state, specification_klass|
25
+ res << state
26
+ res.concat specification_klass.specification.descendant_names.map{|s| "#{state}.#{s}"}
27
+ end
28
+ res
29
+ end
21
30
  end
22
31
 
23
32
  class_attribute :specification
@@ -140,7 +149,7 @@ module StateManager
140
149
  end
141
150
 
142
151
  protected
143
-
152
+
144
153
  attr_writer :name, :states, :parent_state
145
154
 
146
155
  def method_missing(name, *args, &block)
@@ -149,4 +158,4 @@ module StateManager
149
158
 
150
159
 
151
160
  end
152
- end
161
+ end
@@ -1,3 +1,3 @@
1
1
  module StateManager
2
- VERSION = "0.3.5"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -25,7 +25,7 @@ class ActiveRecordTest < Minitest::Test
25
25
  state :active
26
26
  state :rejected
27
27
  state :mutated
28
-
28
+
29
29
  attr_accessor :unsubmitted_entered_count
30
30
  attr_accessor :unsubmitted_enter_committed_count
31
31
  attr_accessor :unsubmitted_exit_committed_count
@@ -51,9 +51,9 @@ class ActiveRecordTest < Minitest::Test
51
51
  def did_transition(*args)
52
52
  self.after_callbacks_called = true
53
53
  end
54
-
54
+
55
55
  class Unsubmitted
56
-
56
+
57
57
  def entered
58
58
  state_manager.unsubmitted_entered_count += 1
59
59
  end
@@ -65,11 +65,11 @@ class ActiveRecordTest < Minitest::Test
65
65
  def exit_committed
66
66
  state_manager.unsubmitted_exit_committed_count += 1
67
67
  end
68
-
68
+
69
69
  end
70
-
70
+
71
71
  class Active
72
-
72
+
73
73
  def entered
74
74
  state_manager.active_entered_count += 1
75
75
  end
@@ -81,7 +81,7 @@ class ActiveRecordTest < Minitest::Test
81
81
  def exit_committed
82
82
  state_manager.active_exit_committed_count += 1
83
83
  end
84
-
84
+
85
85
  end
86
86
 
87
87
  class Mutated
@@ -92,7 +92,7 @@ class ActiveRecordTest < Minitest::Test
92
92
  end
93
93
 
94
94
  end
95
-
95
+
96
96
  end
97
97
 
98
98
  class Post < ActiveRecord::Base
@@ -195,8 +195,8 @@ class ActiveRecordTest < Minitest::Test
195
195
 
196
196
  # +1 from setup
197
197
  assert_equal 1, Post.unsubmitted.count
198
- # +2 from setup
199
- assert_equal 8, Post.submitted.count
198
+ # +1 from setup (one is in a bad state)
199
+ assert_equal 7, Post.submitted.count
200
200
  assert_equal 4, Post.active.count
201
201
  assert_equal 0, Post.rejected.count
202
202
  end
@@ -217,7 +217,7 @@ class ActiveRecordTest < Minitest::Test
217
217
  @resource.state_manager.send_event :review
218
218
  end
219
219
  end
220
-
220
+
221
221
  def test_commit_callbacks
222
222
  @resource = Post.find(1)
223
223
  assert_state 'unsubmitted'
@@ -242,7 +242,7 @@ class ActiveRecordTest < Minitest::Test
242
242
  assert_equal 1, @resource.state_manager.unsubmitted_exit_committed_count
243
243
  assert_equal 1, @resource.state_manager.active_enter_committed_count
244
244
  end
245
-
245
+
246
246
  def test_commit_callbacks_on_create
247
247
  Post.transaction do
248
248
  @resource = Post.new
@@ -254,7 +254,7 @@ class ActiveRecordTest < Minitest::Test
254
254
  end
255
255
  assert_equal 1, @resource.state_manager.unsubmitted_enter_committed_count
256
256
  end
257
-
257
+
258
258
  def test_commit_callbacks_on_different_initial_state
259
259
  Post.transaction do
260
260
  @resource = Post.new(:state => 'active')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gordon L. Hempton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-21 00:00:00.000000000 Z
11
+ date: 2017-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project:
111
- rubygems_version: 2.4.5.1
111
+ rubygems_version: 2.5.2
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: "%Q{Finite state machine implementation.}"