Created
July 22, 2011 18:42
-
-
Save timuruski/1100095 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'mongo_mapper' | |
MongoMapper.database = 'tmp' | |
class HasValidAccessTokensValidator < ActiveModel::EachValidator | |
VALID_ACCESS_TOKENS = ['read', 'write', 'admin'] | |
def validate_each(record, attribute, value) | |
unless value.all? { |token| VALID_ACCESS_TOKENS.include?(token) } | |
record.errors[attribute] << (options[:message] || "has invalid access_tokens") | |
end | |
end | |
end | |
class User | |
include MongoMapper::Document | |
key :access_tokens, Array | |
validates :access_tokens, :has_valid_access_tokens => true | |
end | |
p User.new(:access_tokens => ['read']).valid? | |
p User.new(:access_tokens => ['read','write']).valid? | |
p User.new(:access_tokens => ['read', 'write', 'super-duper-admin']).valid? | |
# For more information on ActiveModel validations: | |
# http://edgeguides.rubyonrails.org/3_0_release_notes.html#validations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment