state_machines-activerecord 0.300.0 → 0.302.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
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 56832b6fe9e7b6bdab1c9890fbf6078015b095c85e7b07f38f991012ee471402
|
|
4
|
+
data.tar.gz: 26cd99926e156dba1f805a7077934a0ee6af40b3f699c60ff44d2a533cd3500b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1af35637c8e020ccdf292d27e9ef1ed40f3dd50d3dc17a027bf35a4f8f0034b80b1bb1c34eec7ffb66998678f33dd84787d57dbce06b1529c27c3fa1c93f7679
|
|
7
|
+
data.tar.gz: fe3d5f5bc88807999c5fbf262eb3c56833a74a06096acbe5a8c79094a94bbacffc526fca33b2b07beacea9d19306a72ee52f30670f699bfd56614bd1a069cb56
|
data/README.md
CHANGED
|
@@ -62,7 +62,24 @@ end
|
|
|
62
62
|
|
|
63
63
|
## State changes go through events
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
Opt a machine in with `direct_assignment: :deny`:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
class Vehicle < ActiveRecord::Base
|
|
69
|
+
state_machine initial: :parked, direct_assignment: :deny do
|
|
70
|
+
event(:ignite) { transition parked: :idling }
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Machines without the option use
|
|
76
|
+
`StateMachines::Integrations::ActiveRecord.direct_assignment`, which is
|
|
77
|
+
`:allow` (the attribute can be written directly). Set it to `:deny` in an
|
|
78
|
+
initializer to opt every machine in; a machine can still pass
|
|
79
|
+
`direct_assignment: :allow`. The default is read when a machine is defined,
|
|
80
|
+
so set it before your models load.
|
|
81
|
+
|
|
82
|
+
With `:deny`, the only way to change a record's state through the model is an event:
|
|
66
83
|
|
|
67
84
|
```ruby
|
|
68
85
|
vehicle.ignite # fire it
|
|
@@ -160,10 +177,10 @@ Account.new.state # => "active"
|
|
|
160
177
|
Account.new.suspend # => true
|
|
161
178
|
```
|
|
162
179
|
|
|
163
|
-
- An explicit
|
|
164
|
-
"different default" warning is printed
|
|
165
|
-
|
|
166
|
-
|
|
180
|
+
- An explicit `initial:`, static or dynamic, always wins over the schema
|
|
181
|
+
default (and the "different default" warning is printed). A dynamic one is
|
|
182
|
+
evaluated after the attributes given to `new` are assigned, so it can depend
|
|
183
|
+
on them; a state given to `new` is kept.
|
|
167
184
|
- The default is read when a record is built, not when the class is loaded,
|
|
168
185
|
so defining the machine before the table exists (e.g. while migrating) is
|
|
169
186
|
fine.
|
|
@@ -229,7 +246,7 @@ When enum integration is detected, the gem automatically:
|
|
|
229
246
|
order = Order.create(status: :pending)
|
|
230
247
|
order.pending? # => true (Rails enum method)
|
|
231
248
|
order.processing? # => false (Rails enum method)
|
|
232
|
-
order.processing! # =>
|
|
249
|
+
order.processing! # => writes the state directly; raises StateMachines::DirectStateAssignment with direct_assignment: :deny
|
|
233
250
|
order.process # Fire the event instead
|
|
234
251
|
|
|
235
252
|
Order.pending # Rails enum scope
|
|
@@ -5,7 +5,10 @@ require 'state_machines/integrations/active_record/errors'
|
|
|
5
5
|
module StateMachines
|
|
6
6
|
module Integrations
|
|
7
7
|
module ActiveRecord
|
|
8
|
-
# Enforces that state changes through the model are event-only
|
|
8
|
+
# Enforces that state changes through the model are event-only, for
|
|
9
|
+
# machines declared with <tt>direct_assignment: :deny</tt> (or all
|
|
10
|
+
# machines when ActiveRecord.direct_assignment is +:deny+). Machines
|
|
11
|
+
# allowing direct assignment (the default) are left alone.
|
|
9
12
|
#
|
|
10
13
|
# Every write of a machine's attribute through the model (setter, mass
|
|
11
14
|
# assignment, attribute alias, +record[:attr] =+, +write_attribute+) is
|
|
@@ -380,6 +383,8 @@ module StateMachines
|
|
|
380
383
|
seen = Set.new
|
|
381
384
|
|
|
382
385
|
record.class.state_machines.each_value do |machine|
|
|
386
|
+
next unless machine.direct_assignment_denied?
|
|
387
|
+
|
|
383
388
|
name = guarded_name(machine)
|
|
384
389
|
next unless seen.add?(name) && record._has_attribute?(name) && !machine.transient_attribute?
|
|
385
390
|
|
|
@@ -391,9 +396,11 @@ module StateMachines
|
|
|
391
396
|
end
|
|
392
397
|
|
|
393
398
|
# @return [StateMachines::Machine, nil] the first machine writing
|
|
394
|
-
# attribute +name+
|
|
399
|
+
# attribute +name+ that denies direct assignment
|
|
395
400
|
def machine_for(record, name)
|
|
396
|
-
record.class.state_machines.each_value.find
|
|
401
|
+
record.class.state_machines.each_value.find do |machine|
|
|
402
|
+
machine.direct_assignment_denied? && guarded_name(machine) == name
|
|
403
|
+
end
|
|
397
404
|
end
|
|
398
405
|
|
|
399
406
|
# @return [Array(StateMachines::Machine, Object), nil] the first
|
|
@@ -112,7 +112,21 @@ module StateMachines
|
|
|
112
112
|
#
|
|
113
113
|
# == Direct state assignment
|
|
114
114
|
#
|
|
115
|
-
#
|
|
115
|
+
# By default the machine's attribute can be written directly. Declare
|
|
116
|
+
# <tt>direct_assignment: :deny</tt> to make state changes through the
|
|
117
|
+
# model event-only:
|
|
118
|
+
#
|
|
119
|
+
# class Vehicle < ActiveRecord::Base
|
|
120
|
+
# state_machine initial: :parked, direct_assignment: :deny do
|
|
121
|
+
# event(:ignite) { transition parked: :idling }
|
|
122
|
+
# end
|
|
123
|
+
# end
|
|
124
|
+
#
|
|
125
|
+
# The default for machines that do not set the option is
|
|
126
|
+
# <tt>StateMachines::Integrations::ActiveRecord.direct_assignment</tt>
|
|
127
|
+
# (+:allow+); set it to +:deny+ to opt every machine in.
|
|
128
|
+
#
|
|
129
|
+
# With +:deny+, state changes through the model are event-only. Writing a different
|
|
116
130
|
# state directly (the attribute writer, mass assignment, an alias,
|
|
117
131
|
# <tt>record[:state] =</tt>, +write_attribute+, building a record with a
|
|
118
132
|
# non-initial state, or a callback doing any of these) raises
|
|
@@ -401,6 +415,10 @@ module StateMachines
|
|
|
401
415
|
# The default options to use for state machines using this integration
|
|
402
416
|
@defaults = { action: :save, use_transactions: true }
|
|
403
417
|
@auto_convert_integer_state_attributes = true
|
|
418
|
+
@direct_assignment = :allow
|
|
419
|
+
|
|
420
|
+
# Values accepted by the +direct_assignment+ machine option
|
|
421
|
+
DIRECT_ASSIGNMENT_MODES = %i[allow deny].freeze
|
|
404
422
|
|
|
405
423
|
# Forgets the refusal reasons recorded on a record when it is reloaded:
|
|
406
424
|
# they describe attempts made against the state being discarded.
|
|
@@ -444,11 +462,37 @@ module StateMachines
|
|
|
444
462
|
def owner_class=(klass)
|
|
445
463
|
super.tap do
|
|
446
464
|
klass.include(RefusalReset) unless klass < RefusalReset
|
|
447
|
-
StateGuard.install(klass, attribute)
|
|
465
|
+
StateGuard.install(klass, attribute) if direct_assignment_denied?
|
|
448
466
|
register_integer_type if integer_type_registered?
|
|
449
467
|
end
|
|
450
468
|
end
|
|
451
469
|
|
|
470
|
+
# How direct writes of the machine's attribute are treated: +:allow+
|
|
471
|
+
# (they go through untouched) or +:deny+ (state changes are
|
|
472
|
+
# event-only, see {StateGuard}). Set per machine with the
|
|
473
|
+
# +direct_assignment+ option, defaulting to
|
|
474
|
+
# {ActiveRecord.direct_assignment}.
|
|
475
|
+
#
|
|
476
|
+
# @return [Symbol] +:allow+ or +:deny+
|
|
477
|
+
# @raise [ArgumentError] when the configured value is not a known mode
|
|
478
|
+
def direct_assignment
|
|
479
|
+
@direct_assignment ||= begin
|
|
480
|
+
mode = integration_options.fetch(:direct_assignment) { ActiveRecord.direct_assignment }
|
|
481
|
+
unless DIRECT_ASSIGNMENT_MODES.include?(mode)
|
|
482
|
+
raise ArgumentError,
|
|
483
|
+
"Invalid direct_assignment: #{mode.inspect} (expected one of #{DIRECT_ASSIGNMENT_MODES.inspect})"
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
mode
|
|
487
|
+
end
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
# @return [Boolean] whether state changes through the model are
|
|
491
|
+
# event-only
|
|
492
|
+
def direct_assignment_denied?
|
|
493
|
+
direct_assignment == :deny
|
|
494
|
+
end
|
|
495
|
+
|
|
452
496
|
# Check if enum integration should be enabled for this machine
|
|
453
497
|
def detect_enum_integration
|
|
454
498
|
return nil unless owner_class.defined_enums.key?(attribute.to_s)
|
|
@@ -948,6 +992,17 @@ module StateMachines
|
|
|
948
992
|
class << self
|
|
949
993
|
attr_accessor :auto_convert_integer_state_attributes
|
|
950
994
|
|
|
995
|
+
# Default +direct_assignment+ mode for machines that do not set one:
|
|
996
|
+
# +:allow+ (default) or +:deny+. Read when a machine is defined.
|
|
997
|
+
#
|
|
998
|
+
# @return [Symbol]
|
|
999
|
+
attr_accessor :direct_assignment
|
|
1000
|
+
|
|
1001
|
+
# @return [Array<Symbol>] machine options added by this integration
|
|
1002
|
+
def integration_options
|
|
1003
|
+
[:direct_assignment]
|
|
1004
|
+
end
|
|
1005
|
+
|
|
951
1006
|
# Classes that inherit from ActiveRecord::Base will automatically use
|
|
952
1007
|
# the ActiveRecord integration.
|
|
953
1008
|
def matching_ancestors
|
|
@@ -973,6 +1028,30 @@ module StateMachines
|
|
|
973
1028
|
static, dynamic = machines.partition { |machine| !machine.dynamic_initial_state? }
|
|
974
1029
|
(static + dynamic).each { |machine| machine.initialize_state(record) }
|
|
975
1030
|
end
|
|
1031
|
+
|
|
1032
|
+
# Initializes the states of a new record. A dynamic initial state is
|
|
1033
|
+
# applied even over a column default, like a static one: the column
|
|
1034
|
+
# default is already in the attribute, so core's dynamic pass (which
|
|
1035
|
+
# only fills a blank attribute) would keep it. A state the caller
|
|
1036
|
+
# passed is left in place for {StateGuard} to check. Dynamic states
|
|
1037
|
+
# are evaluated after static ones, which they may depend on; the
|
|
1038
|
+
# caller's +new+ block runs afterwards, so it cannot be overwritten.
|
|
1039
|
+
#
|
|
1040
|
+
# @param record [ActiveRecord::Base] the record being built
|
|
1041
|
+
# @param attributes [Hash] the attributes given to +new+, aliases
|
|
1042
|
+
# resolved, plus the scope's attributes
|
|
1043
|
+
# @return [void]
|
|
1044
|
+
def initialize_new_record_states(record, attributes)
|
|
1045
|
+
machines = record.class.state_machines
|
|
1046
|
+
machines.initialize_states(record, { dynamic: false }, attributes)
|
|
1047
|
+
|
|
1048
|
+
given = attributes.keys.map(&:to_s)
|
|
1049
|
+
machines.each_value do |machine|
|
|
1050
|
+
next unless machine.dynamic_initial_state?
|
|
1051
|
+
|
|
1052
|
+
machine.initialize_state(record, force: !given.include?(StateGuard.guarded_name(machine)))
|
|
1053
|
+
end
|
|
1054
|
+
end
|
|
976
1055
|
end
|
|
977
1056
|
|
|
978
1057
|
protected
|
|
@@ -1022,7 +1101,7 @@ module StateMachines
|
|
|
1022
1101
|
attributes = (attributes || {}).transform_keys { |key| self.class.attribute_aliases[key.to_s] || key }
|
|
1023
1102
|
scoped_attributes = attributes.merge(self.class.scope_attributes)
|
|
1024
1103
|
|
|
1025
|
-
|
|
1104
|
+
StateMachines::Integrations::ActiveRecord.initialize_new_record_states(self, scoped_attributes)
|
|
1026
1105
|
StateMachines::Integrations::ActiveRecord::StateGuard.finish_construction(self)
|
|
1027
1106
|
yield(*args) if block_given?
|
|
1028
1107
|
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.
|
|
4
|
+
version: 0.302.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
@@ -24,6 +24,20 @@ dependencies:
|
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '8.1'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: state_machines
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 0.301.0
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 0.301.0
|
|
27
41
|
- !ruby/object:Gem::Dependency
|
|
28
42
|
name: state_machines-activemodel
|
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|