minimal_state_machine 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -50,7 +50,7 @@ end
50
50
  class IssueState::Solved < MinimalStateMachine::State; end
51
51
  ```
52
52
 
53
- As you can se we define the possible states with an Hash including both the state names and the classes representing the states.
53
+ As you can see we define the possible states with an Hash including both the state names and the classes representing the states.
54
54
  We can provide an optional `initial_state` class method to indicate the initial state (the default value is the first of the keys in the states Hash)
55
55
 
56
56
  What would happen with this configuration:
data/db/.gitkeep ADDED
File without changes
@@ -5,7 +5,7 @@ require 'rails/generators/active_record'
5
5
  class MinimalStateMachineGenerator < Rails::Generators::Base
6
6
  include Rails::Generators::Migration
7
7
 
8
- desc "Creates migration files required by reputation system gem."
8
+ desc "Creates migration files required by minimal state machine gem."
9
9
 
10
10
  self.source_paths << File.join(File.dirname(__FILE__), 'templates')
11
11
 
@@ -9,6 +9,9 @@ module MinimalStateMachine
9
9
  has_one :state, :as => :state_machine, :class_name => 'MinimalStateMachine::State'
10
10
 
11
11
  after_initialize :set_initial_state, :if => proc { state.nil? }
12
+ after_save :destroy_previous_state, :if => proc { previous_state && previous_state != state }
13
+
14
+ attr_accessor :previous_state
12
15
 
13
16
  def self.states
14
17
  {}
@@ -32,7 +35,7 @@ module MinimalStateMachine
32
35
 
33
36
  def transition_to(state_name)
34
37
  if state.class.valid_transition_states.include?(state_name)
35
- state.destroy
38
+ self.previous_state = state
36
39
  self.state = self.class.states[state_name.to_sym].new
37
40
  else
38
41
  raise InvalidTransitionError
@@ -46,5 +49,9 @@ module MinimalStateMachine
46
49
  self.state_name = self.class.states.keys.map(&:to_s).first
47
50
  end
48
51
  end
52
+
53
+ def destroy_previous_state
54
+ previous_state.destroy
55
+ end
49
56
  end
50
57
  end
@@ -1,3 +1,3 @@
1
1
  module MinimalStateMachine
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -52,6 +52,13 @@ describe StateMachine do
52
52
  @state_machine.state.should be_a(StateMachineClosed)
53
53
  end
54
54
 
55
+ it 'destroys the previous state after the transition' do
56
+ @state_machine.state_name = 'closed'
57
+ @state_machine.save
58
+
59
+ MinimalStateMachine::State.count.should == 1
60
+ end
61
+
55
62
  it 'raises an invalid transition error if the new state is not among the allowed transition states' do
56
63
  expect { @state_machine.state_name = 'solved' }.to raise_error('MinimalStateMachine::InvalidTransitionError')
57
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimal_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -88,6 +88,7 @@ files:
88
88
  - LICENSE.txt
89
89
  - README.md
90
90
  - Rakefile
91
+ - db/.gitkeep
91
92
  - lib/generators/minimal_state_machine/minimal_state_machine_generator.rb
92
93
  - lib/generators/minimal_state_machine/templates/create_msm_states.rb
93
94
  - lib/minimal_state_machine.rb