Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
46873aed
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 搜索 >>
提交
46873aed
编写于
9月 13, 2012
作者:
M
Matt Jones
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor store_accessor
上级
01ef633f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
67 addition
and
9 deletion
+67
-9
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+7
-1
activerecord/lib/active_record/store.rb
activerecord/lib/active_record/store.rb
+38
-6
activerecord/test/cases/store_test.rb
activerecord/test/cases/store_test.rb
+13
-1
activerecord/test/models/admin/user.rb
activerecord/test/models/admin/user.rb
+9
-1
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
46873aed
## Rails 4.0.0 (unreleased) ##
*
You can now override the generated accessor methods for stored attributes
and reuse the original behavior with
`read_store_attribute`
and
`write_store_attribute`
,
which are counterparts to
`read_attribute`
and
`write_attribute`
.
*Matt Jones*
*
Accept belongs_to (including polymorphic) association keys in queries
The following queries are now equivalent:
...
...
@@ -38,7 +44,7 @@
*Jan Bernacki*
*
Fix bug when call
`store_accessor`
multiple times.
*
Fix bug when call
ing
`store_accessor`
multiple times.
Fixes #7532.
*Matt Jones*
...
...
activerecord/lib/active_record/store.rb
浏览文件 @
46873aed
...
...
@@ -37,6 +37,28 @@ module ActiveRecord
# The stored attribute names can be retrieved using +stored_attributes+.
#
# User.stored_attributes[:settings] # [:color, :homepage]
#
# == Overwriting default accessors
#
# All stored values are automatically available through accessors on the Active Record
# object, but sometimes you want to specialize this behavior. This can be done by overwriting
# the default accessors (using the same name as the attribute) and calling
# <tt>read_store_attribute(store_attribute_name, attr_name)</tt> and
# <tt>write_store_attribute(store_attribute_name, attr_name, value)</tt> to actually
# change things.
#
# class Song < ActiveRecord::Base
# # Uses a stored integer to hold the volume adjustment of the song
# store :settings, accessors: [:volume_adjustment]
#
# def volume_adjustment=(decibels)
# write_store_attribute(:settings, :volume_adjustment, decibels.to_i)
# end
#
# def volume_adjustment
# read_store_attribute(:settings, :volume_adjustment).to_i
# end
# end
module
Store
extend
ActiveSupport
::
Concern
...
...
@@ -55,15 +77,11 @@ def store_accessor(store_attribute, *keys)
keys
=
keys
.
flatten
keys
.
each
do
|
key
|
define_method
(
"
#{
key
}
="
)
do
|
value
|
attribute
=
initialize_store_attribute
(
store_attribute
)
if
value
!=
attribute
[
key
]
send
:"
#{
store_attribute
}
_will_change!"
attribute
[
key
]
=
value
end
write_store_attribute
(
store_attribute
,
key
,
value
)
end
define_method
(
key
)
do
initialize_store_attribute
(
store_attribute
)[
key
]
read_store_attribute
(
store_attribute
,
key
)
end
end
...
...
@@ -72,6 +90,20 @@ def store_accessor(store_attribute, *keys)
end
end
protected
def
read_store_attribute
(
store_attribute
,
key
)
attribute
=
initialize_store_attribute
(
store_attribute
)
attribute
[
key
]
end
def
write_store_attribute
(
store_attribute
,
key
,
value
)
attribute
=
initialize_store_attribute
(
store_attribute
)
if
value
!=
attribute
[
key
]
send
:"
#{
store_attribute
}
_will_change!"
attribute
[
key
]
=
value
end
end
private
def
initialize_store_attribute
(
store_attribute
)
attribute
=
send
(
store_attribute
)
...
...
activerecord/test/cases/store_test.rb
浏览文件 @
46873aed
...
...
@@ -29,6 +29,12 @@ class StoreTest < ActiveRecord::TestCase
assert_equal
'graeters'
,
@john
.
reload
.
settings
[
:icecream
]
end
test
"overriding a read accessor"
do
@john
.
settings
[
:phone_number
]
=
'1234567890'
assert_equal
'(123) 456-7890'
,
@john
.
phone_number
end
test
"updating the store will mark it as changed"
do
@john
.
color
=
'red'
assert
@john
.
settings_changed?
...
...
@@ -54,6 +60,12 @@ class StoreTest < ActiveRecord::TestCase
assert_equal
false
,
@john
.
remember_login
end
test
"overriding a write accessor"
do
@john
.
phone_number
=
'(123) 456-7890'
assert_equal
'1234567890'
,
@john
.
settings
[
:phone_number
]
end
test
"preserve store attributes data in HashWithIndifferentAccess format without any conversion"
do
@john
.
json_data
=
HashWithIndifferentAccess
.
new
(
:height
=>
'tall'
,
'weight'
=>
'heavy'
)
@john
.
height
=
'low'
...
...
@@ -124,7 +136,7 @@ class StoreTest < ActiveRecord::TestCase
end
test
"all stored attributes are returned"
do
assert_equal
[
:color
,
:homepage
,
:favorite_food
],
Admin
::
User
.
stored_attributes
[
:settings
]
assert_equal
[
:color
,
:homepage
,
:favorite_food
,
:phone_number
],
Admin
::
User
.
stored_attributes
[
:settings
]
end
test
"stores_attributes are class level settings"
do
...
...
activerecord/test/models/admin/user.rb
浏览文件 @
46873aed
class
Admin::User
<
ActiveRecord
::
Base
belongs_to
:account
store
:settings
,
:accessors
=>
[
:color
,
:homepage
]
store_accessor
:settings
,
:favorite_food
store_accessor
:settings
,
:favorite_food
,
:phone_number
store
:preferences
,
:accessors
=>
[
:remember_login
]
store
:json_data
,
:accessors
=>
[
:height
,
:weight
],
:coder
=>
JSON
store
:json_data_empty
,
:accessors
=>
[
:is_a_good_guy
],
:coder
=>
JSON
def
phone_number
read_store_attribute
(
:settings
,
:phone_number
).
gsub
(
/(\d{3})(\d{3})(\d{4})/
,
'(\1) \2-\3'
)
end
def
phone_number
=
(
value
)
write_store_attribute
(
:settings
,
:phone_number
,
value
&&
value
.
gsub
(
/[^\d]/
,
''
))
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录