Closed benzelano closed 13 years ago
Hi Travis,
I apologize for taking so long to get back to you! I took a look at your fork and the reason those tests fail is because the custom validation method is being defined in the event block and not the actual class. As a result, Rails is never able to find that method. To resolve this, you should define your class / state machine like so:
@@@ ruby class Vehicle state_machine do state :first_gear, :second_gear do validate :seatbelt_made_of_velvet end other_states :parked end
def seatbelt_made_of_velvet errors.add(:seatbelt, ’must be made of velvet’) unless seatbelt == :velvet end @@@
In the tests you provide, the definition of "seatbelt_made_of_velvet" occurs in the state block. That block gets executed in the context of the StateMachine::Machine instance. As a result, you end up defining a method on that object and not your class.
Hope this helps!
Travis Vachon opened this issue
Using custom validators as described here:
http://guides.rubyonrails.org/active_record_validations_callbacks.html#creating-custom-validation-methods
doesn’t seem to work - the method doesn’t appear to get called at all. I’ve added two tests demonstrating this behavior to this fork:
https://github.com/utahstreetlabs/state_machine
travis:~/dev/state_machine [git:master+?]
→ /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -I"lib:lib" -I"/Users/travis/.gem/ruby/1.8/gems/rake-0.9. 2/lib" "/Users/travis/.gem/ruby/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb" test/unit/integrations/active_record_test.rb Loaded suite /Users/travis/.gem/ruby/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader Started ..................................................................................................................................................................F.......................... Finished in 2.429645 seconds.
1) Failure: test_should_be_invalid_if_validation_fails_within_state_scope(ActiveRecordTest::MachineWithStateDrivenCustomValidationsTest) [/Users/travis/dev/state_machine/test/unit/integrations/active_record_test.rb:1110:in
test_should_be_invalid_if_validation_fails_within_state_scope’ /Users/travis/.gem/ruby/1.8/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
send’ /Users/travis/.gem/ruby/1.8/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:inrun’ /Users/travis/.gem/ruby/1.8/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:414:in
_run_setup_callbacks’ /Users/travis/.gem/ruby/1.8/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in `run’]: