Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
65dde287
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,发现更多精彩内容 >>
提交
65dde287
编写于
2月 11, 2013
作者:
C
Carlos Antonio da Silva
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9247 from lest/store-module
define Active Record Store accessors in a module
上级
b1c72a36
9dfef5a4
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
54 addition
and
12 deletion
+54
-12
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+12
-0
activerecord/lib/active_record/store.rb
activerecord/lib/active_record/store.rb
+21
-12
activerecord/test/cases/store_test.rb
activerecord/test/cases/store_test.rb
+12
-0
activerecord/test/models/admin/user.rb
activerecord/test/models/admin/user.rb
+9
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
65dde287
## Rails 4.0.0 (unreleased) ##
*
Allow store accessors to be overrided like other attribute methods, e.g.:
class User < ActiveRecord::Base
store :settings, accessors: [ :color, :homepage ], coder: JSON
def color
super || 'red'
end
end
*Sergey Nartimov*
*
Quote numeric values being compared to non-numeric columns. Otherwise,
in some database, the string column values will be coerced to a numeric
allowing 0, 0.0 or false to match any string starting with a non-digit.
...
...
activerecord/lib/active_record/store.rb
浏览文件 @
65dde287
...
...
@@ -42,21 +42,19 @@ module ActiveRecord
#
# 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.
# the default accessors (using the same name as the attribute) and calling <tt>super</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)
#
super(
decibels.to_i)
# end
#
# def volume_adjustment
#
read_store_attribute(:settings, :volume_adjustment)
.to_i
#
super
.to_i
# end
# end
module
Store
...
...
@@ -75,6 +73,8 @@ def store(store_attribute, options = {})
def
store_accessor
(
store_attribute
,
*
keys
)
keys
=
keys
.
flatten
_store_accessors_module
.
module_eval
do
keys
.
each
do
|
key
|
define_method
(
"
#{
key
}
="
)
do
|
value
|
write_store_attribute
(
store_attribute
,
key
,
value
)
...
...
@@ -84,10 +84,19 @@ def store_accessor(store_attribute, *keys)
read_store_attribute
(
store_attribute
,
key
)
end
end
end
self
.
stored_attributes
[
store_attribute
]
||=
[]
self
.
stored_attributes
[
store_attribute
]
|=
keys
end
def
_store_accessors_module
@_store_accessors_module
||=
begin
mod
=
Module
.
new
include
mod
mod
end
end
end
protected
...
...
activerecord/test/cases/store_test.rb
浏览文件 @
65dde287
...
...
@@ -35,6 +35,12 @@ class StoreTest < ActiveRecord::TestCase
assert_equal
'(123) 456-7890'
,
@john
.
phone_number
end
test
"overriding a read accessor using super"
do
@john
.
settings
[
:color
]
=
nil
assert_equal
'red'
,
@john
.
color
end
test
"updating the store will mark it as changed"
do
@john
.
color
=
'red'
assert
@john
.
settings_changed?
...
...
@@ -66,6 +72,12 @@ class StoreTest < ActiveRecord::TestCase
assert_equal
'1234567890'
,
@john
.
settings
[
:phone_number
]
end
test
"overriding a write accessor using super"
do
@john
.
color
=
'yellow'
assert_equal
'blue'
,
@john
.
color
end
test
"preserve store attributes data in HashWithIndifferentAccess format without any conversion"
do
@john
.
json_data
=
ActiveSupport
::
HashWithIndifferentAccess
.
new
(
:height
=>
'tall'
,
'weight'
=>
'heavy'
)
@john
.
height
=
'low'
...
...
activerecord/test/models/admin/user.rb
浏览文件 @
65dde287
...
...
@@ -27,4 +27,13 @@ def phone_number
def
phone_number
=
(
value
)
write_store_attribute
(
:settings
,
:phone_number
,
value
&&
value
.
gsub
(
/[^\d]/
,
''
))
end
def
color
super
||
'red'
end
def
color
=
(
value
)
value
=
'blue'
unless
%w(black red green blue)
.
include?
(
value
)
super
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录