Class: HasStateMachine::State

Inherits:
String
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Model
Includes:
ActiveModel::Validations
Defined in:
lib/has_state_machine/state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ State

Initializes the HasStateMachine::State instance.

Examples:

state = Workflow::Post::Draft.new(post) #=> "draft"


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

#objectObject (readonly)

Returns the value of attribute object.



11
12
13
# File 'lib/has_state_machine/state.rb', line 11

def object
  @object
end

#stateObject (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_transitionsObject



129
130
131
# File 'lib/has_state_machine/state.rb', line 129

def possible_transitions
  @possible_transitions || []
end

.stateObject



133
134
135
# File 'lib/has_state_machine/state.rb', line 133

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.



153
154
155
156
# File 'lib/has_state_machine/state.rb', line 153

def state_options(transitions_to: [], transactional: false)
  @possible_transitions = transitions_to.map(&:to_s)
  @transactional = transactional
end

.transactional?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/has_state_machine/state.rb', line 137

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.



144
145
146
147
# File 'lib/has_state_machine/state.rb', line 144

def transitions_to(states)
  state_options(transitions_to: states)
  HasStateMachine::Deprecation.deprecation_warning(:transitions_to, "use state_options instead")
end

Instance Method Details

#errorsObject

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
# 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
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_transitionsObject

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"

#transitionObject

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.

Parameters:

  • desired_state (String)

    the state to transition to

  • options (Hash)

    a hash of additional options for transitioning the object

Returns:

  • (Boolean)

    whether or not the transition took place



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, **options)
  transitioned = false

  with_transition_options(options) 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