Skip to content

Instantly share code, notes, and snippets.

@bkeepers
Created May 2, 2011 15:12
Show Gist options
  • Save bkeepers/951751 to your computer and use it in GitHub Desktop.
Save bkeepers/951751 to your computer and use it in GitHub Desktop.
Creating custom validations in MongoMapper
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