Module: RailsStateMachine::Callbacks

Defined in:
lib/rails_state_machine/callbacks.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



4
5
6
7
# File 'lib/rails_state_machine/callbacks.rb', line 4

def included(model)
  register_callbacks(model)
  register_validations(model)
end

Instance Method Details

#flush_state_event_callbacks(name) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rails_state_machine/callbacks.rb', line 54

def flush_state_event_callbacks(name)
  if @state_event_callbacks
    while (event = @state_event_callbacks[name].shift)
      event.public_send("run_#{name}", self)
    end
  end
end

#register_state_events_for_callbacksObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails_state_machine/callbacks.rb', line 36

def register_state_events_for_callbacks
  @state_event_callbacks ||= {
    before_save: [],
    after_save: [],
    after_commit: []
  }
  state_machine_state_managers.each do |state_manager|
    if (next_event = state_manager.next_event)
      @state_event_callbacks[:before_save] << next_event
      @state_event_callbacks[:after_save] << next_event
      @state_event_callbacks[:after_commit] << next_event
      state_manager.next_event = nil
    end
  end

  true
end

#revert_statesObject



62
63
64
65
66
# File 'lib/rails_state_machine/callbacks.rb', line 62

def revert_states
  state_machine_state_managers.each do |state_manager|
    state_manager.revert
  end
end

#run_state_events_before_validationObject



28
29
30
31
32
33
34
# File 'lib/rails_state_machine/callbacks.rb', line 28

def run_state_events_before_validation
  # Since validations may be skipped, we will not register validation callbacks in @state_event_callbacks,
  # but call them explicitly when before_validation callbacks are triggered.
  state_machine_state_managers.each do |state_manager|
    state_manager.next_event&.run_before_validation(self)
  end
end