state_machines-activerecord 0.103.0 → 0.300.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/README.md +242 -3
- data/lib/state_machines/integrations/active_record/errors.rb +83 -0
- data/lib/state_machines/integrations/active_record/state_guard.rb +449 -0
- data/lib/state_machines/integrations/active_record/type/integer.rb +76 -15
- data/lib/state_machines/integrations/active_record/version.rb +1 -1
- data/lib/state_machines/integrations/active_record.rb +371 -27
- metadata +13 -11
|
@@ -4,6 +4,7 @@ require 'state_machines-activemodel'
|
|
|
4
4
|
require 'active_record'
|
|
5
5
|
require 'state_machines/integrations/active_record/version'
|
|
6
6
|
require 'state_machines/integrations/active_record/type/integer'
|
|
7
|
+
require 'state_machines/integrations/active_record/state_guard'
|
|
7
8
|
|
|
8
9
|
module StateMachines
|
|
9
10
|
module Integrations # :nodoc:
|
|
@@ -109,6 +110,26 @@ module StateMachines
|
|
|
109
110
|
# # Control access via strong parameters in your controller
|
|
110
111
|
# end
|
|
111
112
|
#
|
|
113
|
+
# == Direct state assignment
|
|
114
|
+
#
|
|
115
|
+
# State changes through the model are event-only. Writing a different
|
|
116
|
+
# state directly (the attribute writer, mass assignment, an alias,
|
|
117
|
+
# <tt>record[:state] =</tt>, +write_attribute+, building a record with a
|
|
118
|
+
# non-initial state, or a callback doing any of these) raises
|
|
119
|
+
# StateMachines::DirectStateAssignment before the value changes, and
|
|
120
|
+
# +save+ refuses to persist a state changed behind the writer's back.
|
|
121
|
+
# Assigning the state the record already holds is allowed and runs
|
|
122
|
+
# nothing.
|
|
123
|
+
#
|
|
124
|
+
# vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
|
|
125
|
+
# vehicle.state = 'idling' # => StateMachines::DirectStateAssignment
|
|
126
|
+
# vehicle.ignite # => true
|
|
127
|
+
#
|
|
128
|
+
# Raw persistence (+update_column+, +update_columns+, +update_all+,
|
|
129
|
+
# +insert_all+ / +upsert_all+, SQL) stays outside the machine for data
|
|
130
|
+
# repair: no event is inferred and no callback runs. Reload live
|
|
131
|
+
# instances afterwards. See StateGuard for the details.
|
|
132
|
+
#
|
|
112
133
|
# == Transactions
|
|
113
134
|
#
|
|
114
135
|
# In order to ensure that any changes made during transition callbacks
|
|
@@ -183,7 +204,8 @@ module StateMachines
|
|
|
183
204
|
#
|
|
184
205
|
# For example,
|
|
185
206
|
#
|
|
186
|
-
# vehicle = Vehicle.create
|
|
207
|
+
# vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
|
|
208
|
+
# vehicle.ignite # => true
|
|
187
209
|
# vehicle.ignite # => false
|
|
188
210
|
# vehicle.errors.full_messages # => ["State cannot transition via \"ignite\""]
|
|
189
211
|
#
|
|
@@ -316,6 +338,12 @@ module StateMachines
|
|
|
316
338
|
# * (8) *after_transition*
|
|
317
339
|
# * (-) end transaction (if enabled)
|
|
318
340
|
# * (9) after_commit
|
|
341
|
+
# * (10) *after_transition* callbacks defined with <tt>:after_commit => true</tt>
|
|
342
|
+
#
|
|
343
|
+
# An +after_transition+ callback defined with the <tt>:after_commit</tt>
|
|
344
|
+
# option is deferred until the surrounding database transaction has been
|
|
345
|
+
# committed (and discarded if it rolls back). See the documentation for
|
|
346
|
+
# +after_transition+ in this integration for more details.
|
|
319
347
|
#
|
|
320
348
|
# == Internationalization
|
|
321
349
|
#
|
|
@@ -372,6 +400,19 @@ module StateMachines
|
|
|
372
400
|
|
|
373
401
|
# The default options to use for state machines using this integration
|
|
374
402
|
@defaults = { action: :save, use_transactions: true }
|
|
403
|
+
@auto_convert_integer_state_attributes = true
|
|
404
|
+
|
|
405
|
+
# Forgets the refusal reasons recorded on a record when it is reloaded:
|
|
406
|
+
# they describe attempts made against the state being discarded.
|
|
407
|
+
module RefusalReset
|
|
408
|
+
# Reloads the record and forgets its refusal reasons.
|
|
409
|
+
#
|
|
410
|
+
# @return [ActiveRecord::Base] the reloaded record
|
|
411
|
+
# @see StateMachines::Event#refusal
|
|
412
|
+
def reload(*)
|
|
413
|
+
super.tap { StateMachines::Event.clear_refusals(self) }
|
|
414
|
+
end
|
|
415
|
+
end
|
|
375
416
|
|
|
376
417
|
# Machine-specific methods for enum integration
|
|
377
418
|
module MachineMethods
|
|
@@ -382,7 +423,30 @@ module StateMachines
|
|
|
382
423
|
def after_initialize
|
|
383
424
|
super
|
|
384
425
|
initialize_enum_integration
|
|
385
|
-
|
|
426
|
+
|
|
427
|
+
register_integer_type if register_integer_type?
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Hook called when a machine is assigned to a class, including when an
|
|
431
|
+
# inherited machine is cloned for an STI subclass. The cloned machine
|
|
432
|
+
# carries the parent's @integer_type_registered flag while the subclass
|
|
433
|
+
# still inherits the parent's attribute type, which references the
|
|
434
|
+
# parent machine's state collection. Re-register so the subclass type
|
|
435
|
+
# sees this machine's (cloned) states, picking up subclass-added states
|
|
436
|
+
# as they are defined.
|
|
437
|
+
#
|
|
438
|
+
# Also makes reload forget the record's refusal reasons (see
|
|
439
|
+
# {RefusalReset}) and installs the guard making state changes
|
|
440
|
+
# event-only (see {StateGuard}).
|
|
441
|
+
#
|
|
442
|
+
# @param klass [Class] the new owner class
|
|
443
|
+
# @return [Class] the assigned owner class
|
|
444
|
+
def owner_class=(klass)
|
|
445
|
+
super.tap do
|
|
446
|
+
klass.include(RefusalReset) unless klass < RefusalReset
|
|
447
|
+
StateGuard.install(klass, attribute)
|
|
448
|
+
register_integer_type if integer_type_registered?
|
|
449
|
+
end
|
|
386
450
|
end
|
|
387
451
|
|
|
388
452
|
# Check if enum integration should be enabled for this machine
|
|
@@ -418,9 +482,92 @@ module StateMachines
|
|
|
418
482
|
# Generate methods after each state addition if enum integration is enabled
|
|
419
483
|
generate_state_machine_methods if enum_integrated?
|
|
420
484
|
|
|
485
|
+
# States defined after initialization (e.g. adding an explicit value
|
|
486
|
+
# to the initial state) can change how the column default maps to
|
|
487
|
+
# states, so re-evaluate the conflicting-default warning
|
|
488
|
+
recheck_conflicting_attribute_default if integer_type_registered?
|
|
489
|
+
|
|
421
490
|
result
|
|
422
491
|
end
|
|
423
492
|
|
|
493
|
+
# Sets the initial state, remembering whether one was configured
|
|
494
|
+
# explicitly. Without an explicit initial state the attribute's schema
|
|
495
|
+
# default may supply it (see {#initial_state}).
|
|
496
|
+
#
|
|
497
|
+
# @param new_initial_state [Symbol, Proc, nil] the initial state name,
|
|
498
|
+
# a lambda resolving it per record, or nil for none
|
|
499
|
+
# @return [Symbol, Proc, nil] the assigned initial state
|
|
500
|
+
def initial_state=(new_initial_state)
|
|
501
|
+
@explicit_initial_state = !new_initial_state.nil?
|
|
502
|
+
super
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
# Gets the initial state for the given record.
|
|
506
|
+
#
|
|
507
|
+
# An explicit +initial:+ (static or dynamic) always wins. Without one,
|
|
508
|
+
# the attribute's schema default (the database column default, or a
|
|
509
|
+
# default declared through the Attributes API) is used when it names a
|
|
510
|
+
# defined state. The default is read lazily, when a record is built, so
|
|
511
|
+
# defining the machine never requires the table to exist. A default
|
|
512
|
+
# that names no defined state is ignored and the initial state stays
|
|
513
|
+
# nil (the class-load conflicting-default warning reports it).
|
|
514
|
+
#
|
|
515
|
+
# @param object [ActiveRecord::Base] the record being initialized
|
|
516
|
+
# @return [StateMachines::State, nil] the initial state
|
|
517
|
+
def initial_state(object)
|
|
518
|
+
return super if explicit_initial_state?
|
|
519
|
+
|
|
520
|
+
schema_default_state || super
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
# Whether the machine's attribute is not backed by a database column
|
|
524
|
+
# (aliases resolve to their target). Reads the class's memoized
|
|
525
|
+
# columns, so it is cheap enough to call for every loaded record.
|
|
526
|
+
# Returns false while the schema cannot be read.
|
|
527
|
+
#
|
|
528
|
+
# @return [Boolean]
|
|
529
|
+
def transient_attribute?
|
|
530
|
+
name = attribute.to_s
|
|
531
|
+
!owner_class.columns_hash.key?(owner_class.attribute_aliases.fetch(name, name))
|
|
532
|
+
rescue ::ActiveRecord::StatementInvalid, ::ActiveRecord::ConnectionNotEstablished
|
|
533
|
+
false
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
# Whether the machine was given an explicit (non-nil) initial state.
|
|
537
|
+
#
|
|
538
|
+
# @return [Boolean]
|
|
539
|
+
def explicit_initial_state?
|
|
540
|
+
!!@explicit_initial_state
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
# Warns at most once when the column default conflicts with the
|
|
544
|
+
# machine's initial state. The conflict is re-evaluated as states are
|
|
545
|
+
# defined, so remember when the warning has been issued. No warning is
|
|
546
|
+
# issued when the machine has no explicit initial state and the column
|
|
547
|
+
# default names a defined state: the default then *is* the initial
|
|
548
|
+
# state.
|
|
549
|
+
def check_conflicting_attribute_default
|
|
550
|
+
return if @attribute_default_conflict_warned || schema_default_initial_state?
|
|
551
|
+
|
|
552
|
+
initial_state = states.detect(&:initial)
|
|
553
|
+
conflict = !owner_class_attribute_default.nil? && (
|
|
554
|
+
dynamic_initial_state? || !owner_class_attribute_default_matches?(initial_state)
|
|
555
|
+
)
|
|
556
|
+
return unless conflict
|
|
557
|
+
|
|
558
|
+
@attribute_default_conflict_warned = true
|
|
559
|
+
super
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
# Returns true when this machine should use the custom integer attribute type
|
|
563
|
+
# to convert between Ruby state names and integer database values. This only
|
|
564
|
+
# applies to non-enum integer columns when automatic conversion is enabled.
|
|
565
|
+
def register_integer_type?
|
|
566
|
+
StateMachines::Integrations::ActiveRecord.auto_convert_integer_state_attributes &&
|
|
567
|
+
integer_column? &&
|
|
568
|
+
!enum_integrated?
|
|
569
|
+
end
|
|
570
|
+
|
|
424
571
|
# Check if this machine has enum integration enabled
|
|
425
572
|
def enum_integrated?
|
|
426
573
|
enum_integration && enum_integration[:enabled]
|
|
@@ -451,33 +598,175 @@ module StateMachines
|
|
|
451
598
|
!!@integer_type_registered
|
|
452
599
|
end
|
|
453
600
|
|
|
601
|
+
# Creates a callback that will be invoked *after* a transition is
|
|
602
|
+
# performed, so long as the given requirements match the transition.
|
|
603
|
+
#
|
|
604
|
+
# In addition to the configuration options supported by the core
|
|
605
|
+
# +after_transition+ (see StateMachines::Machine#after_transition), the
|
|
606
|
+
# ActiveRecord integration supports:
|
|
607
|
+
# * <tt>:after_commit</tt> - Defer execution of the callback until the
|
|
608
|
+
# database transaction wrapping the transition has been committed.
|
|
609
|
+
# When no transaction is open at that point, the callback runs
|
|
610
|
+
# immediately. When the transaction (or an outer transaction wrapping
|
|
611
|
+
# it) is rolled back, the callback is discarded.
|
|
612
|
+
#
|
|
613
|
+
# This is the safe place to enqueue background jobs that reference the
|
|
614
|
+
# record (e.g. via GlobalID), since a regular +after_transition+ runs
|
|
615
|
+
# inside the transaction, before the record's changes are visible to
|
|
616
|
+
# other connections:
|
|
617
|
+
#
|
|
618
|
+
# class Vehicle < ApplicationRecord
|
|
619
|
+
# state_machine do
|
|
620
|
+
# after_transition on: :ignite, after_commit: true do |vehicle|
|
|
621
|
+
# EngineWarmupJob.perform_later(vehicle)
|
|
622
|
+
# end
|
|
623
|
+
#
|
|
624
|
+
# ...
|
|
625
|
+
# end
|
|
626
|
+
# end
|
|
627
|
+
#
|
|
628
|
+
# Note that a deferred callback cannot halt the callback chain or
|
|
629
|
+
# affect the result of the transition: by the time it runs, the
|
|
630
|
+
# transition has already been committed. For the same reason, an
|
|
631
|
+
# exception raised by a deferred callback is not propagated (doing so
|
|
632
|
+
# would revert the record's in-memory state even though the database
|
|
633
|
+
# was already updated); it is reported to +ActiveSupport.error_reporter+
|
|
634
|
+
# (+Rails.error+) instead. Conditions (<tt>:if</tt>/<tt>:unless</tt>)
|
|
635
|
+
# and state requirements are evaluated when the transition is
|
|
636
|
+
# performed, not at commit time.
|
|
637
|
+
#
|
|
638
|
+
# Like ActiveRecord's own +after_commit+, a surrounding
|
|
639
|
+
# <tt>transaction(joinable: false)</tt> wrapper is transparent: the
|
|
640
|
+
# callback fires at the inner commit. This is what makes it fire
|
|
641
|
+
# under transactional test fixtures.
|
|
642
|
+
def after_transition(*args, **options, &block)
|
|
643
|
+
# The flag may hide in a legacy trailing positional options hash
|
|
644
|
+
positional_options = args.last.is_a?(Hash) ? args.pop.dup : {}
|
|
645
|
+
options = positional_options.merge(options)
|
|
646
|
+
|
|
647
|
+
# Only a boolean is the flag — a non-boolean value is the implicit
|
|
648
|
+
# state-requirement form for a state named :after_commit
|
|
649
|
+
flag = options[:after_commit]
|
|
650
|
+
return super unless flag == true || flag == false
|
|
651
|
+
|
|
652
|
+
options.delete(:after_commit)
|
|
653
|
+
return super unless flag
|
|
654
|
+
|
|
655
|
+
# Method handling goes to a real Callback (reusing core's binding,
|
|
656
|
+
# arity and :do semantics); branch matching stays on the wrapper so
|
|
657
|
+
# conditions are evaluated at transition time
|
|
658
|
+
parsed = parse_callback_arguments(args, options)
|
|
659
|
+
method_options = parsed.slice(:do, :bind_to_object)
|
|
660
|
+
method_options[:terminator] = callback_terminator
|
|
661
|
+
branch_options = parsed.except(:do, :bind_to_object, :terminator)
|
|
662
|
+
|
|
663
|
+
deferred = Callback.new(:after, method_options, &block)
|
|
664
|
+
|
|
665
|
+
super(**branch_options, bind_to_object: false) do |object, transition|
|
|
666
|
+
object.class.current_transaction.after_commit do
|
|
667
|
+
# The transition's catch(:halt) is gone at commit time
|
|
668
|
+
catch(:halt) { deferred.call(object, {}, transition) }
|
|
669
|
+
rescue StandardError => e
|
|
670
|
+
# Raising would roll back in-memory state already committed; report instead
|
|
671
|
+
ActiveSupport.error_reporter.report(e, handled: false, source: 'state_machines-activerecord')
|
|
672
|
+
end
|
|
673
|
+
end
|
|
674
|
+
end
|
|
675
|
+
|
|
676
|
+
# Writes a value to the record. A state write is the machine's own
|
|
677
|
+
# (initialization, a transition persisting its destination, a
|
|
678
|
+
# rollback), so it is authorized for the duration of this single
|
|
679
|
+
# write: callbacks running around it are not (see {StateGuard}).
|
|
680
|
+
#
|
|
681
|
+
# @param object [ActiveRecord::Base] record being written
|
|
682
|
+
# @param attr_sym [Symbol] attribute kind (:state, :event, ...)
|
|
683
|
+
# @param value [Object] the value to write
|
|
684
|
+
# @param ivar [Boolean] whether to write an instance variable
|
|
685
|
+
# @return [Object] the written value
|
|
686
|
+
def write(object, attr_sym, value, ivar = false)
|
|
687
|
+
return super unless attr_sym == :state && !ivar
|
|
688
|
+
|
|
689
|
+
StateGuard.authorize(object, StateGuard.guarded_name(self)) { super }
|
|
690
|
+
end
|
|
691
|
+
|
|
692
|
+
# Runs state events around validation, first refusing to start a
|
|
693
|
+
# transition from a state changed without an event.
|
|
694
|
+
#
|
|
695
|
+
# @param object [ActiveRecord::Base] record being validated
|
|
696
|
+
def around_validation(object, &)
|
|
697
|
+
StateGuard.verify_before_transitions!(object)
|
|
698
|
+
super
|
|
699
|
+
end
|
|
700
|
+
|
|
454
701
|
# Machine internals (state matching, validations) call read() to get the
|
|
455
|
-
# current state value and compare it against state.value.
|
|
456
|
-
#
|
|
457
|
-
#
|
|
458
|
-
#
|
|
702
|
+
# current state value and compare it against state.value. The custom
|
|
703
|
+
# integer type already returns the canonical value when state values are
|
|
704
|
+
# uniform: raw integers when every named state has an explicit integer
|
|
705
|
+
# value (passthrough), name strings when none do (state.value is the
|
|
706
|
+
# name). Only machines mixing explicit and auto-indexed values need an
|
|
707
|
+
# override, because the type returns name strings while the explicit
|
|
708
|
+
# states match on integers; map the stored value back to the matched
|
|
709
|
+
# state's canonical state.value.
|
|
459
710
|
#
|
|
460
|
-
#
|
|
461
|
-
#
|
|
711
|
+
# @param object [ActiveRecord::Base] record being read
|
|
712
|
+
# @param attr_sym [Symbol] attribute kind (:state, :event, ...)
|
|
713
|
+
# @param ivar [Boolean] whether to read from an instance variable
|
|
714
|
+
# @return [Object] a value machine internals can match on state.value
|
|
462
715
|
def read(object, attr_sym, ivar = false)
|
|
463
716
|
return super unless integer_type_registered? && attr_sym == :state
|
|
464
|
-
return super unless
|
|
717
|
+
return super unless mixed_integer_state_values?
|
|
465
718
|
|
|
466
719
|
raw = object.read_attribute_before_type_cast(attribute.to_s)
|
|
467
720
|
if raw.is_a?(::String) || raw.is_a?(::Symbol)
|
|
468
721
|
matched = states.detect { |s| s.name && s.name.to_s == raw.to_s }
|
|
469
|
-
return matched
|
|
722
|
+
return matched.value if matched
|
|
470
723
|
end
|
|
471
|
-
|
|
724
|
+
|
|
725
|
+
name = owner_class.type_for_attribute(attribute.to_s).deserialize(raw)
|
|
726
|
+
matched = states.detect { |s| s.name && s.name.to_s == name.to_s }
|
|
727
|
+
matched ? matched.value : raw
|
|
472
728
|
end
|
|
473
729
|
|
|
474
730
|
private
|
|
475
731
|
|
|
476
|
-
#
|
|
477
|
-
#
|
|
478
|
-
#
|
|
479
|
-
|
|
480
|
-
|
|
732
|
+
# Whether the schema default supplies the initial state: there is no
|
|
733
|
+
# explicit initial state and the default names a defined state.
|
|
734
|
+
#
|
|
735
|
+
# @return [Boolean]
|
|
736
|
+
def schema_default_initial_state?
|
|
737
|
+
!explicit_initial_state? && !schema_default_state.nil?
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
# Finds the named state matching the attribute's schema default.
|
|
741
|
+
#
|
|
742
|
+
# @return [StateMachines::State, nil] the matching state, or nil when
|
|
743
|
+
# there is no default (or no readable schema) or it names no state
|
|
744
|
+
def schema_default_state
|
|
745
|
+
return if owner_class_attribute_default.nil?
|
|
746
|
+
|
|
747
|
+
states.detect { |state| state.name && owner_class_attribute_default_matches?(state) }
|
|
748
|
+
end
|
|
749
|
+
|
|
750
|
+
# Re-runs the conflicting-default check for states defined after
|
|
751
|
+
# initialization. Skipped until an initial state exists (the DSL block
|
|
752
|
+
# evaluates before initial_state= during Machine.new).
|
|
753
|
+
#
|
|
754
|
+
# @return [void]
|
|
755
|
+
def recheck_conflicting_attribute_default
|
|
756
|
+
return unless states.detect(&:initial)
|
|
757
|
+
|
|
758
|
+
check_conflicting_attribute_default
|
|
759
|
+
end
|
|
760
|
+
|
|
761
|
+
# Returns true when named states mix explicit integer values with
|
|
762
|
+
# auto-indexed (name-valued) states. Uses value(false) so dynamic
|
|
763
|
+
# (Proc) state values are never evaluated for this metadata decision.
|
|
764
|
+
#
|
|
765
|
+
# @return [Boolean]
|
|
766
|
+
def mixed_integer_state_values?
|
|
767
|
+
named = states.select(&:name)
|
|
768
|
+
explicit = named.count { |s| s.value(false).is_a?(::Integer) }
|
|
769
|
+
explicit.positive? && explicit < named.size
|
|
481
770
|
end
|
|
482
771
|
|
|
483
772
|
# Returns true when the state machine attribute is backed by an integer column
|
|
@@ -497,7 +786,14 @@ module StateMachines
|
|
|
497
786
|
def register_integer_type
|
|
498
787
|
@raw_integer_column_default = owner_class.column_defaults[attribute.to_s]
|
|
499
788
|
@integer_type_registered = true
|
|
500
|
-
|
|
789
|
+
|
|
790
|
+
# When re-registering (e.g. for an STI subclass that inherited the
|
|
791
|
+
# parent's custom type), unwrap it to keep the column's original type
|
|
792
|
+
# as the passthrough delegate.
|
|
793
|
+
current_type = owner_class.type_for_attribute(attribute.to_s)
|
|
794
|
+
raw_type = current_type.is_a?(StateMachines::Type::Integer) ? current_type.raw_type : current_type
|
|
795
|
+
|
|
796
|
+
owner_class.attribute(attribute.to_s, StateMachines::Type::Integer.new(states, raw_type: raw_type))
|
|
501
797
|
end
|
|
502
798
|
|
|
503
799
|
# Detect existing enum methods for this attribute
|
|
@@ -650,11 +946,33 @@ module StateMachines
|
|
|
650
946
|
include MachineMethods
|
|
651
947
|
|
|
652
948
|
class << self
|
|
949
|
+
attr_accessor :auto_convert_integer_state_attributes
|
|
950
|
+
|
|
653
951
|
# Classes that inherit from ActiveRecord::Base will automatically use
|
|
654
952
|
# the ActiveRecord integration.
|
|
655
953
|
def matching_ancestors
|
|
656
954
|
[::ActiveRecord::Base]
|
|
657
955
|
end
|
|
956
|
+
|
|
957
|
+
# Initializes the states of a record loaded from the database for the
|
|
958
|
+
# machines whose attribute is not a database column (an
|
|
959
|
+
# +attr_accessor+ or a column-less Attributes API attribute). Loading
|
|
960
|
+
# skips +initialize+, so without this such records would start with a
|
|
961
|
+
# blank state. Column-backed machines are left alone: the stored value,
|
|
962
|
+
# even NULL, is the record's state. Only blank attributes are filled,
|
|
963
|
+
# static initial states before dynamic ones (which may depend on them),
|
|
964
|
+
# and before +after_find+/+after_initialize+ callbacks run, so those
|
|
965
|
+
# callbacks see the initialized state (and may fire events from it).
|
|
966
|
+
# The writes are the machine's own, so they are authorized (see
|
|
967
|
+
# {StateGuard}) and run no callbacks.
|
|
968
|
+
#
|
|
969
|
+
# @param record [ActiveRecord::Base] the record being loaded
|
|
970
|
+
# @return [void]
|
|
971
|
+
def initialize_transient_states(record)
|
|
972
|
+
machines = record.class.state_machines.values.select(&:transient_attribute?)
|
|
973
|
+
static, dynamic = machines.partition { |machine| !machine.dynamic_initial_state? }
|
|
974
|
+
(static + dynamic).each { |machine| machine.initialize_state(record) }
|
|
975
|
+
end
|
|
658
976
|
end
|
|
659
977
|
|
|
660
978
|
protected
|
|
@@ -664,27 +982,58 @@ module StateMachines
|
|
|
664
982
|
action == :save
|
|
665
983
|
end
|
|
666
984
|
|
|
667
|
-
# Gets the db default for the machine's attribute
|
|
668
|
-
# For integer columns the raw
|
|
669
|
-
#
|
|
985
|
+
# Gets the db default for the machine's attribute, resolving attribute
|
|
986
|
+
# aliases to the underlying column. For integer columns the raw
|
|
987
|
+
# pre-type-registration default is returned so that
|
|
988
|
+
# check_conflicting_attribute_default can compare integers to integers.
|
|
989
|
+
# Returns nil while the schema cannot be read (no connection, no table).
|
|
990
|
+
#
|
|
991
|
+
# @return [Object, nil] the default value
|
|
670
992
|
def owner_class_attribute_default
|
|
671
993
|
return @raw_integer_column_default if defined?(@raw_integer_column_default)
|
|
672
994
|
return unless owner_class.connected? && owner_class.table_exists?
|
|
673
995
|
|
|
674
|
-
owner_class.column_defaults[attribute.to_s]
|
|
996
|
+
owner_class.column_defaults[owner_class.attribute_aliases.fetch(attribute.to_s, attribute.to_s)]
|
|
997
|
+
rescue ::ActiveRecord::StatementInvalid, ::ActiveRecord::ConnectionNotEstablished
|
|
998
|
+
nil
|
|
999
|
+
end
|
|
1000
|
+
|
|
1001
|
+
# Checks whether the given state matches the column default. When the
|
|
1002
|
+
# custom integer type is registered, auto-indexed states match on their
|
|
1003
|
+
# name string while the column default is a raw integer, so the default
|
|
1004
|
+
# is also compared through the type's mapping (e.g. 0 matches the first
|
|
1005
|
+
# auto-indexed state).
|
|
1006
|
+
#
|
|
1007
|
+
# @param state [StateMachines::State] the state to compare (the initial state)
|
|
1008
|
+
# @return [Boolean] whether the column default represents this state
|
|
1009
|
+
def owner_class_attribute_default_matches?(state)
|
|
1010
|
+
matches = super
|
|
1011
|
+
return matches if matches || !integer_type_registered?
|
|
1012
|
+
|
|
1013
|
+
default = owner_class_attribute_default
|
|
1014
|
+
state.matches?(owner_class.type_for_attribute(attribute.to_s).deserialize(default))
|
|
675
1015
|
end
|
|
676
1016
|
|
|
677
1017
|
def define_state_initializer
|
|
678
1018
|
define_helper :instance, <<-END_EVAL, __FILE__, __LINE__ + 1
|
|
679
1019
|
def initialize(attributes = nil, *)
|
|
1020
|
+
StateMachines::Integrations::ActiveRecord::StateGuard.begin_construction(self)
|
|
680
1021
|
super(attributes) do |*args|
|
|
681
1022
|
attributes = (attributes || {}).transform_keys { |key| self.class.attribute_aliases[key.to_s] || key }
|
|
682
1023
|
scoped_attributes = attributes.merge(self.class.scope_attributes)
|
|
683
1024
|
|
|
684
1025
|
self.class.state_machines.initialize_states(self, {}, scoped_attributes)
|
|
1026
|
+
StateMachines::Integrations::ActiveRecord::StateGuard.finish_construction(self)
|
|
685
1027
|
yield(*args) if block_given?
|
|
686
1028
|
end
|
|
687
1029
|
end
|
|
1030
|
+
|
|
1031
|
+
def init_with_attributes(attributes, new_record = false)
|
|
1032
|
+
super do |record|
|
|
1033
|
+
StateMachines::Integrations::ActiveRecord.initialize_transient_states(record)
|
|
1034
|
+
yield record if block_given?
|
|
1035
|
+
end
|
|
1036
|
+
end
|
|
688
1037
|
END_EVAL
|
|
689
1038
|
end
|
|
690
1039
|
|
|
@@ -712,6 +1061,7 @@ module StateMachines
|
|
|
712
1061
|
|
|
713
1062
|
# Runs state events around the machine's :save action
|
|
714
1063
|
def around_save(object, &)
|
|
1064
|
+
StateGuard.verify_before_transitions!(object)
|
|
715
1065
|
# Pass fiber: false to avoid deadlocks with ActiveRecord's LoadInterlockAwareMonitor
|
|
716
1066
|
object.class.state_machines.transitions(object, action, fiber: false).perform(&)
|
|
717
1067
|
end
|
|
@@ -759,12 +1109,6 @@ module StateMachines
|
|
|
759
1109
|
|
|
760
1110
|
private
|
|
761
1111
|
|
|
762
|
-
# Generates the results for the given scope based on one or more states to filter by
|
|
763
|
-
def run_scope(scope, machine, klass, states)
|
|
764
|
-
values = states.flatten.compact.map { |state| machine.states.fetch(state).value }
|
|
765
|
-
scope.call(klass, values)
|
|
766
|
-
end
|
|
767
|
-
|
|
768
1112
|
# ActiveModel's use of method_missing / respond_to for attribute methods
|
|
769
1113
|
# breaks both ancestor lookups and defined?(super). Need to special-case
|
|
770
1114
|
# the existence of query attribute methods.
|
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.300.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
@@ -16,28 +16,28 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
19
|
+
version: '8.1'
|
|
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
|
-
version: '
|
|
26
|
+
version: '8.1'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: state_machines-activemodel
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
31
|
- - ">="
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 0.
|
|
33
|
+
version: 0.300.0
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
38
|
- - ">="
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: 0.
|
|
40
|
+
version: 0.300.0
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: appraisal
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -56,16 +56,16 @@ dependencies:
|
|
|
56
56
|
name: minitest
|
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
|
58
58
|
requirements:
|
|
59
|
-
- -
|
|
59
|
+
- - "~>"
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
|
-
version:
|
|
61
|
+
version: '6.0'
|
|
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
|
-
version:
|
|
68
|
+
version: '6.0'
|
|
69
69
|
- !ruby/object:Gem::Dependency
|
|
70
70
|
name: minitest-reporters
|
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -120,7 +120,9 @@ files:
|
|
|
120
120
|
- README.md
|
|
121
121
|
- lib/state_machines-activerecord.rb
|
|
122
122
|
- lib/state_machines/integrations/active_record.rb
|
|
123
|
+
- lib/state_machines/integrations/active_record/errors.rb
|
|
123
124
|
- lib/state_machines/integrations/active_record/locale.rb
|
|
125
|
+
- lib/state_machines/integrations/active_record/state_guard.rb
|
|
124
126
|
- lib/state_machines/integrations/active_record/type/integer.rb
|
|
125
127
|
- lib/state_machines/integrations/active_record/version.rb
|
|
126
128
|
homepage: https://github.com/state-machines/state_machines-activerecord/
|
|
@@ -135,14 +137,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
135
137
|
requirements:
|
|
136
138
|
- - ">="
|
|
137
139
|
- !ruby/object:Gem::Version
|
|
138
|
-
version: '
|
|
140
|
+
version: '4.0'
|
|
139
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
142
|
requirements:
|
|
141
143
|
- - ">="
|
|
142
144
|
- !ruby/object:Gem::Version
|
|
143
145
|
version: '0'
|
|
144
146
|
requirements: []
|
|
145
|
-
rubygems_version: 4.0.
|
|
147
|
+
rubygems_version: 4.0.20
|
|
146
148
|
specification_version: 4
|
|
147
149
|
summary: State machines Active Record Integration
|
|
148
150
|
test_files: []
|