Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created July 22, 2011 18:42
Show Gist options
  • Save timuruski/1100095 to your computer and use it in GitHub Desktop.
Save timuruski/1100095 to your computer and use it in GitHub Desktop.
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