state_manager 0.2.11 → 0.2.12

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 CHANGED
@@ -12,7 +12,7 @@ doc
12
12
  # bundler
13
13
  .bundle
14
14
 
15
- # jeweler generated
16
- pkg
15
+ *.gem
16
+ Gemfile.lock
17
17
 
18
18
  .DS_Store
@@ -78,9 +78,9 @@ module StateManager
78
78
  end
79
79
  end
80
80
 
81
- def transition_to(path)
81
+ def transition_to(*args)
82
82
  raise(DirtyTransition, "Only one state transition may be performed before saving a record. This error could be caused by the record being initialized to a default state.") if pending_transition
83
- super(path)
83
+ super
84
84
  end
85
85
 
86
86
  def before_save
@@ -101,6 +101,10 @@ module StateManager
101
101
  def persist_state
102
102
  resource.save
103
103
  end
104
+
105
+ def perform_initial_transition?
106
+ !current_state || resource.new_record?
107
+ end
104
108
 
105
109
  end
106
110
  end
@@ -20,12 +20,22 @@ module StateManager
20
20
  self.resource = resource
21
21
  self.context = context
22
22
 
23
- transition_to(initial_state.path) unless current_state
23
+ if perform_initial_transition?
24
+ initial_path = current_state && current_state.path || initial_state.path
25
+ transition_to initial_path, nil
26
+ end
27
+ end
28
+
29
+ # In the case of a new model, we wan't to transition into the initial state
30
+ # and fire the appropriate callbacks. The default behavior is to just check
31
+ # if the state field is nil.
32
+ def perform_initial_transition?
33
+ !current_state
24
34
  end
25
35
 
26
36
  # Transitions to the state at the specified path. The path can be relative
27
37
  # to any state along the current state's path.
28
- def transition_to(path)
38
+ def transition_to(path, current_state=self.current_state)
29
39
  path = path.to_s
30
40
  state = current_state || self
31
41
  exit_states = []
@@ -189,4 +199,4 @@ module StateManager
189
199
 
190
200
  end
191
201
 
192
- end
202
+ end
@@ -0,0 +1,3 @@
1
+ module StateManager
2
+ VERSION = "0.2.12"
3
+ end
@@ -23,6 +23,14 @@ class ActiveRecordTest < Minitest::Test
23
23
  end
24
24
  state :active
25
25
  state :rejected
26
+
27
+ attr_accessor :unsubmitted_entered_count
28
+ attr_accessor :active_entered_count
29
+ def initialize(*args)
30
+ super
31
+ @unsubmitted_entered_count = 0
32
+ @active_entered_count = 0
33
+ end
26
34
 
27
35
  def will_transition(*args)
28
36
  self.before_callbacks_called = true
@@ -31,6 +39,23 @@ class ActiveRecordTest < Minitest::Test
31
39
  def did_transition(*args)
32
40
  self.after_callbacks_called = true
33
41
  end
42
+
43
+ class Unsubmitted
44
+
45
+ def entered
46
+ state_manager.unsubmitted_entered_count += 1
47
+ end
48
+
49
+ end
50
+
51
+ class Active
52
+
53
+ def entered
54
+ state_manager.active_entered_count += 1
55
+ end
56
+
57
+ end
58
+
34
59
  end
35
60
 
36
61
  class Post < ActiveRecord::Base
@@ -183,6 +208,19 @@ class ActiveRecordTest < Minitest::Test
183
208
  @resource.save
184
209
  assert !@resource.state_manager.after_callbacks_called
185
210
  end
211
+ assert_equal @resource.state_manager.unsubmitted_entered_count, 1
212
+ assert @resource.state_manager.after_callbacks_called
213
+ end
214
+
215
+ def test_after_commit_callback_on_different_initial_state
216
+ Post2.transaction do
217
+ @resource = Post2.new(:state => 'active')
218
+ assert !@resource.state_manager.after_callbacks_called
219
+ @resource.save
220
+ assert !@resource.state_manager.after_callbacks_called
221
+ end
222
+ assert_equal @resource.state_manager.unsubmitted_entered_count, 0
223
+ assert_equal @resource.state_manager.active_entered_count, 1
186
224
  assert @resource.state_manager.after_callbacks_called
187
225
  end
188
226
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -71,11 +71,9 @@ files:
71
71
  - ".gitignore"
72
72
  - ".travis.yml"
73
73
  - Gemfile
74
- - Gemfile.lock
75
74
  - LICENSE.txt
76
75
  - README.md
77
76
  - Rakefile
78
- - VERSION
79
77
  - lib/state_manager.rb
80
78
  - lib/state_manager/adapters.rb
81
79
  - lib/state_manager/adapters/active_record.rb
@@ -89,6 +87,7 @@ files:
89
87
  - lib/state_manager/resource.rb
90
88
  - lib/state_manager/serialization.rb
91
89
  - lib/state_manager/state.rb
90
+ - lib/state_manager/version.rb
92
91
  - state_manager.gemspec
93
92
  - test/adapters/active_record_test.rb
94
93
  - test/basic_test.rb
data/Gemfile.lock DELETED
@@ -1,58 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- state_manager (0.2.11)
5
- activesupport
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- activemodel (4.1.1)
11
- activesupport (= 4.1.1)
12
- builder (~> 3.1)
13
- activerecord (4.1.1)
14
- activemodel (= 4.1.1)
15
- activesupport (= 4.1.1)
16
- arel (~> 5.0.0)
17
- activesupport (4.1.1)
18
- i18n (~> 0.6, >= 0.6.9)
19
- json (~> 1.7, >= 1.7.7)
20
- minitest (~> 5.1)
21
- thread_safe (~> 0.1)
22
- tzinfo (~> 1.1)
23
- arel (5.0.1.20140414130214)
24
- builder (3.2.2)
25
- byebug (3.1.2)
26
- columnize (~> 0.8)
27
- debugger-linecache (~> 1.2)
28
- columnize (0.8.9)
29
- database_cleaner (1.3.0)
30
- debugger-linecache (1.2.0)
31
- delayed_job (4.0.1)
32
- activesupport (>= 3.0, < 4.2)
33
- delayed_job_active_record (4.0.1)
34
- activerecord (>= 3.0, < 4.2)
35
- delayed_job (>= 3.0, < 4.1)
36
- i18n (0.6.9)
37
- json (1.8.1)
38
- minitest (5.3.4)
39
- rake (10.3.2)
40
- sqlite3 (1.3.9)
41
- thread_safe (0.3.4)
42
- timecop (0.7.1)
43
- tzinfo (1.2.1)
44
- thread_safe (~> 0.1)
45
-
46
- PLATFORMS
47
- ruby
48
-
49
- DEPENDENCIES
50
- activerecord
51
- bundler
52
- byebug
53
- database_cleaner
54
- delayed_job_active_record
55
- rake
56
- sqlite3
57
- state_manager!
58
- timecop
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.2.11