Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ff6d4987
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 搜索 >>
未验证
提交
ff6d4987
编写于
3月 24, 2018
作者:
A
Andrew White
提交者:
GitHub
3月 24, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #32306 from danhuynhdev/feature/store-accessor-prefix
Add custom prefix to ActiveRecord::Store accessors
上级
6aa5cf03
3f297be7
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
50 addition
and
6 deletion
+50
-6
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+4
-0
activerecord/lib/active_record/store.rb
activerecord/lib/active_record/store.rb
+20
-5
activerecord/test/cases/store_test.rb
activerecord/test/cases/store_test.rb
+21
-1
activerecord/test/models/admin/user.rb
activerecord/test/models/admin/user.rb
+3
-0
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+2
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
ff6d4987
*
Add custom prefix option to ActiveRecord::Store.store_accessor.
*Tan Huynh*
*
Rails 6 requires Ruby 2.4.1 or newer.
*Jeremy Daer*
...
...
activerecord/lib/active_record/store.rb
浏览文件 @
ff6d4987
...
...
@@ -31,10 +31,14 @@ module ActiveRecord
#
# class User < ActiveRecord::Base
# store :settings, accessors: [ :color, :homepage ], coder: JSON
# store :parent, accessors: [ :name ], coder: JSON, prefix: true
# store :spouse, accessors: [ :name ], coder: JSON, prefix: :partner
# end
#
# u = User.new(color: 'black', homepage: '37signals.com')
# u = User.new(color: 'black', homepage: '37signals.com'
, parent_name: 'Mary', partner_name: 'Lily'
)
# u.color # Accessor stored attribute
# u.parent_name # Accessor stored attribute with prefix
# u.partner_name # Accessor stored attribute with custom prefix
# u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
#
# # There is no difference between strings and symbols for accessing custom attributes
...
...
@@ -44,6 +48,7 @@ module ActiveRecord
# # Add additional accessors to an existing store through store_accessor
# class SuperUser < User
# store_accessor :settings, :privileges, :servants
# store_accessor :parent, :birthday, prefix: true
# end
#
# The stored attribute names can be retrieved using {.stored_attributes}[rdoc-ref:rdoc-ref:ClassMethods#stored_attributes].
...
...
@@ -81,19 +86,29 @@ class << self
module
ClassMethods
def
store
(
store_attribute
,
options
=
{})
serialize
store_attribute
,
IndifferentCoder
.
new
(
store_attribute
,
options
[
:coder
])
store_accessor
(
store_attribute
,
options
[
:accessors
])
if
options
.
has_key?
:accessors
store_accessor
(
store_attribute
,
options
[
:accessors
]
,
prefix:
options
[
:prefix
]
)
if
options
.
has_key?
:accessors
end
def
store_accessor
(
store_attribute
,
*
keys
)
def
store_accessor
(
store_attribute
,
*
keys
,
prefix:
nil
)
keys
=
keys
.
flatten
accessor_prefix
=
case
prefix
when
String
,
Symbol
"
#{
prefix
}
_"
when
TrueClass
"
#{
store_attribute
}
_"
else
""
end
_store_accessors_module
.
module_eval
do
keys
.
each
do
|
key
|
define_method
(
"
#{
key
}
="
)
do
|
value
|
define_method
(
"
#{
accessor_prefix
}#{
key
}
="
)
do
|
value
|
write_store_attribute
(
store_attribute
,
key
,
value
)
end
define_method
(
key
)
do
define_method
(
"
#{
accessor_prefix
}#{
key
}
"
)
do
read_store_attribute
(
store_attribute
,
key
)
end
end
...
...
activerecord/test/cases/store_test.rb
浏览文件 @
ff6d4987
...
...
@@ -8,7 +8,12 @@ class StoreTest < ActiveRecord::TestCase
fixtures
:'admin/users'
setup
do
@john
=
Admin
::
User
.
create!
(
name:
"John Doe"
,
color:
"black"
,
remember_login:
true
,
height:
"tall"
,
is_a_good_guy:
true
)
@john
=
Admin
::
User
.
create!
(
name:
"John Doe"
,
color:
"black"
,
remember_login:
true
,
height:
"tall"
,
is_a_good_guy:
true
,
parent_name:
"Quinn"
,
partner_name:
"Dallas"
,
partner_birthday:
"1997-11-1"
)
end
test
"reading store attributes through accessors"
do
...
...
@@ -24,6 +29,21 @@ class StoreTest < ActiveRecord::TestCase
assert_equal
"37signals.com"
,
@john
.
homepage
end
test
"reading store attributes through accessors with prefix"
do
assert_equal
"Quinn"
,
@john
.
parent_name
assert_nil
@john
.
parent_birthday
assert_equal
"Dallas"
,
@john
.
partner_name
assert_equal
"1997-11-1"
,
@john
.
partner_birthday
end
test
"writing store attributes through accessors with prefix"
do
@john
.
partner_name
=
"River"
@john
.
partner_birthday
=
"1999-2-11"
assert_equal
"River"
,
@john
.
partner_name
assert_equal
"1999-2-11"
,
@john
.
partner_birthday
end
test
"accessing attributes not exposed by accessors"
do
@john
.
settings
[
:icecream
]
=
"graeters"
@john
.
save
...
...
activerecord/test/models/admin/user.rb
浏览文件 @
ff6d4987
...
...
@@ -19,6 +19,9 @@ def load(s)
store
:params
,
accessors:
[
:token
],
coder:
YAML
store
:settings
,
accessors:
[
:color
,
:homepage
]
store_accessor
:settings
,
:favorite_food
store
:parent
,
accessors:
[
:birthday
,
:name
],
prefix:
true
store
:spouse
,
accessors:
[
:birthday
],
prefix: :partner
store_accessor
:spouse
,
:name
,
prefix: :partner
store
:preferences
,
accessors:
[
:remember_login
]
store
:json_data
,
accessors:
[
:height
,
:weight
],
coder:
Coder
.
new
store
:json_data_empty
,
accessors:
[
:is_a_good_guy
],
coder:
Coder
.
new
...
...
activerecord/test/schema/schema.rb
浏览文件 @
ff6d4987
...
...
@@ -21,6 +21,8 @@
create_table
:admin_users
,
force:
true
do
|
t
|
t
.
string
:name
t
.
string
:settings
,
null:
true
,
limit:
1024
t
.
string
:parent
,
null:
true
,
limit:
1024
t
.
string
:spouse
,
null:
true
,
limit:
1024
# MySQL does not allow default values for blobs. Fake it out with a
# big varchar below.
t
.
string
:preferences
,
null:
true
,
default:
""
,
limit:
1024
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录