Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
4d4bdb07
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 搜索 >>
提交
4d4bdb07
编写于
1月 17, 2010
作者:
M
Mikel Lindsaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added ActiveModel::Errors documentation
上级
6f663add
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
139 addition
and
0 deletion
+139
-0
activemodel/README
activemodel/README
+32
-0
activemodel/lib/active_model/errors.rb
activemodel/lib/active_model/errors.rb
+107
-0
未找到文件。
activemodel/README
浏览文件 @
4d4bdb07
...
...
@@ -78,3 +78,35 @@ functionality from the following modules:
{Learn more}[link:classes/ActiveModel/Dirty.html]
* Adding +errors+ support to your object
Provides the error messages to allow your object to interact with Action Pack
helpers seamlessly...
class Person
def initialize
@errors = ActiveModel::Errors.new(self)
end
attr_accessor :name
attr_reader :errors
def validate!
errors.add(:name, "can not be nil") if name == nil
end
def ErrorsPerson.human_attribute_name(attr, options = {})
"Name"
end
end
... gives you...
person.errors.full_messages
# => ["Name Can not be nil"]
person.errors.full_messages
# => ["Name Can not be nil"]
{Learn more}[link:classes/ActiveModel/Errors.html]
activemodel/lib/active_model/errors.rb
浏览文件 @
4d4bdb07
...
...
@@ -2,9 +2,66 @@
require
'active_support/ordered_hash'
module
ActiveModel
# Provides a modified +OrderedHash+ that you can include in your object
# for handling error messages and interacting with Action Pack helpers.
#
# A minimal implementation could be:
#
# class Person
#
# def initialize
# @errors = ActiveModel::Errors.new(self)
# end
#
# attr_accessor :name
# attr_reader :errors
#
# def validate!
# errors.add(:name, "can not be nil") if name == nil
# end
#
# # The following methods are needed to be minimally implemented
#
# def read_attribute_for_validation(attr)
# send(attr)
# end
#
# def ErrorsPerson.human_attribute_name(attr, options = {})
# attr
# end
#
# def ErrorsPerson.lookup_ancestors
# [self]
# end
#
# def ErrorsPerson.model_name
# ActiveModel::Name.new(ErrorsPerson)
# end
#
# end
#
# The last four methods are required in your object for Errors to be
# able to generate error messages correctly and also handle multiple
# languages.
#
# The above allows you to do:
#
# p = Person.new
# p.validate! # => ["can not be nil"]
# p.errors.full_messages # => ["name can not be nil"]
# # etc..
class
Errors
<
ActiveSupport
::
OrderedHash
include
DeprecatedErrorMethods
# Pass in the instance of the object that is using the errors object.
#
# class Person
# def initialize
# @errors = ActiveModel::Errors.new(self)
# end
# end
#
#
def
initialize
(
base
)
@base
=
base
super
()
...
...
@@ -13,6 +70,10 @@ def initialize(base)
alias_method
:get
,
:[]
alias_method
:set
,
:[]=
# When passed a symbol or a name of a method, returns an array of errors for the method.
#
# p.errors[:name] #=> ["can not be nil"]
# p.errors['name'] #=> ["can not be nil"]
def
[]
(
attribute
)
if
errors
=
get
(
attribute
.
to_sym
)
errors
...
...
@@ -21,28 +82,73 @@ def [](attribute)
end
end
# Adds to the supplied attribute the supplied error message.
#
# p.errors[:name] = "must be set"
# p.errors[:name] #=> ['must be set']
def
[]=
(
attribute
,
error
)
self
[
attribute
.
to_sym
]
<<
error
end
# Iterates through each error key, value pair in the error messages hash.
# Yields the attribute and the error for that attribute. If the attribute
# has more than one error message, yields once for each error message.
#
# p.errors.add(:name, "can't be blank")
# p.errors.each do |attribute, errors_array|
# # Will yield :name and "can't be blank"
# end
#
# p.errors.add(:name, "must be specified")
# p.errors.each do |attribute, errors_array|
# # Will yield :name and "can't be blank"
# # then yield :name and "must be specified"
# end
def
each
each_key
do
|
attribute
|
self
[
attribute
].
each
{
|
error
|
yield
attribute
,
error
}
end
end
# Returns the number of error messages.
#
# p.errors.add(:name, "can't be blank")
# p.errors.size #=> 1
# p.errors.add(:name, "must be specified")
# p.errors.size #=> 2
def
size
values
.
flatten
.
size
end
# Returns an array of error messages, with the attribute name included
#
# p.errors.add(:name, "can't be blank")
# p.errors.add(:name, "must be specified")
# p.errors.to_a #=> ["name can't be blank", "name must be specified"]
def
to_a
full_messages
end
# Returns the number of error messages.
# p.errors.add(:name, "can't be blank")
# p.errors.count #=> 1
# p.errors.add(:name, "must be specified")
# p.errors.count #=> 2
def
count
to_a
.
size
end
# Returns an xml formatted representation of the Errors hash.
#
# p.errors.add(:name, "can't be blank")
# p.errors.add(:name, "must be specified")
# p.errors.to_xml #=> Produces:
#
# # <?xml version=\"1.0\" encoding=\"UTF-8\"?>
# # <errors>
# # <error>name can't be blank</error>
# # <error>name must be specified</error>
# # </errors>
def
to_xml
(
options
=
{})
require
'builder'
unless
defined?
::
Builder
options
[
:root
]
||=
"errors"
...
...
@@ -59,6 +165,7 @@ def to_xml(options={})
# for the same attribute and ensure that this error object returns false when asked if <tt>empty?</tt>. More than one
# error can be added to the same +attribute+ in which case an array will be returned on a call to <tt>on(attribute)</tt>.
# If no +messsage+ is supplied, :invalid is assumed.
#
# If +message+ is a Symbol, it will be translated, using the appropriate scope (see translate_error).
def
add
(
attribute
,
message
=
nil
,
options
=
{})
message
||=
:invalid
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录