class RailsStateMachine::Event

Constants

ExistingTransitionError
Transition
TransitionNotFoundError
UndefinedStateError

Attributes

name[R]

Public Class Methods

new(name, state_machine) click to toggle source
# File lib/rails_state_machine/event.rb, line 11
def initialize(name, state_machine)
  @name = name
  @state_machine = state_machine

  @before_validation = []
  @before_save = []
  @after_save = []
  @after_commit = []

  @transitions_by_state_name = {}
end

Public Instance Methods

allowed_from?(state_name) click to toggle source
# File lib/rails_state_machine/event.rb, line 63
def allowed_from?(state_name)
  @transitions_by_state_name.key?(state_name&.to_sym)
end
configure(&block) click to toggle source
# File lib/rails_state_machine/event.rb, line 23
def configure(&block)
  instance_eval(&block)
end
find_transition_from(state_name) click to toggle source
# File lib/rails_state_machine/event.rb, line 59
def find_transition_from(state_name)
  @transitions_by_state_name[state_name&.to_sym] || raise(TransitionNotFoundError, "#{name} does not transition from #{state_name}; defined are #{transitions}")
end
future_state_name(state_name) click to toggle source
# File lib/rails_state_machine/event.rb, line 67
def future_state_name(state_name)
  find_transition_from(state_name).to
end
run_after_commit(record) click to toggle source
# File lib/rails_state_machine/event.rb, line 53
def run_after_commit(record)
  @after_commit.each do |block|
    record.instance_eval(&block)
  end
end
run_after_save(record) click to toggle source
# File lib/rails_state_machine/event.rb, line 47
def run_after_save(record)
  @after_save.each do |block|
    record.instance_eval(&block)
  end
end
run_before_save(record) click to toggle source
# File lib/rails_state_machine/event.rb, line 41
def run_before_save(record)
  @before_save.each do |block|
    record.instance_eval(&block)
  end
end
run_before_validation(record) click to toggle source
# File lib/rails_state_machine/event.rb, line 35
def run_before_validation(record)
  @before_validation.each do |block|
    record.instance_eval(&block)
  end
end
transitions(**options) click to toggle source
# File lib/rails_state_machine/event.rb, line 27
def transitions(**options)
  if options.present?
    add_transitions(**options)
  else
    @transitions_by_state_name.values
  end
end

Private Instance Methods

add_transition(from, to) click to toggle source
# File lib/rails_state_machine/event.rb, line 78
def add_transition(from, to)
  if !@state_machine.has_state?(from)
    raise UndefinedStateError, "#{from} is not a valid state in the state machine of #{@state_machine.model}"
  elsif allowed_from?(from)
    raise ExistingTransitionError, "#{name} already defines a transition from #{from} (to #{future_state_name(from)})"
  else
    @transitions_by_state_name[from] = Transition.new(from, to)
  end
end
add_transitions(from:, to:) click to toggle source
# File lib/rails_state_machine/event.rb, line 73
def add_transitions(from:, to:)
  froms = Array(from)
  froms.each { |from| add_transition(from, to) }
end
after_commit(&block) click to toggle source
# File lib/rails_state_machine/event.rb, line 100
def after_commit(&block)
  @after_commit << block
end
after_save(&block) click to toggle source
# File lib/rails_state_machine/event.rb, line 96
def after_save(&block)
  @after_save << block
end
before_save(&block) click to toggle source
# File lib/rails_state_machine/event.rb, line 92
def before_save(&block)
  @before_save << block
end
before_validation(&block) click to toggle source
# File lib/rails_state_machine/event.rb, line 88
def before_validation(&block)
  @before_validation << block
end