Skip to content

Instantly share code, notes, and snippets.

@cobbr2
Created August 21, 2012 01:28
Show Gist options
  • Save cobbr2/3410344 to your computer and use it in GitHub Desktop.
Save cobbr2/3410344 to your computer and use it in GitHub Desktop.
Can't get my state_machine to Enum erate
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment