Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
85b64f98
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,发现更多精彩内容 >>
提交
85b64f98
编写于
10月 13, 2011
作者:
D
David Heinemeier Hansson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added ActiveRecord::Base.store for declaring simple single-column key/value stores [DHH]
上级
8f11d535
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
93 addition
and
1 deletion
+93
-1
activerecord/CHANGELOG
activerecord/CHANGELOG
+11
-0
activerecord/lib/active_record.rb
activerecord/lib/active_record.rb
+1
-0
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+1
-1
activerecord/lib/active_record/store.rb
activerecord/lib/active_record/store.rb
+49
-0
activerecord/test/cases/store_test.rb
activerecord/test/cases/store_test.rb
+29
-0
activerecord/test/models/admin/user.rb
activerecord/test/models/admin/user.rb
+1
-0
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+1
-0
未找到文件。
activerecord/CHANGELOG
浏览文件 @
85b64f98
*Rails 3.2.0 (unreleased)*
* Added ActiveRecord::Base.store for declaring simple single-column key/value stores [DHH]
class User < ActiveRecord::Base
store :settings, accessors: [ :color, :homepage ]
end
u = User.new(color: 'black', homepage: '37signals.com')
u.color # Accessor stored attribute
u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
* MySQL: case-insensitive uniqueness validation avoids calling LOWER when
the column already uses a case-insensitive collation. Fixes #561.
...
...
activerecord/lib/active_record.rb
浏览文件 @
85b64f98
...
...
@@ -69,6 +69,7 @@ module ActiveRecord
autoload
:Schema
autoload
:SchemaDumper
autoload
:Serialization
autoload
:Store
autoload
:SessionStore
autoload
:Timestamp
autoload
:Transactions
...
...
activerecord/lib/active_record/base.rb
浏览文件 @
85b64f98
...
...
@@ -2145,7 +2145,7 @@ def populate_with_current_scope_attributes
# AutosaveAssociation needs to be included before Transactions, because we want
# #save_with_autosave_associations to be wrapped inside a transaction.
include
AutosaveAssociation
,
NestedAttributes
include
Aggregations
,
Transactions
,
Reflection
,
Serialization
include
Aggregations
,
Transactions
,
Reflection
,
Serialization
,
Store
NilClass
.
add_whiner
(
self
)
if
NilClass
.
respond_to?
(
:add_whiner
)
...
...
activerecord/lib/active_record/store.rb
0 → 100644
浏览文件 @
85b64f98
module
ActiveRecord
# Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
# It's like a simple key/value store backed into your record when you don't care about being able to
# query that store outside the context of a single record.
#
# You can then declare accessors to this store that are then accessible just like any other attribute
# of the model. This is very helpful for easily exposing store keys to a form or elsewhere that's
# already built around just accessing attributes on the model.
#
# Make sure that you declare the database column used for the serialized store as a text, so there's
# plenty of room.
#
# Examples:
#
# class User < ActiveRecord::Base
# store :settings, accessors: [ :color, :homepage ]
# end
#
# u = User.new(color: 'black', homepage: '37signals.com')
# u.color # Accessor stored attribute
# u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
#
# # Add additional accessors to an existing store through store_accessor
# class SuperUser < User
# store_accessor :settings, :privileges, :servants
# end
module
Store
extend
ActiveSupport
::
Concern
module
ClassMethods
def
store
(
store_attribute
,
options
=
{})
serialize
store_attribute
,
Hash
store_accessor
(
store_attribute
,
options
[
:accessors
])
if
options
.
has_key?
:accessors
end
def
store_accessor
(
store_attribute
,
*
keys
)
Array
(
keys
).
flatten
.
each
do
|
key
|
define_method
(
"
#{
key
}
="
)
do
|
value
|
send
(
store_attribute
)[
key
]
=
value
end
define_method
(
key
)
do
send
(
store_attribute
)[
key
]
end
end
end
end
end
end
\ No newline at end of file
activerecord/test/cases/store_test.rb
0 → 100644
浏览文件 @
85b64f98
require
'cases/helper'
require
'models/admin'
require
'models/admin/user'
class
StoreTest
<
ActiveRecord
::
TestCase
setup
do
@john
=
Admin
::
User
.
create
(
name:
'John Doe'
,
color:
'black'
)
end
test
"reading store attributes through accessors"
do
assert_equal
'black'
,
@john
.
color
assert_nil
@john
.
homepage
end
test
"writing store attributes through accessors"
do
@john
.
color
=
'red'
@john
.
homepage
=
'37signals.com'
assert_equal
'red'
,
@john
.
color
assert_equal
'37signals.com'
,
@john
.
homepage
end
test
"accessing attributes not exposed by accessors"
do
@john
.
settings
[
:icecream
]
=
'graeters'
@john
.
save
assert
'graeters'
,
@john
.
reload
.
settings
[
:icecream
]
end
end
activerecord/test/models/admin/user.rb
浏览文件 @
85b64f98
class
Admin::User
<
ActiveRecord
::
Base
belongs_to
:account
store
:settings
,
accessors:
[
:color
,
:homepage
]
end
\ No newline at end of file
activerecord/test/schema/schema.rb
浏览文件 @
85b64f98
...
...
@@ -37,6 +37,7 @@ def create_table(*args, &block)
create_table
:admin_users
,
:force
=>
true
do
|
t
|
t
.
string
:name
t
.
text
:settings
t
.
references
:account
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录