state_machines-activerecord 0.102.0 → 0.103.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: 571fa09486dd051901ae113941b3ae04e9867da62da325c609f97f44f1272f66
|
|
4
|
+
data.tar.gz: 22513e4df2238588d70dcff91f79fb00dfc18dac3a7b36a8a971765355939f8a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a15cf9abb68277ce52ab35e139b15fa8ce6b28fb68424bcbcf840de0216267f7f58b62483c01b2cbd62df9694ba117dfee3b090e2b64692cfe9114396d64c0f2
|
|
7
|
+
data.tar.gz: 97cb794b7e45222503e682641e80da05282f3593453979650431f35fa687831e9349dabe46ea7f2969dc2f95e9b3bc181cfc5860af17e3d57aefb72a8144a485
|
|
@@ -447,8 +447,39 @@ module StateMachines
|
|
|
447
447
|
enum_integration[:state_machine_methods] || []
|
|
448
448
|
end
|
|
449
449
|
|
|
450
|
+
def integer_type_registered?
|
|
451
|
+
!!@integer_type_registered
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
# Machine internals (state matching, validations) call read() to get the
|
|
455
|
+
# current state value and compare it against state.value. Only override
|
|
456
|
+
# when states have explicit integer values (e.g. state :pending, value: 0).
|
|
457
|
+
# In that case the custom type returns a state name string but machine
|
|
458
|
+
# internals need the raw integer for value-based lookup.
|
|
459
|
+
#
|
|
460
|
+
# For auto-indexed states (no explicit value), state.value is the string
|
|
461
|
+
# name so super() returns the right thing already.
|
|
462
|
+
def read(object, attr_sym, ivar = false)
|
|
463
|
+
return super unless integer_type_registered? && attr_sym == :state
|
|
464
|
+
return super unless states_with_explicit_integer_values?
|
|
465
|
+
|
|
466
|
+
raw = object.read_attribute_before_type_cast(attribute.to_s)
|
|
467
|
+
if raw.is_a?(::String) || raw.is_a?(::Symbol)
|
|
468
|
+
matched = states.detect { |s| s.name && s.name.to_s == raw.to_s }
|
|
469
|
+
return matched ? matched.value : raw
|
|
470
|
+
end
|
|
471
|
+
raw
|
|
472
|
+
end
|
|
473
|
+
|
|
450
474
|
private
|
|
451
475
|
|
|
476
|
+
# Returns true when at least one named state has an explicit integer value.
|
|
477
|
+
# Used to decide whether read() needs to return raw integers for machine
|
|
478
|
+
# internals rather than relying on the custom type's string representation.
|
|
479
|
+
def states_with_explicit_integer_values?
|
|
480
|
+
states.any? { |s| s.name && s.value.is_a?(::Integer) }
|
|
481
|
+
end
|
|
482
|
+
|
|
452
483
|
# Returns true when the state machine attribute is backed by an integer column
|
|
453
484
|
def integer_column?
|
|
454
485
|
return false unless owner_class.respond_to?(:type_for_attribute)
|
|
@@ -465,6 +496,7 @@ module StateMachines
|
|
|
465
496
|
# (which fires later, during initial_state=) still compares raw integers.
|
|
466
497
|
def register_integer_type
|
|
467
498
|
@raw_integer_column_default = owner_class.column_defaults[attribute.to_s]
|
|
499
|
+
@integer_type_registered = true
|
|
468
500
|
owner_class.attribute(attribute.to_s, StateMachines::Type::Integer.new(states))
|
|
469
501
|
end
|
|
470
502
|
|