Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
87323487
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,发现更多精彩内容 >>
提交
87323487
编写于
12月 26, 2013
作者:
G
Godfrey Chan
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #13442 from aditya-kapoor/update-docs
Updated documentation for Attribute Methods [ci skip]
上级
c99d9691
1dae89cc
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
11 addition
and
16 deletion
+11
-16
activemodel/lib/active_model/attribute_methods.rb
activemodel/lib/active_model/attribute_methods.rb
+11
-16
未找到文件。
activemodel/lib/active_model/attribute_methods.rb
浏览文件 @
87323487
...
...
@@ -14,11 +14,11 @@ module ActiveModel
class
MissingAttributeError
<
NoMethodError
end
# == Active
Model Attribute
Methods
# == Active
\Model \Attribute \
Methods
#
#
<tt>ActiveModel::AttributeMethods</tt> provides a way to add prefixes and
#
suffixes to your methods as well as handling the creation of
#
<tt>ActiveRecord::Base</tt>-like
class methods such as +table_name+.
#
Provides a way to add prefixes and suffixes to your methods as
#
well as handling the creation of <tt>ActiveRecord::Base</tt>-like
# class methods such as +table_name+.
#
# The requirements to implement <tt>ActiveModel::AttributeMethods</tt> are to:
#
...
...
@@ -27,7 +27,9 @@ class MissingAttributeError < NoMethodError
# or +attribute_method_prefix+.
# * Call +define_attribute_methods+ after the other methods are called.
# * Define the various generic +_attribute+ methods that you have declared.
# * Define an +attributes+ method, see below.
# * Define an +attributes+ method which returns a hash with each
# attribute name in your model as hash key and the attribute value as hash value.
# Hash keys must be strings.
#
# A minimal implementation could be:
#
...
...
@@ -42,7 +44,7 @@ class MissingAttributeError < NoMethodError
# attr_accessor :name
#
# def attributes
# {
'name' => @name
}
# {
'name' => @name
}
# end
#
# private
...
...
@@ -59,13 +61,6 @@ class MissingAttributeError < NoMethodError
# send("#{attr}=", 'Default Name')
# end
# end
#
# Note that whenever you include <tt>ActiveModel::AttributeMethods</tt> in
# your class, it requires you to implement an +attributes+ method which
# returns a hash with each attribute name in your model as hash key and the
# attribute value as hash value.
#
# Hash keys must be strings.
module
AttributeMethods
extend
ActiveSupport
::
Concern
...
...
@@ -173,14 +168,14 @@ def attribute_method_suffix(*suffixes)
# private
#
# def reset_attribute_to_default!(attr)
#
...
#
send("#{attr}=", 'Default Name')
# end
# end
#
# person = Person.new
# person.name # => 'Gem'
# person.reset_name_to_default!
# person.name # => '
Gemma
'
# person.name # => '
Default Name
'
def
attribute_method_affix
(
*
affixes
)
self
.
attribute_method_matchers
+=
affixes
.
map!
{
|
affix
|
AttributeMethodMatcher
.
new
prefix:
affix
[
:prefix
],
suffix:
affix
[
:suffix
]
}
undefine_attribute_methods
...
...
@@ -250,7 +245,7 @@ def attribute_alias(name)
# private
#
# def clear_attribute(attr)
#
...
#
send("#{attr}", nil)
# end
# end
def
define_attribute_methods
(
*
attr_names
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录