Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6a78e0ec
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,体验更适合开发者的 AI 搜索 >>
未验证
提交
6a78e0ec
编写于
10月 10, 2016
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Removed deprecated :tokenizer in the length validator
上级
9de6457a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
5 addition
and
74 deletion
+5
-74
activemodel/CHANGELOG.md
activemodel/CHANGELOG.md
+4
-0
activemodel/lib/active_model/validations/length.rb
activemodel/lib/active_model/validations/length.rb
+1
-34
activemodel/test/cases/validations/length_validation_test.rb
activemodel/test/cases/validations/length_validation_test.rb
+0
-36
activemodel/test/models/topic.rb
activemodel/test/models/topic.rb
+0
-4
未找到文件。
activemodel/CHANGELOG.md
浏览文件 @
6a78e0ec
*
Removed deprecated
`:tokenizer`
in the length validator.
*Rafael Mendonça França*
*
Removed deprecated methods in
`ActiveModel::Errors`
.
`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
...
...
activemodel/lib/active_model/validations/length.rb
浏览文件 @
6a78e0ec
...
...
@@ -6,7 +6,7 @@ class LengthValidator < EachValidator # :nodoc:
MESSAGES
=
{
is: :wrong_length
,
minimum: :too_short
,
maximum: :too_long
}.
freeze
CHECKS
=
{
is: :==
,
minimum:
:
>=
,
maximum: :<=
}.
freeze
RESERVED_OPTIONS
=
[
:minimum
,
:maximum
,
:within
,
:is
,
:to
kenizer
,
:to
o_short
,
:too_long
]
RESERVED_OPTIONS
=
[
:minimum
,
:maximum
,
:within
,
:is
,
:too_short
,
:too_long
]
def
initialize
(
options
)
if
range
=
(
options
.
delete
(
:in
)
||
options
.
delete
(
:within
))
...
...
@@ -18,27 +18,6 @@ def initialize(options)
options
[
:minimum
]
=
1
end
if
options
[
:tokenizer
]
ActiveSupport
::
Deprecation
.
warn
(
<<-
EOS
.
strip_heredoc
)
The `:tokenizer` option is deprecated, and will be removed in Rails 5.1.
You can achieve the same functionality by defining an instance method
with the value that you want to validate the length of. For example,
validates_length_of :essay, minimum: 100,
tokenizer: ->(str) { str.scan(/
\w
+/) }
should be written as
validates_length_of :words_in_essay, minimum: 100
private
def words_in_essay
essay.scan(/
\w
+/)
end
EOS
end
super
end
...
...
@@ -59,7 +38,6 @@ def check_validity!
end
def
validate_each
(
record
,
attribute
,
value
)
value
=
tokenize
(
record
,
value
)
value_length
=
value
.
respond_to?
(
:length
)
?
value
.
length
:
value
.
to_s
.
length
errors_options
=
options
.
except
(
*
RESERVED_OPTIONS
)
...
...
@@ -80,17 +58,6 @@ def validate_each(record, attribute, value)
end
private
def
tokenize
(
record
,
value
)
tokenizer
=
options
[
:tokenizer
]
if
tokenizer
&&
value
.
kind_of?
(
String
)
if
tokenizer
.
kind_of?
(
Proc
)
tokenizer
.
call
(
value
)
elsif
record
.
respond_to?
(
tokenizer
)
record
.
send
(
tokenizer
,
value
)
end
end
||
value
end
def
skip_nil_check?
(
key
)
key
==
:maximum
&&
options
[
:allow_nil
].
nil?
&&
options
[
:allow_blank
].
nil?
end
...
...
activemodel/test/cases/validations/length_validation_test.rb
浏览文件 @
6a78e0ec
...
...
@@ -318,42 +318,6 @@ def test_validates_length_of_using_is_utf8
assert_equal
[
"is the wrong length (should be 5 characters)"
],
t
.
errors
[
"title"
]
end
def
test_validates_length_of_with_block
assert_deprecated
do
Topic
.
validates_length_of
(
:content
,
minimum:
5
,
too_short:
"Your essay must be at least %{count} words."
,
tokenizer:
lambda
{
|
str
|
str
.
scan
(
/\w+/
)
},
)
end
t
=
Topic
.
new
(
content:
"this content should be long enough"
)
assert
t
.
valid?
t
.
content
=
"not long enough"
assert
t
.
invalid?
assert
t
.
errors
[
:content
].
any?
assert_equal
[
"Your essay must be at least 5 words."
],
t
.
errors
[
:content
]
end
def
test_validates_length_of_with_symbol
assert_deprecated
do
Topic
.
validates_length_of
(
:content
,
minimum:
5
,
too_short:
"Your essay must be at least %{count} words."
,
tokenizer: :my_word_tokenizer
,
)
end
t
=
Topic
.
new
(
content:
"this content should be long enough"
)
assert
t
.
valid?
t
.
content
=
"not long enough"
assert
t
.
invalid?
assert
t
.
errors
[
:content
].
any?
assert_equal
[
"Your essay must be at least 5 words."
],
t
.
errors
[
:content
]
end
def
test_validates_length_of_for_integer
Topic
.
validates_length_of
(
:approved
,
is:
4
)
...
...
activemodel/test/models/topic.rb
浏览文件 @
6a78e0ec
...
...
@@ -36,8 +36,4 @@ def my_validation
def
my_validation_with_arg
(
attr
)
errors
.
add
attr
,
"is missing"
unless
send
(
attr
)
end
def
my_word_tokenizer
(
str
)
str
.
scan
(
/\w+/
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录