Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't pass method symbol name to if #183

Closed
mhmaguire opened this issue Jul 29, 2016 · 2 comments
Closed

Can't pass method symbol name to if #183

mhmaguire opened this issue Jul 29, 2016 · 2 comments

Comments

@mhmaguire
Copy link

Condition on an event doesn't take method name symbol as indicated in the documentation

class Thing < ActiveRecord::Base
    include Workflow 

    workflow do 
        state :on do 
            event :turn_off, transitions_to: :off
        end

        state :off do 
            event :turn_on, transitions_to: :on, if: :has_power
        end
    end

    def has_power
        power > 0
    end

    def turn_on
        halt!('cannot turn on') unless check
    end
end

Thing.new # => TypeError: condition must be nil or callable (eg. a proc or lambda)

with 1.2.0

@mhmaguire
Copy link
Author

hmmm i see that source for 1.2.0 doesn't support symbol method names so perhaps this is a non issue

@dan-jensen
Copy link

Yeah, the documentation still needs to be corrected. The conditional event transitions should only show the proc example.

For anyone who wants to use the same proc in multiple places (like an instance method), you can store the proc to a constant like this:

TRANSITIONS_TO_ANOTHER_STATE = proc do |record|
  record.should_transition?
end

workflow do
  state :default do
    event :something_happened, transitions_to: :another_state, if: TRANSITIONS_TO_ANOTHER_STATE
  end
end

@geekq geekq closed this as completed May 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants