def transition(event_name)
      clear_raised_error
      if to = next_state(event_name)
        begin
          result = yield
        rescue => e
          error_state = error_state(event_name, e) ||
            state_machine_definition.default_error_state
          if error_state
            @raised_error = e
            @subject.send("#{state_method}=", error_state)
            return result
          else
            raise
          end
        end
        # TODO refactor out to AR module
        if defined?(::ActiveRecord) && @subject.is_a?(::ActiveRecord::Base)
          if @subject.errors.entries.empty?
            @subject.send("#{state_method}=", to)
            return true
          else
            return false
          end
        else
          @subject.send("#{state_method}=", to)
          return result
        end
      else
        illegal_event_callback event_name
      end
    end
def transition(event_name)
      clear_raised_error
      if to = next_state(event_name)
        begin
          result = yield
        rescue => e
          error_state = error_state(event_name, e) ||
            state_machine_definition.default_error_state
          if error_state
            @raised_error = e
            @subject.send("#{state_method}=", error_state)
            return result
          else
            raise
          end
        end
        # TODO refactor out to AR module
        if defined?(::ActiveRecord) && @subject.is_a?(::ActiveRecord::Base)
          if @subject.errors.entries.empty?
            @subject.send("#{state_method}=", to)
            return true
          else
            return false
          end
        else
          @subject.send("#{state_method}=", to)
          return result
        end
      else
        illegal_event_callback event_name
      end
    end
Transitions to the next state if next_state exists.
 When an error occurs, it uses the error to determine next state.
 If no next state can be determined it transitions to the default error
 state if defined, otherwise the error is re-raised.
 Calls illegal_event_callback event_name if no next_state is found