state_machines-activerecord 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28d56ed7729d05b9e68f5f70804a28c087208062
4
- data.tar.gz: b404daeed3fae75b6f75ea16be408995203d5cc1
3
+ metadata.gz: 05961f0c1b5b3ab64919dcde984de7b32760a238
4
+ data.tar.gz: d1ec7191b20c2a0bb489e07204a01ebdf6df5f8f
5
5
  SHA512:
6
- metadata.gz: cae4bff0c7f3b4c647ede3e3a1fb6f81a0f4864ec03ac0032deefb9f4ac58eec8ed138603282505981c2352350a973dd6071ff9eaaa6346f6d63fa4def8494ca
7
- data.tar.gz: 6110e0ce1e2f7f8b29b46a66e0c72b33083c054e3379154c86b9b9e30b29b355201859a2260ade7620616b81e8441855260f213945bdaa738a6877ad2fa24a2a
6
+ metadata.gz: 38056137768ae3c54e2ac920f88a87f0acf135f3aeaa2b00fb4d5288532a214574a1737dc93e2c598cccb24165491f27917224ef7eaa550d4484dd82b9eceb14
7
+ data.tar.gz: 9362187783fa4720309dc6a27043376a6792d6058aafb7b762cf058d01b73eb087a6009f53d12f2a87db573a72015d18c2c6f66798cce14fd44f546d08407f14
data/README.md CHANGED
@@ -52,6 +52,15 @@ class Vehicle < ActiveRecord::Base
52
52
  end
