Comments (12)
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.
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.
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.
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:
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.
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.
@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.
@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.
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.
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.
@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.
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.
I just ran into the same issue like @homanchou. A proper error message would be really nice.
from transitions.
Related Issues (20)
- getting "uninitialized constant ActiveRecord::Transitions" with rails 4.1 HOT 2
- can_event methods don't respect guards HOT 1
- Initial state is not set HOT 3
- Problem with Success Callback HOT 5
- Refactor our specs big time
- Introduce rubocop and auto-fix offenses HOT 1
- Fix all remaining rubocop offenses in lib/ and add rubocop to our travis build HOT 1
- Transitions#set_initial_state is forcibly inserting the "state" attribute HOT 6
- new feature: put exception as a param to event_failed HOT 1
- Success callback not fired HOT 1
- Passing parameters to events HOT 1
- Raise error when guard fails HOT 6
- Throw out callbacks... HOT 5
- Introduce Reek for CI HOT 1
- Introduce RDL
- available_state available_events should list states or events in the order they are defined HOT 4
- undefined method `state_machine' for nil:NilClass HOT 4
- Multiple timestamps on state transition HOT 3
- Publish 1.3.0 release HOT 2
- Tapioca DSL compiler interest?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from transitions.