Coder Social home page Coder Social logo

Comments (12)

homanchou avatar homanchou commented on June 20, 2024 1

I have a worker model that has a state machine:

  state_machine :auto_scopes => true do
    state :running # initial state
    state :completed #success
    state :aborted #errored out

    event :complete do
      transitions :from => [:running], :to => :completed
    end

    event :abort do
      transitions :from => [:running], :to => :aborted, :on_transition => [:send_admin_email]
    end

  end

But in the test I forgot that I renamed state 'in_progress' to 'running', so my test was doing:

worker = Sweeper.create(name:'remove_files', state:'in_progress')
worker.complete!

I would see an error like: "undefined method `call_action' for nil:NilClass"

That was kind of confusing for me. Probably a more helpful error might be "Invalid transition state 'in_progress' is not a defined state"

from transitions.

troessner avatar troessner commented on June 20, 2024

Thanks for the report - I edited your submission to make it look nicer using GH's markdown

```Ruby
# your_code 
```

Now, to the problem: Yes, you're right, this is a problem in the gem that went unnoticed because apparently you are the first person ever to use strings with whitespace in it as state names..:)

Quickfix: Use symbols only:

class Order < ActiveRecord::Base
  include ActiveModel::Transitions

  state_machine do

    state :received
    state :shipped

    event :ship do
      transitions :to => :shipped, :from => :received
    end
  end
end

I will look into this problem asap.

from transitions.

troessner avatar troessner commented on June 20, 2024

@jmonegro

In a nutshell: Don't use strings but symbols. Right now the gem expects symbols, nothing else.

I could update the gem so that it works with strings as well but this would mean a LOT of ugly code calling "to_sym" and type checks all over the place (even with using HashWithIndifferentAccess from ActiveSupport).

In addition, whitespace would never work since transitions possibly generates methods e.g.

def received?

which means that whitespace is prohibited anyway.

If you want to use a different display name for states than the state name itself you need to set up a mapping from state_name to display_name for yourself (there is a display_name functionality in the gem but it looks like it is broken).

I will make this clearer in the documenation, thanks for reporting this -> closed.

Addendum:

This means that you will need to rewrite your example to:

  state_machine do
      state :order_shipped
      state :order_received

      event :ship, success: :notify_customer do
        transitions :to => :order_shipped, :from => :order_received
      end
    end

from transitions.

jmonegro avatar jmonegro commented on June 20, 2024

Got it. I resorted to using "string symbols" (i.e. :'String Status') which
works for my purposes and implementation.

Thanks!

On Mon, Jul 30, 2012 at 9:12 AM, Timo Rößner <
[email protected]

wrote:

@jmonegro

In a nutshell: Don't use strings but symbols. Right now the gem expects
symbols, nothing else.

I could update the gem so that it works with strings as well but this
would mean a LOT of ugly code calling "to_sym" and type checks all over the
place (even with using HashWithIndifferentAccess from ActiveSupport).

In addition, whitespace would never work since transitions possibly
generates methods e.g.

def received?

which means that whitespace is prohibited anyway.

If you want to use a different display name for states than the state name
itself you need to set up a mapping from state_name to display_name for
yourself (there is a display_name functionality in the gem but it looks
like it is broken).

I will make this clearer in the documenation, thanks for reporting this ->
closed.


Reply to this email directly or view it on GitHub:
#60 (comment)

  • Joel

from transitions.

homanchou avatar homanchou commented on June 20, 2024

I just wanted to mention that this error can also occur in another scenario even if you are using symbols. I was writing some tests and decided to change the state machine state names, but I forgot that my test scaffold was still creating a mock record with the old state name. When it came time to transition the object's state, this same error is thrown.

from transitions.

jondecko avatar jondecko commented on June 20, 2024

@homanchou - Would you be able to give a glimpse into the before/after of the specific test you were working with that use the state machine?

from transitions.

jondecko avatar jondecko commented on June 20, 2024

@troessner Might be able to give some more insight/options on the type of error that could be thrown. It is a tad confusing like you said.

This specific error does remind me of a different (but has some crossover) issue ( #120) . In the other issue, there was invalid data within the database (invalid state). The same type of thing was exposed via your testing. Your test just happened to have invalid data this time! :)

from transitions.

homanchou avatar homanchou commented on June 20, 2024

Yup, it was my own fault for having bad data in the DB. I just noticed this in the README: "The explicitly specified state must be one of the states listed in the state definition below, otherwise transitions will raise a rather unhelpful exception like "NoMethodError: undefined method `call_action' for nil:NilClass" (there's a ticket to fix this already: #112)" , I remember reading that when I first started using the gem but didn't know what that was about. Now I do, I was totally bitten by it! :)

from transitions.

homanchou avatar homanchou commented on June 20, 2024

Actually, that reminds me. I think I helped reword that part of the README in PR, but back then I thought it was only regarding the explicitly defined INITIAL state had to be one of the states defined. But actually that goes for states that are in the DB as well.

from transitions.

troessner avatar troessner commented on June 20, 2024

@homanchou you're absolutely right, the error message IS absolutely confusing.

That was kind of confusing for me. Probably a more helpful error might be "Invalid transition state 'in_progress' is not a defined state"

I agree. Pull request welcome.:)
If you're not up to it, can you please open up a corresponding issue? I'll look into it once I find some time on my hands.

from transitions.

paulleader avatar paulleader commented on June 20, 2024

I encountered the same error when I typoed the name of the target state in an event. If I get the chance in the next week or so I'll do a fix.

from transitions.

alexanderadam avatar alexanderadam commented on June 20, 2024

I just ran into the same issue like @homanchou. A proper error message would be really nice.

from transitions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.