Class: HasStateMachine::State
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Model
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/has_state_machine/state.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
- .possible_transitions ⇒ Object
- .state ⇒ Object
-
.state_options(transitions_to: [], transactional: false) ⇒ Object
Set the options for the HasStateMachine::State classes to define the possible states the current state can transition to and whether or not transitioning to the state should be performed within a transaction.
- .transactional? ⇒ Boolean
-
.transitions_to(states) ⇒ Object
Setter for the HasStateMachine::State classes to define the possible states the current state can transition to.
Instance Method Summary collapse
-
#initialize(object) ⇒ State
constructor
Initializes the HasStateMachine::State instance.
-
#perform_transactional_transition! ⇒ Object
Makes the actual transition from one state to the next and runs the before and after transition callbacks in a transaction to allow for roll backs.
-
#perform_transition! ⇒ Object
Makes the actual transition from one state to the next and runs the before and after transition callbacks.
-
#possible_transitions ⇒ Object
possible_transitions - Retrieves the next available transitions for a given state.
-
#transition ⇒ Object
Defines the before_transition and after_transition callbacks for use on a HasStateMachine::State instance.
-
#transition_to(desired_state, **options) ⇒ Boolean
Checks to see if the desired state is valid and then gives responsibility to the desired state’s instance to make the transition.
Constructor Details
#initialize(object) ⇒ State
Initializes the HasStateMachine::State instance.
28 29 30 31 32 |
# File 'lib/has_state_machine/state.rb', line 28 def initialize(object) @object = object super(state) end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
11 12 13 |
# File 'lib/has_state_machine/state.rb', line 11 def object @object end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
11 12 13 |
# File 'lib/has_state_machine/state.rb', line 11 def state @state end |
Class Method Details
.possible_transitions ⇒ Object
126 127 128 |
# File 'lib/has_state_machine/state.rb', line 126 def possible_transitions @possible_transitions || [] end |
.state ⇒ Object
130 131 132 |
# File 'lib/has_state_machine/state.rb', line 130 def state to_s.demodulize.underscore end |
.state_options(transitions_to: [], transactional: false) ⇒ Object
Set the options for the HasStateMachine::State classes to define the possible states the current state can transition to and whether or not transitioning to the state should be performed within a transaction.
150 151 152 153 |
# File 'lib/has_state_machine/state.rb', line 150 def (transitions_to: [], transactional: false) @possible_transitions = transitions_to.map(&:to_s) @transactional = transactional end |
.transactional? ⇒ Boolean
134 135 136 |
# File 'lib/has_state_machine/state.rb', line 134 def transactional? @transactional || false end |
.transitions_to(states) ⇒ Object
Setter for the HasStateMachine::State classes to define the possible states the current state can transition to.
141 142 143 144 |
# File 'lib/has_state_machine/state.rb', line 141 def transitions_to(states) (transitions_to: states) HasStateMachine::Deprecation.deprecation_warning(:transitions_to, "use state_options instead") end |
Instance Method Details
#perform_transactional_transition! ⇒ Object
Makes the actual transition from one state to the next and runs the before and after transition callbacks in a transaction to allow for roll backs.
75 76 77 78 79 80 81 82 83 |
# File 'lib/has_state_machine/state.rb', line 75 def perform_transactional_transition! ActiveRecord::Base.transaction(requires_new: true, joinable: false) do run_callbacks :transition do rollback_transition unless object.update("#{object.state_attribute}": state) end end object.reload.public_send(object.state_attribute) == state end |
#perform_transition! ⇒ Object
Makes the actual transition from one state to the next and runs the before and after transition callbacks.
65 66 67 68 69 |
# File 'lib/has_state_machine/state.rb', line 65 def perform_transition! run_callbacks :transition do object.update("#{object.state_attribute}": state) end end |
#possible_transitions ⇒ Object
possible_transitions - Retrieves the next available transitions for a given state. transactional? - Determines whether or not the transition should happen with a transactional block.
21 |
# File 'lib/has_state_machine/state.rb', line 21 delegate :possible_transitions, :transactional?, :state, to: "self.class" |
#transition ⇒ Object
Defines the before_transition and after_transition callbacks for use on a HasStateMachine::State instance.
16 |
# File 'lib/has_state_machine/state.rb', line 16 define_model_callbacks :transition, only: %i[before after] |
#transition_to(desired_state, **options) ⇒ Boolean
Checks to see if the desired state is valid and then gives responsibility to the desired state’s instance to make the transition.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/has_state_machine/state.rb', line 44 def transition_to(desired_state, **) transitioned = false () do return false unless valid_transition?(desired_state.to_s) desired_state = state_instance(desired_state.to_s) transitioned = if desired_state.transactional? desired_state.perform_transactional_transition! else desired_state.perform_transition! end end transitioned end |