Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
27906763
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
27906763
编写于
12月 23, 2009
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
validates_each uses a BlockValidator.
上级
977a5c43
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
49 addition
and
41 deletion
+49
-41
activemodel/lib/active_model.rb
activemodel/lib/active_model.rb
+1
-0
activemodel/lib/active_model/validations.rb
activemodel/lib/active_model/validations.rb
+23
-33
activemodel/lib/active_model/validations/length.rb
activemodel/lib/active_model/validations/length.rb
+3
-4
activemodel/lib/active_model/validations/with.rb
activemodel/lib/active_model/validations/with.rb
+2
-2
activemodel/lib/active_model/validator.rb
activemodel/lib/active_model/validator.rb
+20
-2
未找到文件。
activemodel/lib/active_model.rb
浏览文件 @
27906763
...
...
@@ -46,6 +46,7 @@ module ActiveModel
autoload
:ValidationsRepairHelper
autoload
:Validator
autoload
:EachValidator
,
'active_model/validator'
autoload
:BlockValidator
,
'active_model/validator'
autoload
:VERSION
module
Serializers
...
...
activemodel/lib/active_model/validations.rb
浏览文件 @
27906763
...
...
@@ -12,6 +12,29 @@ module Validations
end
module
ClassMethods
# Validates each attribute against a block.
#
# class Person < ActiveRecord::Base
# validates_each :first_name, :last_name do |record, attr, value|
# record.errors.add attr, 'starts with z.' if value[0] == ?z
# end
# end
#
# Options:
# * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
# * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
def
validates_each
(
*
attr_names
,
&
block
)
options
=
attr_names
.
extract_options!
.
symbolize_keys
validates_with
BlockValidator
,
options
.
merge
(
:attributes
=>
attr_names
.
flatten
),
&
block
end
# Adds a validation method or block to the class. This is useful when
# overriding the +validate+ instance method becomes too unwieldly and
# you're looking for more descriptive declaration of your validations.
...
...
@@ -39,39 +62,6 @@ module ClassMethods
# end
#
# This usage applies to +validate_on_create+ and +validate_on_update as well+.
# Validates each attribute against a block.
#
# class Person < ActiveRecord::Base
# validates_each :first_name, :last_name do |record, attr, value|
# record.errors.add attr, 'starts with z.' if value[0] == ?z
# end
# end
#
# Options:
# * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
# * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
def
validates_each
(
*
attrs
)
options
=
attrs
.
extract_options!
.
symbolize_keys
attrs
=
attrs
.
flatten
# Declare the validation.
validate
options
do
|
record
|
attrs
.
each
do
|
attr
|
value
=
record
.
send
(
:read_attribute_for_validation
,
attr
)
next
if
(
value
.
nil?
&&
options
[
:allow_nil
])
||
(
value
.
blank?
&&
options
[
:allow_blank
])
yield
record
,
attr
,
value
end
end
end
def
validate
(
*
args
,
&
block
)
options
=
args
.
last
if
options
.
is_a?
(
Hash
)
&&
options
.
key?
(
:on
)
...
...
activemodel/lib/active_model/validations/length.rb
浏览文件 @
27906763
...
...
@@ -10,8 +10,8 @@ class LengthValidator < EachValidator
def
initialize
(
options
)
options
[
:tokenizer
]
||=
DEFAULT_TOKENIZER
super
@type
=
(
OPTIONS
&
options
.
keys
).
first
super
end
def
check_validity!
...
...
@@ -108,9 +108,8 @@ module ClassMethods
# count words as in above example.)
# Defaults to <tt>lambda{ |value| value.split(//) }</tt> which counts individual characters.
def
validates_length_of
(
*
attr_names
)
options
=
{
:tokenizer
=>
DEFAULT_TOKENIZER
}
options
.
update
(
attr_names
.
extract_options!
)
validates_with
LengthValidator
,
options
.
merge
(
:attributes
=>
attr_names
,
:type
=>
type
)
options
=
attr_names
.
extract_options!
validates_with
LengthValidator
,
options
.
merge
(
:attributes
=>
attr_names
)
end
alias_method
:validates_size_of
,
:validates_length_of
...
...
activemodel/lib/active_model/validations/with.rb
浏览文件 @
27906763
...
...
@@ -48,9 +48,9 @@ module ClassMethods
# end
# end
#
def
validates_with
(
*
args
)
def
validates_with
(
*
args
,
&
block
)
options
=
args
.
extract_options!
args
.
each
{
|
klass
|
validate
(
klass
.
new
(
options
),
options
)
}
args
.
each
{
|
klass
|
validate
(
klass
.
new
(
options
,
&
block
),
options
)
}
end
end
end
...
...
activemodel/lib/active_model/validator.rb
浏览文件 @
27906763
...
...
@@ -51,7 +51,6 @@ module ActiveModel #:nodoc:
# @my_custom_field = options[:field_name] || :first_name
# end
# end
#
class
Validator
attr_reader
:options
...
...
@@ -64,6 +63,11 @@ def validate(record)
end
end
# EachValidator is a validator which iterates through the attributes given
# in the options hash invoking the validate_each method passing in the
# record, attribute and value.
#
# All ActiveModel validations are built on top of this Validator.
class
EachValidator
<
Validator
attr_reader
:attributes
...
...
@@ -81,11 +85,25 @@ def validate(record)
end
end
def
validate_each
(
record
)
def
validate_each
(
record
,
attribute
,
value
)
raise
NotImplementedError
end
def
check_validity!
end
end
# BlockValidator is a special EachValidator which receives a block on initialization
# and call this block for each attribute being validated. +validates_each+ uses this
# Validator.
class
BlockValidator
<
EachValidator
def
initialize
(
options
,
&
block
)
@block
=
block
super
end
def
validate_each
(
record
,
attribute
,
value
)
@block
.
call
(
record
,
attribute
,
value
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录