Skip to content

Commit

Permalink
state inclusion validator added (now there is no possibility to arbit…
Browse files Browse the repository at this point in the history
…rary set state to value different, than state list
  • Loading branch information
mlitwiniuk authored and Jakub Kuźma committed Jul 29, 2010
1 parent 0b748ed commit ff98acb
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/active_record/transitions.rb
Expand Up @@ -28,6 +28,7 @@ module Transitions
include ::Transitions
before_validation :set_initial_state
validates_presence_of :state
validate :state_inclusion
end

protected
Expand All @@ -43,5 +44,12 @@ def read_state(state_machine)
def set_initial_state
self.state ||= self.class.state_machine.initial_state.to_s
end

def state_inclusion
unless self.class.state_machine.states.map{|s| s.name.to_s }.include?(self.state.to_s)
self.errors.add(:state, :inclusion, :value => self.state)
end
end
end
end

0 comments on commit ff98acb

Please sign in to comment.