提交 e9e9ed6b 编写于 作者: C Carl Lerche

Be able to pass a validator method to #validates

上级 ed7614aa
......@@ -8,6 +8,12 @@ def _merge_attributes(attr_names)
end
end
class WithValidator < EachValidator
def validate_each(record, attr, val)
record.send options[:with]
end
end
module ClassMethods
# Passes the record off to the class or classes specified and allows them
# to add errors based on more complex conditions.
......
......@@ -171,4 +171,16 @@ def check_validity!
assert topic.errors[:title].empty?
assert topic.errors[:content].empty?
end
test "validates_with can validate with an instance method" do
Topic.validates :title, :with => :my_validation
topic = Topic.new :title => "foo"
assert topic.valid?
assert topic.errors[:title].empty?
topic = Topic.new
assert !topic.valid?
assert_equal ['is missing'], topic.errors[:title]
end
end
......@@ -29,4 +29,8 @@ def perform_after_validation
self.after_validation_performed = true
end
def my_validation
errors.add :title, "is missing" unless title
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册