state_machines-activerecord 0.9.0 → 0.10.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 +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +1 -2
- data/lib/state_machines/integrations/active_record/locale.rb +2 -0
- data/lib/state_machines/integrations/active_record/version.rb +3 -1
- data/lib/state_machines/integrations/active_record.rb +22 -81
- data/lib/state_machines-activerecord.rb +2 -0
- data/test/files/models/post.rb +2 -0
- data/test/integration_test.rb +2 -0
- data/test/machine_by_default_test.rb +2 -0
- data/test/machine_errors_test.rb +2 -0
- data/test/machine_multiple_test.rb +2 -0
- data/test/machine_nested_action_test.rb +2 -0
- data/test/machine_unmigrated_test.rb +2 -0
- data/test/machine_with_aliased_attribute_test.rb +2 -0
- data/test/machine_with_callbacks_test.rb +2 -0
- data/test/machine_with_column_state_attribute_test.rb +2 -0
- data/test/machine_with_complex_pluralization_scopes_test.rb +2 -0
- data/test/machine_with_conflicting_predicate_test.rb +2 -0
- data/test/machine_with_conflicting_state_name_test.rb +4 -2
- data/test/machine_with_custom_attribute_test.rb +2 -0
- data/test/machine_with_default_scope_test.rb +2 -0
- data/test/machine_with_different_column_default_test.rb +2 -0
- data/test/machine_with_different_integer_column_default_test.rb +2 -0
- data/test/machine_with_dirty_attribute_and_custom_attributes_during_loopback_test.rb +2 -0
- data/test/machine_with_dirty_attribute_and_state_events_test.rb +2 -0
- data/test/machine_with_dirty_attributes_and_custom_attribute_test.rb +2 -0
- data/test/machine_with_dirty_attributes_during_loopback_test.rb +2 -0
- data/test/machine_with_dirty_attributes_test.rb +2 -0
- data/test/machine_with_dynamic_initial_state_test.rb +2 -0
- data/test/machine_with_event_attributes_on_autosave_test.rb +2 -0
- data/test/machine_with_event_attributes_on_custom_action_test.rb +2 -0
- data/test/machine_with_event_attributes_on_save_bang_test.rb +2 -0
- data/test/machine_with_event_attributes_on_save_test.rb +2 -0
- data/test/machine_with_event_attributes_on_validation_test.rb +2 -0
- data/test/machine_with_events_test.rb +2 -0
- data/test/machine_with_failed_action_test.rb +2 -0
- data/test/machine_with_failed_after_callbacks_test.rb +2 -0
- data/test/machine_with_failed_before_callbacks_test.rb +2 -0
- data/test/machine_with_initialized_state_test.rb +2 -0
- data/test/machine_with_internationalization_test.rb +2 -0
- data/test/machine_with_loopback_test.rb +2 -0
- data/test/machine_with_non_column_state_attribute_defined_test.rb +2 -0
- data/test/machine_with_same_column_default_test.rb +2 -0
- data/test/machine_with_same_integer_column_default_test.rb +2 -0
- data/test/machine_with_scopes_and_joins_test.rb +2 -0
- data/test/machine_with_scopes_and_owner_subclass_test.rb +2 -0
- data/test/machine_with_scopes_test.rb +2 -0
- data/test/machine_with_state_driven_validations_test.rb +2 -0
- data/test/machine_with_states_test.rb +2 -0
- data/test/machine_with_static_initial_state_test.rb +2 -0
- data/test/machine_with_transactions_test.rb +2 -0
- data/test/machine_with_validations_and_custom_attribute_test.rb +2 -0
- data/test/machine_with_validations_test.rb +2 -0
- data/test/machine_without_database_test.rb +2 -0
- data/test/machine_without_transactions_test.rb +2 -0
- data/test/model_test.rb +2 -0
- data/test/test_helper.rb +3 -4
- metadata +8 -11
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a5e8b447b65a1f99ad367425556a3e726b3f42be5056af03a2a48e741851ff72
|
|
4
|
+
data.tar.gz: 801a7943d18a71f506acefedb764cb8ed0a9caa1c459893df99f3b21419c78f7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c6794e0f3d9e6835682f4a541bb8bde8210599749621c262ed164bae482c78f3ec40aa6cc4279db5ed62583bd5d2f30476f43f49f5adc149ac80eb9df20ee487
|
|
7
|
+
data.tar.gz: 0fc0b42cc6de4c17f4a844634647dd0e0245933c6a713cf51e68655b4354e03801b0a6b63e61c1e27298a1db56ae8559a605972e2a6ef3cc879f32f75691c3b6
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
[](https://codeclimate.com/github/state-machines/state_machines-activerecord)
|
|
1
|
+
[](https://github.com/state-machines/state_machines-activerecord/actions/workflows/ruby.yml)
|
|
3
2
|
|
|
4
3
|
# StateMachines Active Record Integration
|
|
5
4
|
|
|
@@ -78,16 +78,14 @@ module StateMachines
|
|
|
78
78
|
# === Security implications
|
|
79
79
|
#
|
|
80
80
|
# Beware that public event attributes mean that events can be fired
|
|
81
|
-
# whenever mass-assignment is being used.
|
|
82
|
-
# users from tampering with events through URLs / forms,
|
|
83
|
-
#
|
|
84
|
-
#
|
|
85
|
-
# class
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
# state_machine do
|
|
90
|
-
# ...
|
|
81
|
+
# whenever mass-assignment is being used. If you want to prevent malicious
|
|
82
|
+
# users from tampering with events through URLs / forms, you should use
|
|
83
|
+
# Rails' strong parameters to control which attributes are permitted:
|
|
84
|
+
#
|
|
85
|
+
# class VehiclesController < ApplicationController
|
|
86
|
+
# def vehicle_params
|
|
87
|
+
# params.require(:vehicle).permit(:color, :make, :model)
|
|
88
|
+
# # Exclude state_event to prevent tampering
|
|
91
89
|
# end
|
|
92
90
|
# end
|
|
93
91
|
#
|
|
@@ -95,8 +93,7 @@ module StateMachines
|
|
|
95
93
|
# you can build two state machines (one public and one protected) like so:
|
|
96
94
|
#
|
|
97
95
|
# class Vehicle < ApplicationRecord
|
|
98
|
-
#
|
|
99
|
-
#
|
|
96
|
+
# # Define private machine
|
|
100
97
|
# state_machine do
|
|
101
98
|
# # Define private events here
|
|
102
99
|
# end
|
|
@@ -105,6 +102,8 @@ module StateMachines
|
|
|
105
102
|
# state_machine :public_state, :attribute => :state do
|
|
106
103
|
# # Define public events here
|
|
107
104
|
# end
|
|
105
|
+
#
|
|
106
|
+
# # Control access via strong parameters in your controller
|
|
108
107
|
# end
|
|
109
108
|
#
|
|
110
109
|
# == Transactions
|
|
@@ -199,27 +198,28 @@ module StateMachines
|
|
|
199
198
|
#
|
|
200
199
|
# == Scopes
|
|
201
200
|
#
|
|
202
|
-
# To assist in filtering models with specific states, a series of
|
|
203
|
-
#
|
|
201
|
+
# To assist in filtering models with specific states, a series of scopes
|
|
202
|
+
# are defined on the model for finding records with or without a
|
|
204
203
|
# particular set of states.
|
|
205
204
|
#
|
|
206
|
-
# These
|
|
205
|
+
# These scopes are essentially the functional equivalent of the
|
|
207
206
|
# following definitions:
|
|
208
207
|
#
|
|
209
208
|
# class Vehicle < ApplicationRecord
|
|
210
209
|
# # with_states also aliased to with_state
|
|
210
|
+
# scope :with_states, ->(states) { where(state: states) }
|
|
211
211
|
#
|
|
212
|
-
# named_scope :without_states, lambda {|*states| {:conditions => ['state NOT IN (?)', states]}}
|
|
213
212
|
# # without_states also aliased to without_state
|
|
213
|
+
# scope :without_states, ->(states) { where.not(state: states) }
|
|
214
214
|
# end
|
|
215
215
|
#
|
|
216
216
|
# *Note*, however, that the states are converted to their stored values
|
|
217
217
|
# before being passed into the query.
|
|
218
218
|
#
|
|
219
|
-
# Because of the way
|
|
219
|
+
# Because of the way scopes work in ActiveRecord, they can be
|
|
220
220
|
# chained like so:
|
|
221
221
|
#
|
|
222
|
-
# Vehicle.with_state(:parked).
|
|
222
|
+
# Vehicle.with_state(:parked).order(id: :desc)
|
|
223
223
|
#
|
|
224
224
|
# Note that states can also be referenced by the string version of their
|
|
225
225
|
# name:
|
|
@@ -266,7 +266,7 @@ module StateMachines
|
|
|
266
266
|
# your callback to roll back. You can work around this issue like so:
|
|
267
267
|
#
|
|
268
268
|
# class TransitionLog < ApplicationRecord
|
|
269
|
-
#
|
|
269
|
+
# connects_to database: { writing: :primary, reading: :primary }
|
|
270
270
|
# end
|
|
271
271
|
#
|
|
272
272
|
# class Vehicle < ApplicationRecord
|
|
@@ -279,7 +279,7 @@ module StateMachines
|
|
|
279
279
|
# end
|
|
280
280
|
# end
|
|
281
281
|
#
|
|
282
|
-
# The +TransitionLog+ model establishes a
|
|
282
|
+
# The +TransitionLog+ model establishes a separate connection to the database
|
|
283
283
|
# that allows new records to be saved without being affected by rollbacks
|
|
284
284
|
# in the +Vehicle+ model's transaction.
|
|
285
285
|
#
|
|
@@ -304,65 +304,9 @@ module StateMachines
|
|
|
304
304
|
# * (-) end transaction (if enabled)
|
|
305
305
|
# * (9) after_commit
|
|
306
306
|
#
|
|
307
|
-
# == Observers
|
|
308
|
-
#
|
|
309
|
-
# In addition to support for ActiveRecord-like hooks, there is additional
|
|
310
|
-
# support for ActiveRecord observers. Because of the way ActiveRecord
|
|
311
|
-
# observers are designed, there is less flexibility around the specific
|
|
312
|
-
# transitions that can be hooked in. However, a large number of hooks
|
|
313
|
-
# *are* supported. For example, if a transition for a record's +state+
|
|
314
|
-
# attribute changes the state from +parked+ to +idling+ via the +ignite+
|
|
315
|
-
# event, the following observer methods are supported:
|
|
316
|
-
# * before/after/after_failure_to-_ignite_from_parked_to_idling
|
|
317
|
-
# * before/after/after_failure_to-_ignite_from_parked
|
|
318
|
-
# * before/after/after_failure_to-_ignite_to_idling
|
|
319
|
-
# * before/after/after_failure_to-_ignite
|
|
320
|
-
# * before/after/after_failure_to-_transition_state_from_parked_to_idling
|
|
321
|
-
# * before/after/after_failure_to-_transition_state_from_parked
|
|
322
|
-
# * before/after/after_failure_to-_transition_state_to_idling
|
|
323
|
-
# * before/after/after_failure_to-_transition_state
|
|
324
|
-
# * before/after/after_failure_to-_transition
|
|
325
|
-
#
|
|
326
|
-
# The following class shows an example of some of these hooks:
|
|
327
|
-
#
|
|
328
|
-
# class VehicleObserver < ActiveRecord::Observer
|
|
329
|
-
# def before_save(vehicle)
|
|
330
|
-
# # log message
|
|
331
|
-
# end
|
|
332
|
-
#
|
|
333
|
-
# # Callback for :ignite event *before* the transition is performed
|
|
334
|
-
# def before_ignite(vehicle, transition)
|
|
335
|
-
# # log message
|
|
336
|
-
# end
|
|
337
|
-
#
|
|
338
|
-
# # Callback for :ignite event *after* the transition has been performed
|
|
339
|
-
# def after_ignite(vehicle, transition)
|
|
340
|
-
# # put on seatbelt
|
|
341
|
-
# end
|
|
342
|
-
#
|
|
343
|
-
# # Generic transition callback *before* the transition is performed
|
|
344
|
-
# def after_transition(vehicle, transition)
|
|
345
|
-
# Audit.log(vehicle, transition)
|
|
346
|
-
# end
|
|
347
|
-
# end
|
|
348
|
-
#
|
|
349
|
-
# More flexible transition callbacks can be defined directly within the
|
|
350
|
-
# model as described in StateMachines::Machine#before_transition
|
|
351
|
-
# and StateMachines::Machine#after_transition.
|
|
352
|
-
#
|
|
353
|
-
# To define a single observer for multiple state machines:
|
|
354
|
-
#
|
|
355
|
-
# class StateMachineObserver < ActiveRecord::Observer
|
|
356
|
-
# observe Vehicle, Switch, Project
|
|
357
|
-
#
|
|
358
|
-
# def after_transition(record, transition)
|
|
359
|
-
# Audit.log(record, transition)
|
|
360
|
-
# end
|
|
361
|
-
# end
|
|
362
|
-
#
|
|
363
307
|
# == Internationalization
|
|
364
308
|
#
|
|
365
|
-
#
|
|
309
|
+
# Any error message that is generated from performing invalid
|
|
366
310
|
# transitions can be localized. The following default translations are used:
|
|
367
311
|
#
|
|
368
312
|
# en:
|
|
@@ -375,9 +319,6 @@ module StateMachines
|
|
|
375
319
|
# # %{value} = attribute value, %{event} = Human event name, %{state} = Human current state name
|
|
376
320
|
# invalid_transition: "cannot transition via %{event}"
|
|
377
321
|
#
|
|
378
|
-
# Notice that the interpolation syntax is %{key} in Rails 3+. In Rails 2.x,
|
|
379
|
-
# the appropriate syntax is {{key}}.
|
|
380
|
-
#
|
|
381
322
|
# You can override these for a specific model like so:
|
|
382
323
|
#
|
|
383
324
|
# en:
|
|
@@ -515,7 +456,7 @@ module StateMachines
|
|
|
515
456
|
|
|
516
457
|
private
|
|
517
458
|
|
|
518
|
-
# Defines a new
|
|
459
|
+
# Defines a new scope with the given name
|
|
519
460
|
def create_scope(name, scope)
|
|
520
461
|
lambda { |model, values| model.where(scope.call(values)) }
|
|
521
462
|
end
|
data/test/files/models/post.rb
CHANGED
data/test/integration_test.rb
CHANGED
data/test/machine_errors_test.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative 'test_helper'
|
|
2
4
|
require 'stringio'
|
|
3
5
|
|
|
@@ -12,14 +14,14 @@ class MachineWithConflictingStateNameTest < BaseTestCase
|
|
|
12
14
|
@machine = StateMachines::Machine.new(@model)
|
|
13
15
|
@machine.state :state
|
|
14
16
|
|
|
15
|
-
assert_match(/^Instance method "state\?" is already defined in
|
|
17
|
+
assert_match(/^Instance method "state\?" is already defined in .+, use generic helper instead.*\n$/, $stderr.string)
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
def test_should_output_warning_with_same_machine_attribute
|
|
19
21
|
@machine = StateMachines::Machine.new(@model, :public_state, :attribute => :state)
|
|
20
22
|
@machine.state :state
|
|
21
23
|
|
|
22
|
-
assert_match(/^Instance method "state\?" is already defined in
|
|
24
|
+
assert_match(/^Instance method "state\?" is already defined in .+, use generic helper instead.*\n$/, $stderr.string)
|
|
23
25
|
end
|
|
24
26
|
|
|
25
27
|
def teardown
|
data/test/model_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'debug' if RUBY_ENGINE == 'ruby'
|
|
5
4
|
require 'minitest/reporters'
|
|
6
5
|
Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
|
|
7
6
|
require 'state_machines-activerecord'
|
metadata
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: state_machines-activerecord
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
8
8
|
- Aaron Pfeifer
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
14
|
name: state_machines-activemodel
|
|
@@ -17,28 +16,28 @@ dependencies:
|
|
|
17
16
|
requirements:
|
|
18
17
|
- - ">="
|
|
19
18
|
- !ruby/object:Gem::Version
|
|
20
|
-
version: 0.
|
|
19
|
+
version: 0.10.0
|
|
21
20
|
type: :runtime
|
|
22
21
|
prerelease: false
|
|
23
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
24
23
|
requirements:
|
|
25
24
|
- - ">="
|
|
26
25
|
- !ruby/object:Gem::Version
|
|
27
|
-
version: 0.
|
|
26
|
+
version: 0.10.0
|
|
28
27
|
- !ruby/object:Gem::Dependency
|
|
29
28
|
name: activerecord
|
|
30
29
|
requirement: !ruby/object:Gem::Requirement
|
|
31
30
|
requirements:
|
|
32
31
|
- - ">="
|
|
33
32
|
- !ruby/object:Gem::Version
|
|
34
|
-
version: '
|
|
33
|
+
version: '7.1'
|
|
35
34
|
type: :runtime
|
|
36
35
|
prerelease: false
|
|
37
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
38
37
|
requirements:
|
|
39
38
|
- - ">="
|
|
40
39
|
- !ruby/object:Gem::Version
|
|
41
|
-
version: '
|
|
40
|
+
version: '7.1'
|
|
42
41
|
- !ruby/object:Gem::Dependency
|
|
43
42
|
name: rake
|
|
44
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -179,7 +178,6 @@ homepage: https://github.com/state-machines/state_machines-activerecord/
|
|
|
179
178
|
licenses:
|
|
180
179
|
- MIT
|
|
181
180
|
metadata: {}
|
|
182
|
-
post_install_message:
|
|
183
181
|
rdoc_options: []
|
|
184
182
|
require_paths:
|
|
185
183
|
- lib
|
|
@@ -187,15 +185,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
187
185
|
requirements:
|
|
188
186
|
- - ">="
|
|
189
187
|
- !ruby/object:Gem::Version
|
|
190
|
-
version: '3.
|
|
188
|
+
version: '3.1'
|
|
191
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
192
190
|
requirements:
|
|
193
191
|
- - ">="
|
|
194
192
|
- !ruby/object:Gem::Version
|
|
195
193
|
version: '0'
|
|
196
194
|
requirements: []
|
|
197
|
-
rubygems_version: 3.
|
|
198
|
-
signing_key:
|
|
195
|
+
rubygems_version: 3.6.7
|
|
199
196
|
specification_version: 4
|
|
200
197
|
summary: State machines Active Record Integration
|
|
201
198
|
test_files:
|