Wednesday, 11 September 2013

rails4 state_machine validations not working

rails4 state_machine validations not working

I'm trying to use state_machines validations but they don't appear to work.
I tried many difference kinds of validations, but when I call an event on
my object, the transition fires but there is no validation error..., ie
c.complete! or c.complete
c.errors.size
=> 0
Validations outside my state_machine work fine. I also tried moving the
state_machine out of the concern to just be sure and there was no change.
Any ideas?
In my event_log.rb model I have:
class EventLog < ActiveRecord::Base
##STATE MACHINE
include Stateable
and I moved them into a concern...
module Stateable
extend ActiveSupport::Concern
#include ActiveModel::Validations
included do
##SCOPES
scope :pending, -> { with_state(:pending)}
state_machine :initial => :incomplete do
before_transition :on => [:complete,:cover], :do => :complete_item
##BAD THINGS
before_transition :on => :no_show, :do => :no_show_item
before_transition :on => :cancellation, :do => :cancel_item
before_transition :on => :no_quiz, :do => :no_quiz_item
event :complete, :cover do
transition :incomplete => :completed
end
event :needs_cover do
transition :incomplete => :pending
end
event :no_show, :cancellation, :no_quiz do
transition [:incomplete,:pending] => :failed
end
##VALIDATIONS
state :complete do
validates_presence_of :head_count
end
end
end
# defs are here
end

No comments:

Post a Comment