Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1727f433
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,发现更多精彩内容 >>
提交
1727f433
编写于
6月 22, 2012
作者:
F
Francesco Rodriguez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add documentation and examples to ActiveModel::Errors [ci skip]
上级
bc818e4b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
26 addition
and
4 deletion
+26
-4
activemodel/lib/active_model/errors.rb
activemodel/lib/active_model/errors.rb
+26
-4
未找到文件。
activemodel/lib/active_model/errors.rb
浏览文件 @
1727f433
...
...
@@ -99,6 +99,7 @@ def clear
def
include?
(
error
)
(
v
=
messages
[
error
])
&&
v
.
any?
end
# aliases include?
alias
:has_key?
:include?
# Get messages for +key+.
...
...
@@ -218,6 +219,7 @@ def count
def
empty?
all?
{
|
k
,
v
|
v
&&
v
.
empty?
&&
!
v
.
is_a?
(
String
)
}
end
# aliases empty?
alias_method
:blank?
,
:empty?
# Returns an xml formatted representation of the Errors hash.
...
...
@@ -235,10 +237,9 @@ def to_xml(options={})
to_a
.
to_xml
({
:root
=>
"errors"
,
:skip_types
=>
true
}.
merge!
(
options
))
end
# Returns an Hash that can be used as the JSON representation for this
# object. Also, You can pass the <tt>:full_messages</tt> option. This
# determines if the json object should contain full messages or not (false
# by default).
# Returns a Hash that can be used as the JSON representation for this
# object. You can pass the <tt>:full_messages</tt> option. This determines
# if the json object should contain full messages or not (false by default).
#
# person.as_json # => { :name => ["can not be nil"] }
# person.as_json(full_messages: true) # => { :name => ["name can not be nil"] }
...
...
@@ -246,6 +247,11 @@ def as_json(options=nil)
to_hash
(
options
&&
options
[
:full_messages
])
end
# Returns a Hash of attributes with their error messages. If +full_messages+
# is +true+, it will contain full messages (see +full_message+).
#
# person.to_hash # => { :name => ["can not be nil"] }
# person.to_hash(true) # => { :name => ["name can not be nil"] }
def
to_hash
(
full_messages
=
false
)
if
full_messages
messages
=
{}
...
...
@@ -262,6 +268,14 @@ def to_hash(full_messages = false)
# can be added to the same +attribute+. If no +message+ is supplied,
# <tt>:invalid</tt> is assumed.
#
# person.errors.add(:name)
# # => ["is invalid"]
# person.errors.add(:name, 'must be implemented')
# # => ["is invalid", "must be implemented"]
#
# person.errors.messages
# # => { :name => ["must be implemented", "is invalid"] }
#
# If +message+ is a symbol, it will be translated using the appropriate
# scope (see +generate_message+).
#
...
...
@@ -278,6 +292,10 @@ def add(attribute, message = nil, options = {})
# Will add an error message to each of the attributes in +attributes+
# that is empty.
#
# person.errors.add_on_empty(:name)
# person.errors.messages
# # => { :name => ["can't be empty"] }
def
add_on_empty
(
attributes
,
options
=
{})
[
attributes
].
flatten
.
each
do
|
attribute
|
value
=
@base
.
send
(
:read_attribute_for_validation
,
attribute
)
...
...
@@ -288,6 +306,10 @@ def add_on_empty(attributes, options = {})
# Will add an error message to each of the attributes in +attributes+ that
# is blank (using Object#blank?).
#
# person.errors.add_on_blank(:name)
# person.errors.messages
# # => { :name => ["can't be blank"] }
def
add_on_blank
(
attributes
,
options
=
{})
[
attributes
].
flatten
.
each
do
|
attribute
|
value
=
@base
.
send
(
:read_attribute_for_validation
,
attribute
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录