public
Created

Can't get my state_machine to Enum erate

  • Download Gist
vehicle_enum.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
class VehicleEnum
include DataMapper::Resource
 
property :id, Serial
property :name, String
property :state, Enum[ :parked, :idling ]
 
state_machine :initial => :parked do
event :ignite do
transition :parked => :idling
end
 
state :idling do
validates_with_method :name_is_fred
end
end
 
def name_is_fred
return true if name == "fred"
return [false, "Only fred can be idling"]
end
end
 
fred = VehicleEnum.create({:name => 'fred'})
fred.ignite or raise "failed: couldn't ignite fred"
fred.clean? or raise "failed: couldn't save an idle fred"
 
wilma = VehicleEnum.create({:name => 'wilma'})
!wilma.ignite or raise "failed: could ignite wilma"
!wilma.errors.blank? or raise "failed: wilma has no errors"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.