stator 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,12 +8,13 @@ gem 'stator', '~> x.y.z'
8
8
 
9
9
  ## Usage
10
10
 
11
- If you've used the state_machine it's a pretty similar dsl. You define your state machine, transitions, states, and your callbacks (if any). One difference is that stator assumes you've set your db column to the initial state.
11
+ If you've used the state_machine gem it's a pretty similar dsl. You define your state machine, transitions, states, and your callbacks (if any). One difference is that stator assumes you've set your db column's default value to the initial state.
12
12
 
13
13
  ```ruby
14
14
  class User < ActiveRecord::Base
15
15
  extend Stator::Model
16
16
 
17
+ # initial state (column default) is "unactivated"
17
18
  stator do
18
19
 
19
20
  transition :semiactivate do
@@ -44,16 +44,21 @@ module Stator
44
44
  is = self.state
45
45
 
46
46
  if @record.new_record?
47
- unless @machine.matching_transition(::Stator::Transition::ANY, is)
48
- @record.errors.add(@machine.field, "is not a valid state")
49
- end
47
+ invalid_state! unless @machine.matching_transition(::Stator::Transition::ANY, is)
50
48
  else
51
- unless @machine.matching_transition(was, is)
52
- @record.errors.add(@machine.field, "cannot transition to #{is.inspect} from #{was.inspect}")
53
- end
49
+ invalid_transition!(was, is) unless @machine.matching_transition(was, is)
54
50
  end
55
51
  end
56
52
 
53
+ # todo: i18n
54
+ def invalid_state!
55
+ @record.errors.add(@machine.field, "is not a valid state")
56
+ end
57
+
58
+ def invalid_transition!(was, is)
59
+ @record.errors.add(@machine.field, "cannot transition to #{is.inspect} from #{was.inspect}")
60
+ end
61
+
57
62
  def track_transition
58
63
  self.attempt_to_track_state(self.state_was)
59
64
  self.attempt_to_track_state(self.state)
@@ -67,6 +67,8 @@ module Stator
67
67
  options[:use_previous] ||= false
68
68
  %Q{
69
69
  (
70
+ self._stator(#{@namespace.inspect}).integration(self).state_changed?
71
+ ) && (
70
72
  #{@froms.inspect}.include?(self._stator(#{@namespace.inspect}).integration(self).state_was(#{options[:use_previous].inspect})) ||
71
73
  #{@froms.inspect}.include?(::Stator::Transition::ANY)
72
74
  ) && (
@@ -80,12 +82,24 @@ module Stator
80
82
  klass.class_eval <<-EV, __FILE__, __LINE__ + 1
81
83
  def #{@full_name}(should_save = true)
82
84
  integration = self._stator(#{@namespace.inspect}).integration(self)
85
+
86
+ unless can_#{@full_name}?
87
+ integration.invalid_transition!(integration.state, #{@to.inspect}) if should_save
88
+ return false
89
+ end
90
+
83
91
  integration.state = #{@to.inspect}
84
92
  self.save if should_save
85
93
  end
86
94
 
87
95
  def #{@full_name}!
88
96
  integration = self._stator(#{@namespace.inspect}).integration(self)
97
+
98
+ unless can_#{@full_name}?
99
+ integration.invalid_transition!(integration.state, #{@to.inspect})
100
+ raise ActiveRecord::RecordInvalid.new(self)
101
+ end
102
+
89
103
  integration.state = #{@to.inspect}
90
104
  self.save!
91
105
  end
@@ -1,7 +1,7 @@
1
1
  module Stator
2
2
  MAJOR = 0
3
3
  MINOR = 0
4
- PATCH = 14
4
+ PATCH = 15
5
5
  PRERELEASE = nil
6
6
 
7
7
  VERSION = [MAJOR, MINOR, PATCH, PRERELEASE].compact.join('.')
data/spec/model_spec.rb CHANGED
@@ -41,7 +41,16 @@ describe Stator::Model do
41
41
 
42
42
  u.should_not be_valid
43
43
  u.errors[:state].should_not be_empty
44
+ end
45
+
46
+ it 'should not allow a transition that is currently in a `to` state' do
47
+ u = User.new(:email => 'fred@example.com')
48
+ u.activate!
49
+ u.hyperactivate!
44
50
 
51
+ lambda{
52
+ u.hyperactivate!
53
+ }.should raise_error(/cannot transition to \"hyperactivated\" from \"hyperactivated\"/)
45
54
  end
46
55
 
47
56
  it 'should run conditional validations' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-22 00:00:00.000000000 Z
12
+ date: 2014-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord