benzelano / test_lh_import

1 stars 0 forks source link

active_{record,model} custom validations ignored #87

Closed benzelano closed 13 years ago

benzelano commented 13 years ago

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:insend’ /Users/travis/.gem/ruby/1.8/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in run’ /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’]:

is not true. 189 tests, 201 assertions, 1 failures, 0 errors 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_model_test.rb Loaded suite /Users/travis/.gem/ruby/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader Started ..........................F.................................................................... Finished in 0.347041 seconds. 1) Failure: test_should_be_invalid_if_validation_fails_within_state_scope(ActiveModelTest::MachineWithCustomStateDrivenValidationsTest) [/Users/travis/dev/state_machine/test/unit/integrations/active_model_test.rb:756]: is not true. 95 tests, 102 assertions, 1 failures, 0 errors [original LH ticket](http://pluginaweek.lighthouseapp.com/projects/13288/tickets/87) This ticket has 0 attachment(s).
benzelano commented 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!