53
53
  ```
54
54
 
55
+ ### Scopes
56
+ Usage of the generated scopes (assuming default column `state`):
57
+
58
+ ```ruby
59
+ Vehicle.with_state(:parked) # also plural #with_states
60
+ Vehicle.without_states(:first_gear, :second_gear) # also singular #without_state
61
+ ```
62
+
63
+
55
64
  Dependencies
56
65
 
57
66
  Active Record 4.1+
@@ -418,7 +418,7 @@ module StateMachines
418
418
  include ActiveModel
419
419
 
420
420
  # The default options to use for state machines using this integration
421
- @defaults = {:action => :save}
421
+ @defaults = {:action => :save, use_transactions: true}
422
422
  class << self
423
423
  # Classes that inherit from ActiveRecord::Base will automatically use
424
424
  # the ActiveRecord integration.
@@ -493,7 +493,8 @@ module StateMachines
493
493
  end
494
494
 
495
495
  def save!(*)
496
- self.class.state_machine(#{name.inspect}).send(:around_save, self) { super } || raise(ActiveRecord::RecordInvalid.new(self))
496
+ result = self.class.state_machine(#{name.inspect}).send(:around_save, self) { super }
497
+ result || raise(ActiveRecord::RecordInvalid.new(self))
497
498
  end
498
499
 
499
500
  def changed_for_autosave?
@@ -507,9 +508,7 @@ module StateMachines
507
508
 
508
509
  # Runs state events around the machine's :save action
509
510
  def around_save(object)
510
- transaction(object) do
511
- object.class.state_machines.transitions(object, action).perform { yield }
512
- end
511
+ object.class.state_machines.transitions(object, action).perform { yield }
513
512
  end
514
513
 
515
514
  # Creates a scope for finding records *with* a particular state or
@@ -1,7 +1,7 @@
1
1
  module StateMachines
2
2
  module Integrations
3
3
  module ActiveRecord
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
6
6
  end
7
7
  end
@@ -10,14 +10,14 @@ Gem::Specification.new do |spec|
10
10
  spec.email = %w(terminale@gmail.com aaron@pluginaweek.org)
11
11
  spec.summary = %q(State machines Active Record Integration)
12
12
  spec.description = %q(Adds support for creating state machines for attributes on ActiveRecord)
13
- spec.homepage = 'https://github.com/seuros/state_machines-activemodel'
13
+ spec.homepage = 'https://github.com/seuros/state_machines-activerecord'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.test_files = spec.files.grep(/^test\//)
18
18
  spec.require_paths = ['lib']
19
19
 
20
- spec.add_dependency 'state_machines-activemodel', '~> 0.1.0'
20
+ spec.add_dependency 'state_machines-activemodel', '>= 0.3.0'
21
21
  spec.add_dependency 'activerecord' , '~> 4.1'
22
22
  spec.add_development_dependency 'rake', '~> 10.3'
23
23
  spec.add_development_dependency 'sqlite3', '~> 1.3'
@@ -20,7 +20,7 @@ class IntegrationTest < BaseTestCase
20
20
  end
21
21
 
22
22
  def test_should_have_defaults
23
- assert_equal({:action => :save}, StateMachines::Integrations::ActiveRecord.defaults)
23
+ assert_equal({action: :save, use_transactions: true}, StateMachines::Integrations::ActiveRecord.defaults)
24
24
  end
25
25
 
26
26
  def test_should_have_a_locale_path
@@ -176,9 +176,69 @@ class MachineWithEventAttributesOnSaveTest < BaseTestCase
176
176
  assert_equal 'first_gear', @record.state
177
177
  end
178
178
 
179
- def test_should_return_nil_on_manual_rollback
180
- @machine.before_transition { raise ActiveRecord::Rollback }
179
+ def test_should_yield_one_model!
180
+ assert_equal true, @record.save!
181
+ assert_equal 1, @model.count
182
+ end
183
+
184
+ # explicit tests of #save and #save! to ensure expected behavior
185
+ def test_should_yield_two_models_with_before
186
+ @machine.before_transition { @model.create! }
187
+ assert_equal true, @record.save
188
+ assert_equal 2, @model.count
189
+ end
190
+
191
+ def test_should_yield_two_models_with_before!
192
+ @machine.before_transition { @model.create! }
193
+ assert_equal true, @record.save!
194
+ assert_equal 2, @model.count
195
+ end
196
+
197
+ def test_should_raise_on_around_transition_rollback!
198
+ @machine.before_transition { @model.create! }
199
+ @machine.around_transition { @model.create!; raise ActiveRecord::Rollback }
200
+
201
+ raised = false
202
+ begin
203
+ @record.save!
204
+ rescue Exception
205
+ raised = true
206
+ end
181
207
 
208
+ assert_equal true, raised
209
+ assert_equal 0, @model.count
210
+ end
211
+
212
+ def test_should_return_nil_on_around_transition_rollback
213
+ @machine.before_transition { @model.create! }
214
+ @machine.around_transition { @model.create!; raise ActiveRecord::Rollback }
215
+ assert_equal nil, @record.save
216
+ assert_equal 0, @model.count
217
+ end
218
+
219
+ def test_should_return_nil_on_before_transition_rollback
220
+ @machine.before_transition { raise ActiveRecord::Rollback }
182
221
  assert_equal nil, @record.save
222
+ assert_equal 0, @model.count
183
223
  end
224
+
225
+ #
226
+ # @rosskevin - This fails and I'm not sure why, it was existing behavior.
227
+ # see: https://github.com/state-machines/state_machines-activerecord/pull/26#issuecomment-112911886
228
+ #
229
+ # def test_should_yield_three_models_with_before_and_around_save
230
+ # @machine.before_transition { @model.create!; puts "before ran, now #{@model.count}" }
231
+ # @machine.around_transition { @model.create!; puts "around ran, now #{@model.count}" }
232
+ #
233
+ # assert_equal true, @record.save
234
+ # assert_equal 3, @model.count
235
+ # end
236
+ #
237
+ # def test_should_yield_three_models_with_before_and_around_save!
238
+ # @machine.before_transition { @model.create!; puts "before ran, now #{@model.count}" }
239
+ # @machine.around_transition { @model.create!; puts "around ran, now #{@model.count}" }
240
+ #
241
+ # assert_equal true, @record.save!
242
+ # assert_equal 3, @model.count
243
+ # end
184
244
  end
@@ -113,14 +113,31 @@ class MachineWithEventAttributesOnValidationTest < BaseTestCase
113
113
  refute ran_callback
114
114
  end
115
115
 
116
- def test_should_not_run_before_transitions_within_transaction
117
- @machine.before_transition { @model.create; raise ActiveRecord::Rollback }
116
+ def test_should_rollback_before_transitions_with_raise
117
+ @machine.before_transition {
118
+ @model.create;
119
+ raise ActiveRecord::Rollback
120
+ }
118
121
 
119
122
  begin
120
123
  @record.valid?
121
124
  rescue Exception
122
125
  end
123
126
 
124
- assert_equal 1, @model.count
127
+ assert_equal 0, @model.count
128
+ end
129
+
130
+ def test_should_rollback_before_transitions_with_false
131
+ @machine.before_transition {
132
+ @model.create;
133
+ false
134
+ }
135
+
136
+ begin
137
+ @record.valid?
138
+ rescue Exception
139
+ end
140
+
141
+ assert_equal 0, @model.count
125
142
  end
126
143
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machines-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-01 00:00:00.000000000 Z
12
+ date: 2015-07-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: state_machines-activemodel
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: 0.1.0
20
+ version: 0.3.0
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: 0.1.0
27
+ version: 0.3.0
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activerecord
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -183,7 +183,7 @@ files:
183
183
  - test/machine_without_transactions_test.rb
184
184
  - test/model_test.rb
185
185
  - test/test_helper.rb
186
- homepage: https://github.com/seuros/state_machines-activemodel
186
+ homepage: https://github.com/seuros/state_machines-activerecord
187
187
  licenses:
188
188
  - MIT
189
189
  metadata: {}
@@ -203,7 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
203
  version: '0'
204
204
  requirements: []
205
205
  rubyforge_project:
206
- rubygems_version: 2.4.5
206
+ rubygems_version: 2.4.7
207
207
  signing_key:
208
208
  specification_version: 4
209
209
  summary: State machines Active Record Integration
@@ -259,4 +259,3 @@ test_files:
259
259
  - test/machine_without_transactions_test.rb
260
260
  - test/model_test.rb
261
261
  - test/test_helper.rb
262
- has_rdoc: