Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class TransitionValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return unless record.changes[attribute]
transitions = options
start = record.send(:"#{attribute}_was")
destination = value
message = options[:message] || "can not be transitioned from #{start.inspect} to #{destination.inspect}"
allowed_transitions = transitions[start]
unless allowed_transitions
raise Error, record.errors.full_message(attribute, message)
end
unless Array(allowed_transitions).map(&:to_sym).include?(destination.to_sym)
raise Error, record.errors.full_message(attribute, message)
end
end
class Error < StandardError
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment