提交 92160219 编写于 作者: J Josh Kalderimis 提交者: Carl Lerche

minor changes to instance level validations implementation based on feedback from José Valim

上级 9131a88b
......@@ -47,7 +47,8 @@ module Validations
included do
extend ActiveModel::Translation
extend HelperMethods; include HelperMethods
extend HelperMethods
include HelperMethods
define_callbacks :validate, :scope => :name
......
module ActiveModel
module Validations
module HelperMethods
private
def _merge_attributes(attr_names)
options = attr_names.extract_options!
options.merge(:attributes => attr_names.flatten)
end
end
end
end
\ No newline at end of file
module ActiveModel
module Validations
module HelperMethods
private
def _merge_attributes(attr_names)
options = attr_names.extract_options!
options.merge(:attributes => attr_names.flatten)
end
end
module ClassMethods
# Passes the record off to the class or classes specified and allows them
......@@ -89,18 +97,8 @@ def validates_with(*args, &block)
# end
# end
#
# class MyValidator < ActiveModel::Validator
# def validate(record)
# if some_complex_logic
# record.errors[:base] << "This record is invalid"
# end
# end
#
# private
# def some_complex_logic
# # ...
# end
# end
# Please consult the class method documentation for more information on
# creating your own validator.
#
# You may also pass it multiple classes, like so:
#
......@@ -120,24 +118,13 @@ def validates_with(*args, &block)
# in the callback
#
# If you pass any additional configuration options, they will be passed
# to the class and available as <tt>options</tt>:
#
# class Person
# include ActiveModel::Validations
# validates_with MyValidator, :my_custom_key => "my custom value"
# end
#
# class MyValidator < ActiveModel::Validator
# def validate(record)
# options[:my_custom_key] # => "my custom value"
# end
# end
# to the class and available as <tt>options</tt>, please refer to the
# class version of this method for more information
#
def validates_with(*args, &block)
options = args.extract_options!
args.each do |klass|
validator = klass.new(options, &block)
validator.setup(self) if validator.respond_to?(:setup)
validator.validate(self)
end
end
......
......@@ -88,6 +88,9 @@ module ActiveModel #:nodoc:
# klass.send :attr_accessor, :custom_attribute
# end
# end
#
# This setup method is only called when used with validation macros or the
# class level <tt>validates_with</tt> method.
#
class Validator
attr_reader :options
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册