Module: SolidState

Extended by:
ActiveSupport::Concern
Defined in:
lib/solidstate.rb

Defined Under Namespace

Modules: ClassMethods Classes: InvalidTransitionError

Constant Summary collapse

STATE_ATTRIBUTE =
:state.freeze

Instance Method Summary collapse

Instance Method Details

#can_transition_to?(new_state) ⇒ Boolean



85
86
87
88
89
90
# File 'lib/solidstate.rb', line 85

def can_transition_to?(new_state)
  return true if state.to_sym == new_state.to_sym

  possible = self.class.state_transitions[state.to_sym] || []
  possible.include?(new_state.to_sym)
end

#ensure_valid_transitionObject



79
80
81
82
83
# File 'lib/solidstate.rb', line 79

def ensure_valid_transition
  if send("#{STATE_ATTRIBUTE}_changed?") and !can_transition_to?(state)
    errors.add(STATE_ATTRIBUTE, "can't transition from current state to #{state}")
  end
end

#set_state(new_state) ⇒ Object



74
75
76
77
# File 'lib/solidstate.rb', line 74

def set_state(new_state)
  return false unless can_transition_to?(new_state)
  self.state = new_state
end