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
-
#errors ⇒ Object
Add errors to the ActiveRecord object rather than the HasStateMachine::State class.
-
#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.
33 34 35 36 37 |
# File 'lib/has_state_machine/state.rb', line 33 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
131 132 133 |
# File 'lib/has_state_machine/state.rb', line 131 def possible_transitions @possible_transitions || [] end |
.state ⇒ Object
135 136 137 |
# File 'lib/has_state_machine/state.rb', line 135 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.
155 156 157 158 |
# File 'lib/has_state_machine/state.rb', line 155 def (transitions_to: [], transactional: false) @possible_transitions = transitions_to.map(&:to_s) @transactional = transactional end |
.transactional? ⇒ Boolean
139 140 141 |
# File 'lib/has_state_machine/state.rb', line 139 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.
146 147 148 149 |
# File 'lib/has_state_machine/state.rb', line 146 def transitions_to(states) (transitions_to: states) HasStateMachine::Deprecation.deprecation_warning(:transitions_to, "use state_options instead") end |
Instance Method Details
#errors ⇒ Object
Add errors to the ActiveRecord object rather than the HasStateMachine::State class.
26 |
# File 'lib/has_state_machine/state.rb', line 26 delegate :errors, to: :object |
#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.
80 81 82 83 84 85 86 87 88 |
# File 'lib/has_state_machine/state.rb', line 80 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.
70 71 72 73 74 |
# File 'lib/has_state_machine/state.rb', line 70 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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/has_state_machine/state.rb', line 49 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 |