简体   繁体   中英

Rails: state_machine method for this or later states?

Is there any way to access that functionality within the state_machine gem? Kinda like levels:

def check_if_editor
  redirect_to :root unless current_user.editor? OR ANY NEXT STATE
end

Can't find much in the docs. Thanks!

I don't think there is. I've come across the same requirement and solved it by creating a method that checks each acceptable state. I'm not entirely happy with it because if a new state gets introduced it potentially needs to be added to the list.

def after_state1?
  state2? || state3?
end

I saw a closed discussion on the state_machine gem (can't find it again now) where they said they didn't want to implement state ordering because it would make it too complicated.

You can use state machine methods state_paths (which returns an array of transitions from one specified state to another) and to_states (which converts the result to a nice array of states).

redirect_to :root unless editor_or_later?

def editor_or_later?
  states_after_editor = current_user.state_paths(:from => :editor, :to => :some_end_state).to_states
  states_editor_or_later = [:editor] +  states_after_editor

  states_editor_or_later.include? current_user.state.to_sym
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM