Created
August 21, 2012 01:28
-
-
Save cobbr2/3410344 to your computer and use it in GitHub Desktop.
Can't get my state_machine to Enum erate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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