class RailsStateMachine::StateManager

Attributes

next_event[RW]
state_before_state_event[RW]

Public Class Methods

new(record, state_machine, state_attribute) click to toggle source
# File lib/rails_state_machine/state_manager.rb, line 5
def initialize(record, state_machine, state_attribute)
  @record = record
  @state_machine = state_machine
  @state_attribute = state_attribute
end

Public Instance Methods

revert() click to toggle source
# File lib/rails_state_machine/state_manager.rb, line 23
def revert
  self.state = @state_before_state_event if @next_event
end
source_state() click to toggle source
# File lib/rails_state_machine/state_manager.rb, line 27
def source_state
  if @record.new_record?
    state
  else
    state_in_database
  end
end
state() click to toggle source
# File lib/rails_state_machine/state_manager.rb, line 11
def state
  @record.public_send(@state_attribute)
end
state=(value) click to toggle source
# File lib/rails_state_machine/state_manager.rb, line 19
def state=(value)
  @record.public_send(:"#{@state_attribute}=", value)
end
state_in_database() click to toggle source
# File lib/rails_state_machine/state_manager.rb, line 15
def state_in_database
  @record.public_send(:"#{@state_attribute}_in_database").to_s
end
transition_allowed_for?(event_name) click to toggle source
# File lib/rails_state_machine/state_manager.rb, line 43
def transition_allowed_for?(event_name)
  @state_machine.find_event(event_name).allowed_from?(source_state)
end
transition_to(event_name) click to toggle source
# File lib/rails_state_machine/state_manager.rb, line 35
def transition_to(event_name)
  @next_event = @state_machine.find_event(event_name)
  @state_before_state_event = source_state

  # If the event can not transition from source_state, a TransitionNotFoundError will be raised
  self.state = @next_event.future_state_name(source_state).to_s
end