Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Future request] :transitionsif option

18 views
Skip to first unread message

akalyaev

unread,
Sep 30, 2012, 3:25:50 PM9/30/12
to pluginaw...@googlegroups.com
Hello,

Usually, all my `ActiveRecord` models have a state_machine to control the state of the record (e.g. :new, :archived, :deleted). That is how I am implementing a safe delete mechanizm. All of the transitions of such a state_machine should only occur if object is persisted in the DB. Therefore, for each transition I have to duplicate `:if` condition.

    state_machine :state, :initial => :active do
      state :active
      state :archived
      state :deleted

      event :activate do
        transition any - :active => :active, :if => :persisted?
      end

      event :archive do
        transition :active => :archived, :if => :persisted?
      end

      event :del do
        transition any - :deleted => :deleted, :if => :persisted?
      end
    end

I thought, it would be nice to have an option like :transitionsif, which will be a method, proc or string to call to determine if any determined transition is possible.

   state_machine :state, :initial => :active, :transitionsif => :persisted? do
      state :active
      state :archived
      state :deleted

      event :activate do
        transition any - :active => :active
      end

      event :archive do
        transition :active => :archived
      end

      event :del do
        transition any - :deleted => :deleted
      end
    end

    
What do you think?
Reply all
Reply to author
Forward
0 new messages