Created
May 2, 2011 15:12
-
-
Save bkeepers/951751 to your computer and use it in GitHub Desktop.
Creating custom validations in MongoMapper
This file contains 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 MyCustomValidation | |
extend ActiveSupport::Concern | |
module InstanceMethods | |
def do_the_validation | |
errors.add(:foo, 'is not valid') unless some_condition_is_met? | |
end | |
def some_condition_is_met? | |
true | |
end | |
end | |
module ClassMethods | |
def validate_my_stuff | |
validate :do_the_validation | |
end | |
end | |
end | |
class MyThing | |
include MongoMapper::Document | |
plugin MyCustomValidation | |
validate_my_stuff | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment