ministry_of_state 0.1.0 → 0.2.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: fee29b6c22b9b8a1e96cd23c447879ecffa61e66
4
- data.tar.gz: 265f34f8d6f14f2a8044fc7018737b28ad29cf39
3
+ metadata.gz: 9987f8af486f7e33c06b18925622f9b6a696e34f
4
+ data.tar.gz: d3c34e4a6898e962e772baec3a4fb9e5a4c53bfd
5
5
  SHA512:
6
- metadata.gz: 95b251d93d73210d8c70497701df729e64abc286b61b5c6f96b83c800e5488f67744152c8318e70b883815391675391f3966600d4696e604fffcf5d010d3ed3e
7
- data.tar.gz: 3bd244877747f72622cea22d3bf73cceaa5ab6fbd241a7f8e3e248c49e46ffdacec86f2aca7dc8c161fe0dcd236d9ec7a12ca826e5bb71a7dfa787263875b271
6
+ metadata.gz: 165f45dcbbab28525c54d9d7f2a0e533e3dbd7a574a77ab4dde02c9370d1c5a1d88008a76f47dac1fef62a8995c70b4127656a816e36e74532dc75041672050f
7
+ data.tar.gz: 658b915271b7242f849767205eadf63ef108c46478061165b4be87a2f18608a70baa802254830f9f937f961b6e21272efaddf3b14715b3e439a2addc71250193
@@ -2,10 +2,13 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
+ - 2.1.5
5
6
 
6
7
  gemfile:
7
8
  - Gemfile
8
- - gemfiles/Gemfile.rails-3.2.13
9
+ - gemfiles/Gemfile.rails-3.2.x
10
+ - gemfiles/Gemfile.rails-4.0.x
11
+ - gemfiles/Gemfile.rails-4.1.x
9
12
 
10
13
  script:
11
14
  - bundle exec rake test
@@ -8,8 +8,8 @@ Because State machines are simple, they do not have to be greater than 2000 line
8
8
  = Features
9
9
  * Supports rails4 now. No changes required.
10
10
  * Events are wrapped in a transaction and record is locked before going for a state change.
11
- * Ignores dirty attributes (still on the fence with this one)
12
- * Events and states can be inhereted across classes.
11
+ * Ignores dirty attributes while going for state change. This means unlike AASM you can't use an event transition as shorthand of `save`.
12
+ * Events and states can be inherited across classes.
13
13
  * Simple 136 lines of code, covered with close to > 98% test coverage.
14
14
 
15
15
  = Using it
@@ -0,0 +1,10 @@
1
+ # -*- ruby -*-
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gemspec path: '..'
6
+ gem 'rails', '~> 3.2.13'
7
+
8
+ # rails 3.2.x depends on minitest 4.x, so i had to hardcode it.
9
+ # rails 4.x depends on minitest 5.x which is default now
10
+ gem 'minitest', '~> 4.7.5'
@@ -0,0 +1,6 @@
1
+ # -*- ruby -*-
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gemspec path: '..'
6
+ gem 'rails', '~> 4.0.0'
@@ -0,0 +1,6 @@
1
+ # -*- ruby -*-
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gemspec path: '..'
6
+ gem 'rails', '~> 4.1.0'
@@ -37,7 +37,7 @@ module MinistryOfState
37
37
  self.states = HashWithIndifferentAccess.new
38
38
  self.events = HashWithIndifferentAccess.new
39
39
 
40
- before_create :set_initial_states
40
+ after_initialize :set_initial_states_for_new_record
41
41
  after_create :run_initial_state_actions
42
42
  end
43
43
  end
@@ -83,6 +83,10 @@ module MinistryOfState
83
83
  end
84
84
 
85
85
  def transitions(opts = {})
86
+ if opts[:from] == :any
87
+ opts[:from] = self.states.keys
88
+ end
89
+
86
90
  if opts[:from].blank? || opts[:to].blank?
87
91
  raise TransitionNotAllowed.new("You need to specify from and to states")
88
92
  end
@@ -108,6 +112,11 @@ module MinistryOfState
108
112
  end
109
113
  end
110
114
 
115
+
116
+ def set_initial_states_for_new_record
117
+ set_initial_states if new_record?
118
+ end
119
+
111
120
  def run_initial_state_actions
112
121
  states.each do |name, state|
113
122
  next unless state.initial?
@@ -129,7 +138,8 @@ module MinistryOfState
129
138
 
130
139
  def check_state(state)
131
140
  column = states[state].column
132
- send("#{column}_was") == state.to_s
141
+ method = new_record? ? column : "#{column}_was"
142
+ send(method) == state.to_s
133
143
  end
134
144
 
135
145
  def fire(event)
@@ -1,3 +1,3 @@
1
1
  module MinistryOfState
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -8,10 +8,15 @@ class Blog < ActiveRecord::Base
8
8
  add_initial_state 'pending'
9
9
 
10
10
  add_state :approved, :enter => :test_method
11
+ add_state :cancelled
11
12
 
12
13
  add_event :approve do
13
14
  transitions :from => 'pending', :to => 'approved'
14
15
  end
16
+
17
+ add_event(:cancel) do
18
+ transitions from: :any, to: 'cancelled'
19
+ end
15
20
  end
16
21
 
17
22
  def test_method
@@ -7,7 +7,9 @@ rescue Bundler::BundlerError => e
7
7
  $stderr.puts "Run `bundle install` to install missing gems"
8
8
  exit e.status_code
9
9
  end
10
- require 'test/unit'
10
+
11
+ require 'minitest/autorun'
12
+ require 'minitest/unit'
11
13
  require 'shoulda'
12
14
 
13
15
  # TODO: Fix this one when shoulda-context patches MiniTest::Unit::TestCase
@@ -147,6 +147,26 @@ class TestMinistryOfState < ActiveSupport::TestCase
147
147
  end
148
148
  end
149
149
 
150
+ context "can specify from transition as any" do
151
+ should "cancel a pending blog" do
152
+ @pending_blog = Blog.create(status: 'pending', text: "some text")
153
+ assert @pending_blog.cancel!
154
+ assert @pending_blog.errors.blank?
155
+ end
156
+
157
+ should "cancel a approved blog" do
158
+ @approved_blog = Blog.create(status: 'approved', text: "some text")
159
+ assert @approved_blog.cancel!
160
+ assert @approved_blog.errors.blank?
161
+ end
162
+
163
+ should "cancel an already cancelled blog" do
164
+ @cancelled_blog = Blog.create(status: 'cancelled', text: "some text")
165
+ assert @cancelled_blog.cancel!
166
+ assert @cancelled_blog.errors.blank?
167
+ end
168
+ end
169
+
150
170
  context "calling enter and exit callbacks for normal events" do
151
171
  setup do
152
172
  @post = Post.create(:title => "Hello", :content => "Good world")
@@ -227,4 +247,35 @@ class TestMinistryOfState < ActiveSupport::TestCase
227
247
  assert @cargo.paid?
228
248
  end
229
249
  end
250
+
251
+ context 'status helpers without persisting objects' do
252
+ should 'be able to read value from mos column' do
253
+ @cargo = Cargo.new
254
+ assert @cargo.unpaid?
255
+ end
256
+
257
+ should 'be able to read updated value from mos column' do
258
+ @cargo = Cargo.new
259
+ @cargo.payment = 'paid'
260
+ assert @cargo.paid?
261
+ end
262
+
263
+ should 'be able to persist with a different state' do
264
+ @cargo = Cargo.create(payment: 'paid')
265
+ assert @cargo.reload.paid?
266
+ end
267
+ end
268
+
269
+ context 'fetching records from database' do
270
+ setup do
271
+ @cargo = Cargo.create(payment: 'paid')
272
+ end
273
+
274
+ should 'not fire set initial state callback' do
275
+ Cargo.any_instance.expects(:set_initial_state).times(0)
276
+
277
+ fetch_cargo_from_db = Cargo.find(@cargo.id)
278
+ assert fetch_cargo_from_db.paid?
279
+ end
280
+ end
230
281
  end
metadata CHANGED
@@ -1,97 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ministry_of_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hemant Kumar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-02 00:00:00.000000000 Z
11
+ date: 2014-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.2.13
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.2.13
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: sqlite3
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.3.7
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.3.7
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: shoulda
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: 3.5.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 3.5.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: mocha
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: 0.14.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ~>
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: 0.14.0
97
97
  description: A ActiveRecord plugin for working with state machines
@@ -103,15 +103,16 @@ extra_rdoc_files:
103
103
  - LICENSE.txt
104
104
  - README.rdoc
105
105
  files:
106
- - .document
107
- - .gitignore
108
- - .travis.yml
106
+ - ".document"
107
+ - ".gitignore"
108
+ - ".travis.yml"
109
109
  - Gemfile
110
110
  - LICENSE.txt
111
111
  - README.rdoc
112
112
  - Rakefile
113
- - VERSION
114
- - gemfiles/Gemfile.rails-3.2.13
113
+ - gemfiles/Gemfile.rails-3.2.x
114
+ - gemfiles/Gemfile.rails-4.0.x
115
+ - gemfiles/Gemfile.rails-4.1.x
115
116
  - lib/ministry_of_state.rb
116
117
  - lib/ministry_of_state/ministry_of_state.rb
117
118
  - lib/ministry_of_state/railtie.rb
@@ -135,17 +136,17 @@ require_paths:
135
136
  - lib
136
137
  required_ruby_version: !ruby/object:Gem::Requirement
137
138
  requirements:
138
- - - '>='
139
+ - - ">="
139
140
  - !ruby/object:Gem::Version
140
141
  version: '0'
141
142
  required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  requirements:
143
- - - '>='
144
+ - - ">="
144
145
  - !ruby/object:Gem::Version
145
146
  version: '0'
146
147
  requirements: []
147
148
  rubyforge_project:
148
- rubygems_version: 2.0.3
149
+ rubygems_version: 2.2.2
149
150
  signing_key:
150
151
  specification_version: 4
151
152
  summary: Handling state machines
@@ -158,3 +159,4 @@ test_files:
158
159
  - test/student.rb
159
160
  - test/test_ministry_of_state.rb
160
161
  - test/user.rb
162
+ has_rdoc: true
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.1
@@ -1,6 +0,0 @@
1
- # -*- ruby -*-
2
-
3
- source "http://rubygems.org"
4
-
5
- gemspec path: '..'
6
- gem 'rails', "~> 3.2.13"