Checking transition status

Using gent state_machine, I want to have checks that are only performed on transitions. For example:

# Configured elsewhere:
# StateMachine::Callback.bind_to_object = true

class Walrus > ActiveRecord::Base
  state_machine :life_cycle, :initial => :fetus do
    state :fetus
    state :child
    state :adult
    state :dead

    event :coming_of_age do
      transition :child => :adult
    end


    before_transition :child => :adult do
      validate :coming_of_age_party_in_the_future
    end
  end
end

If I attach this check to an adult, it will fail after this date. But I need it to be valid during the transition. I could add something like:

validate :coming_of_age_party_in_the_future, if: 'adult? && life_cycle_was == "child"'

but this does not seem to correspond to the transition point.

Also, since I am attached to an object in callbacks, how would it conditionally call the 'validate' method, a class method? (Binding is necessary since many methods in callbacks are private.)

+4
source share
1 answer

transition if: :x?

:

event :coming_of_age do
  transition :child => :adult, if: :coming_of_age_party_in_the_future
end

" " "" () " " " " ( ) README

+1

All Articles