transitions 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4680820f6d1835b1e12df126890a7c898070b74b
4
+ data.tar.gz: 98e7433625dee0e047fda25da2c46611c0d93228
5
+ SHA512:
6
+ metadata.gz: 3c91efda00de051d2b0990d6bc82fd71d9eb27ad41e6f8d44ed6adf85e37a77a1372a0f824b04b3e44a0d8c72963fa410804e6f8bcaed6732315468acb4f3c7e
7
+ data.tar.gz: 5471dd6553cc527e69e90571ef68709ec7dd28678f96cb43d9e6d9df07c0ecb78a05c014d0e361d80365d121ad4164af10dd92cd9ab87b7e19af9637ea953cfe
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ transitions
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.1.10
2
+
3
+ * (troessner) Fix: Using a custom ActiveRecord select query without the name of the state attribute would trigger an exception.
4
+
1
5
  # 0.1.9
2
6
 
3
7
  * (barelyknow) add can_transition? and cant_transition? methods
data/README.md CHANGED
@@ -1,10 +1,7 @@
1
1
  ### Overview
2
-
3
- [<img
4
- src="https://secure.travis-ci.org/troessner/transitions.png?branch=master"/>](
5
- http://travis-ci.org/troessner/transitions) [<img
6
- src="https://badge.fury.io/rb/transitions.png" alt="Gem Version"
7
- />](http://badge.fury.io/rb/transitions)
2
+ [![Build status](https://secure.travis-ci.org/troessner/transitions.png?branch=master)](http://travis-ci.org/troessner/transitions)
3
+ [![Gem Version](https://badge.fury.io/rb/transitions.png)](http://badge.fury.io/rb/transitions)
4
+ [![Code Climate](https://codeclimate.com/github/troessner/transitions.png)](https://codeclimate.com/github/troessner/transitions)
8
5
 
9
6
  ### Synopsis
10
7
 
@@ -71,7 +68,7 @@ wherever you load your dependencies in your application.
71
68
  * You can only use one state machine per model. While in theory you can
72
69
  define two or more, this won't work as you would expect. Not supporting
73
70
  this was intentional, if you're interested in the ratione look up version
74
- 1.0.0 in the CHANGELOG.
71
+ 0.1.0 in the CHANGELOG.
75
72
 
76
73
  * Use symbols, not strings for declaring the state machine. Using strings is
77
74
  **not** supported as is using whitespace in names (because `transitions`
@@ -110,6 +107,11 @@ In addition, a `can_transition?` method is added to the object that expects one
110
107
  >> Product.new.can_transition? :out_of_stock
111
108
  => true
112
109
 
110
+ If you need to get all available transitions for current state you can simply call:
111
+
112
+ >> Product.new.available_transitions
113
+ => [:discontinued, :out_of_stock]
114
+
113
115
  #### Automatic scope generation
114
116
 
115
117
  `transitions` will automatically generate scopes for you if you are using
@@ -142,12 +144,18 @@ you can use this feature a la:
142
144
  Each event definition takes an optional `guard` argument, which acts as a
143
145
  predicate for the transition.
144
146
 
145
- You can pass in a Symbol, a String, or a Proc like this:
147
+ You can pass in Symbols, Strings, or Procs like this:
146
148
 
147
149
  event :discontinue do
148
150
  transitions :to => :discontinued, :from => [:available, :out_of_stock], :guard => :can_discontinue
149
151
  end
150
152
 
153
+ or
154
+
155
+ event :discontinue do
156
+ transitions :to => :discontinued, :from => [:available, :out_of_stock], :guard => [:can_discontinue, :super_sure?]
157
+ end
158
+
151
159
  Any arguments passed to the event method will be passed on to the `guard`
152
160
  predicate.
153
161
 
@@ -87,8 +87,11 @@ module ActiveModel
87
87
  end
88
88
 
89
89
  def set_initial_state
90
- self[transitions_state_column_name] ||= self.class.get_state_machine.initial_state.to_s if self.has_attribute?(transitions_state_column_name)
91
- self.class.get_state_machine.state_index[self[transitions_state_column_name].to_sym].call_action(:enter, self)
90
+ # In case we use a query with a custom select that excludes our state attribute name we need to skip the initialization below.
91
+ if self.has_attribute?(transitions_state_column_name)
92
+ self[transitions_state_column_name] ||= self.class.get_state_machine.initial_state.to_s
93
+ self.class.get_state_machine.state_index[self[transitions_state_column_name].to_sym].call_action(:enter, self)
94
+ end
92
95
  end
93
96
 
94
97
  def state_presence
@@ -30,11 +30,15 @@ module Transitions
30
30
  end
31
31
 
32
32
  def perform(obj, *args)
33
- case @guard
33
+ [@guard].flatten.all? { |g| perform_guard(obj, g, *args) }
34
+ end
35
+
36
+ def perform_guard(obj, guard, *args)
37
+ case guard
34
38
  when Symbol, String
35
- obj.send(@guard, *args)
39
+ obj.send(guard, *args)
36
40
  when Proc
37
- @guard.call(obj, *args)
41
+ guard.call(obj, *args)
38
42
  else
39
43
  true
40
44
  end
@@ -1,3 +1,3 @@
1
1
  module Transitions
2
- VERSION = '0.1.9'
2
+ VERSION = '0.1.10'
3
3
  end
data/lib/transitions.rb CHANGED
@@ -72,6 +72,10 @@ module Transitions
72
72
  instance_variable_set(ivar, new_state)
73
73
  end
74
74
 
75
+ def available_transitions
76
+ self.class.get_state_machine.events_for(current_state)
77
+ end
78
+
75
79
  def can_transition?(*events)
76
80
  events.all? do |event|
77
81
  self.class.get_state_machine.events_for(current_state).include?(event.to_sym)
@@ -0,0 +1,36 @@
1
+ require "helper"
2
+
3
+ # Regressiontest for https://github.com/troessner/transitions/issues/95
4
+ # TODO We use this Trafficlight class quite a lot in our specs including a ton of duplication.
5
+ # Unify class and migration definition in one place and then clean up all related specs, including this one.
6
+ class CreateTrafficLights < ActiveRecord::Migration
7
+ def self.up
8
+ create_table(:traffic_lights, :force => true) do |t|
9
+ t.string :state
10
+ t.string :name
11
+ end
12
+ end
13
+ end
14
+
15
+ class TrafficLight < ActiveRecord::Base
16
+ include ActiveModel::Transitions
17
+
18
+ state_machine do
19
+ state :off
20
+ state :on
21
+ end
22
+ end
23
+
24
+ class TestCustomSelect < Test::Unit::TestCase
25
+ def setup
26
+ set_up_db CreateTrafficLights
27
+ @light = TrafficLight.create!
28
+ end
29
+
30
+ test "should not trigger an exception when we use a custom select query which excludes the name of our state attribute" do
31
+ result = TrafficLight.select("id, name")
32
+ assert_nothing_raised NoMethodError do
33
+ result.inspect
34
+ end
35
+ end
36
+ end
@@ -36,6 +36,13 @@ class TransitionsMachineTest < Test::Unit::TestCase
36
36
  assert events.include?(:timeout)
37
37
  end
38
38
 
39
+ test "knows all available transitions for current state" do
40
+ machine = MachineTestSubject.new
41
+ assert_equal [:restart], machine.available_transitions
42
+ machine.restart
43
+ assert_equal [:shutdown, :timeout], machine.available_transitions
44
+ end
45
+
39
46
  test "knows that it can use a transition when it is available" do
40
47
  machine = MachineTestSubject.new
41
48
  machine.restart
@@ -39,4 +39,16 @@ class TestStateTransitionGuardCheck < Test::Unit::TestCase
39
39
 
40
40
  st.perform(obj, *args)
41
41
  end
42
+
43
+ test "should call the method on the object if guard is a symbol" do
44
+ opts = {:from => "foo", :to => "bar", :guard => [:test_guard, :test_another_guard] }
45
+ st = Transitions::StateTransition.new(opts)
46
+
47
+ obj = stub
48
+ obj.expects(:test_guard).with(*args).returns(true)
49
+ obj.expects(:test_another_guard).with(*args).returns(true)
50
+
51
+ assert st.perform(obj, *args)
52
+ end
53
+
42
54
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transitions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
5
- prerelease:
4
+ version: 0.1.10
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jakub Kuzma
@@ -10,12 +9,11 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2013-05-26 00:00:00.000000000 Z
12
+ date: 2013-08-07 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: bundler
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
18
  - - ~>
21
19
  - !ruby/object:Gem::Version
@@ -23,7 +21,6 @@ dependencies:
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
25
  - - ~>
29
26
  - !ruby/object:Gem::Version
@@ -31,7 +28,6 @@ dependencies:
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: test-unit
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
32
  - - ~>
37
33
  - !ruby/object:Gem::Version
@@ -39,7 +35,6 @@ dependencies:
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
39
  - - ~>
45
40
  - !ruby/object:Gem::Version
@@ -47,7 +42,6 @@ dependencies:
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: mocha
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
46
  - - ~>
53
47
  - !ruby/object:Gem::Version
@@ -55,7 +49,6 @@ dependencies:
55
49
  type: :development
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
53
  - - ~>
61
54
  - !ruby/object:Gem::Version
@@ -63,55 +56,48 @@ dependencies:
63
56
  - !ruby/object:Gem::Dependency
64
57
  name: rake
65
58
  requirement: !ruby/object:Gem::Requirement
66
- none: false
67
59
  requirements:
68
- - - ! '>='
60
+ - - '>='
69
61
  - !ruby/object:Gem::Version
70
62
  version: '0'
71
63
  type: :development
72
64
  prerelease: false
73
65
  version_requirements: !ruby/object:Gem::Requirement
74
- none: false
75
66
  requirements:
76
- - - ! '>='
67
+ - - '>='
77
68
  - !ruby/object:Gem::Version
78
69
  version: '0'
79
70
  - !ruby/object:Gem::Dependency
80
71
  name: random_data
81
72
  requirement: !ruby/object:Gem::Requirement
82
- none: false
83
73
  requirements:
84
- - - ! '>='
74
+ - - '>='
85
75
  - !ruby/object:Gem::Version
86
76
  version: '0'
87
77
  type: :development
88
78
  prerelease: false
89
79
  version_requirements: !ruby/object:Gem::Requirement
90
- none: false
91
80
  requirements:
92
- - - ! '>='
81
+ - - '>='
93
82
  - !ruby/object:Gem::Version
94
83
  version: '0'
95
84
  - !ruby/object:Gem::Dependency
96
85
  name: sqlite3
97
86
  requirement: !ruby/object:Gem::Requirement
98
- none: false
99
87
  requirements:
100
- - - ! '>='
88
+ - - '>='
101
89
  - !ruby/object:Gem::Version
102
90
  version: '0'
103
91
  type: :development
104
92
  prerelease: false
105
93
  version_requirements: !ruby/object:Gem::Requirement
106
- none: false
107
94
  requirements:
108
- - - ! '>='
95
+ - - '>='
109
96
  - !ruby/object:Gem::Version
110
97
  version: '0'
111
98
  - !ruby/object:Gem::Dependency
112
99
  name: activerecord
113
100
  requirement: !ruby/object:Gem::Requirement
114
- none: false
115
101
  requirements:
116
102
  - - ~>
117
103
  - !ruby/object:Gem::Version
@@ -119,7 +105,6 @@ dependencies:
119
105
  type: :development
120
106
  prerelease: false
121
107
  version_requirements: !ruby/object:Gem::Requirement
122
- none: false
123
108
  requirements:
124
109
  - - ~>
125
110
  - !ruby/object:Gem::Version
@@ -131,7 +116,8 @@ extensions: []
131
116
  extra_rdoc_files: []
132
117
  files:
133
118
  - .gitignore
134
- - .rvmrc
119
+ - .ruby-gemset
120
+ - .ruby-version
135
121
  - .travis.yml
136
122
  - CHANGELOG.md
137
123
  - Gemfile
@@ -149,6 +135,7 @@ files:
149
135
  - test/active_record/test_active_record.rb
150
136
  - test/active_record/test_active_record_scopes.rb
151
137
  - test/active_record/test_active_record_timestamps.rb
138
+ - test/active_record/test_custom_select.rb
152
139
  - test/event/test_event.rb
153
140
  - test/event/test_event_arguments.rb
154
141
  - test/event/test_event_being_fired.rb
@@ -167,29 +154,25 @@ files:
167
154
  - transitions.gemspec
168
155
  homepage: http://github.com/troessner/transitions
169
156
  licenses: []
157
+ metadata: {}
170
158
  post_install_message:
171
159
  rdoc_options: []
172
160
  require_paths:
173
161
  - lib
174
162
  required_ruby_version: !ruby/object:Gem::Requirement
175
- none: false
176
163
  requirements:
177
- - - ! '>='
164
+ - - '>='
178
165
  - !ruby/object:Gem::Version
179
166
  version: '0'
180
- segments:
181
- - 0
182
- hash: -611477343
183
167
  required_rubygems_version: !ruby/object:Gem::Requirement
184
- none: false
185
168
  requirements:
186
- - - ! '>='
169
+ - - '>='
187
170
  - !ruby/object:Gem::Version
188
171
  version: 1.3.6
189
172
  requirements: []
190
173
  rubyforge_project: transitions
191
- rubygems_version: 1.8.25
174
+ rubygems_version: 2.0.6
192
175
  signing_key:
193
- specification_version: 3
176
+ specification_version: 4
194
177
  summary: State machine extracted from ActiveModel
195
178
  test_files: []
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm 1.9.3@transitions --create