Created
May 7, 2010 13:23
-
-
Save jnunemaker/393411 to your computer and use it in GitHub Desktop.
Validates existence of in MongoMapper
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
module ValidatesExistenceOfPlugin | |
def self.included(model) | |
model.plugin ValidatesExistenceOfPlugin | |
end | |
module ClassMethods | |
def validates_existence_of(*args) | |
add_validations(args, ValidatesExistenceOfPlugin::ValidatesExistenceOf) | |
end | |
end | |
class ValidatesExistenceOf < Validatable::ValidationBase | |
def valid?(instance) | |
instance.send(attribute).present? | |
end | |
def message(instance) | |
super || "does not exist" | |
end | |
end | |
end | |
MongoMapper::Document.append_inclusions(ValidatesExistenceOfPlugin) | |
MongoMapper::EmbeddedDocument.append_inclusions(ValidatesExistenceOfPlugin) |
This is stupid actually. Just use validates_presence_of. Sometimes one over thinks...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage in combination with belongs_to's: