Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b32ba367
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,发现更多精彩内容 >>
提交
b32ba367
编写于
11月 05, 2013
作者:
D
Damien Mathieu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add #no_touching on ActiveRecord models
上级
bba8bb8b
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
112 addition
and
0 deletion
+112
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+10
-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
-0
activerecord/lib/active_record/no_touching.rb
activerecord/lib/active_record/no_touching.rb
+52
-0
activerecord/test/cases/timestamp_test.rb
activerecord/test/cases/timestamp_test.rb
+48
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
b32ba367
*
Added
`ActiveRecord::Base.no_touching`
, which allows ignoring touch on models.
Examples:
Post.no_touching do
Post.first.touch
end
*Sam Stephenson*, *Damien Mathieu*
*
Prevent the counter cache from being decremented twice when destroying
a record on a has_many :through association.
...
...
activerecord/lib/active_record.rb
浏览文件 @
b32ba367
...
...
@@ -45,6 +45,7 @@ module ActiveRecord
autoload
:Migrator
,
'active_record/migration'
autoload
:ModelSchema
autoload
:NestedAttributes
autoload
:NoTouching
autoload
:Persistence
autoload
:QueryCache
autoload
:Querying
...
...
activerecord/lib/active_record/base.rb
浏览文件 @
b32ba367
...
...
@@ -295,6 +295,7 @@ class Base
extend
Delegation
::
DelegateCache
include
Persistence
include
NoTouching
include
ReadonlyAttributes
include
ModelSchema
include
Inheritance
...
...
activerecord/lib/active_record/no_touching.rb
0 → 100644
浏览文件 @
b32ba367
module
ActiveRecord
# = Active Record No Touching
module
NoTouching
extend
ActiveSupport
::
Concern
module
ClassMethods
# Lets you selectively disable calls to `touch` for the
# duration of a block.
#
# ==== Examples
# ActiveRecord::Base.no_touching do
# Project.first.touch # does nothing
# Message.first.touch # does nothing
# end
#
# Project.no_touching do
# Project.first.touch # does nothing
# Message.first.touch # works, but does not touch the associated project
# end
#
def
no_touching
(
&
block
)
NoTouching
.
apply_to
(
self
,
&
block
)
end
end
class
<<
self
def
apply_to
(
klass
)
#:nodoc:
klasses
.
push
(
klass
)
yield
ensure
klasses
.
pop
end
def
applied_to?
(
klass
)
#:nodoc:
klasses
.
any?
{
|
k
|
k
>=
klass
}
end
private
def
klasses
Thread
.
current
[
:no_touching_classes
]
||=
[]
end
end
def
no_touching?
NoTouching
.
applied_to?
(
self
.
class
)
end
def
touch
(
*
)
super
unless
no_touching?
end
end
end
activerecord/test/cases/timestamp_test.rb
浏览文件 @
b32ba367
...
...
@@ -11,6 +11,7 @@ class TimestampTest < ActiveRecord::TestCase
def
setup
@developer
=
Developer
.
first
@owner
=
Owner
.
first
@developer
.
update_columns
(
updated_at:
Time
.
now
.
prev_month
)
@previously_updated_at
=
@developer
.
updated_at
end
...
...
@@ -92,6 +93,53 @@ def test_touching_a_record_without_timestamps_is_unexceptional
assert_nothing_raised
{
Car
.
first
.
touch
}
end
def
test_touching_a_no_touching_object
Developer
.
no_touching
do
assert
@developer
.
no_touching?
assert
!
@owner
.
no_touching?
@developer
.
touch
end
assert
!
@developer
.
no_touching?
assert
!
@owner
.
no_touching?
assert_equal
@previously_updated_at
,
@developer
.
updated_at
end
def
test_touching_related_objects
@owner
=
Owner
.
first
@previously_updated_at
=
@owner
.
updated_at
Owner
.
no_touching
do
@owner
.
pets
.
first
.
touch
end
assert_equal
@previously_updated_at
,
@owner
.
updated_at
end
def
test_global_no_touching
ActiveRecord
::
Base
.
no_touching
do
assert
@developer
.
no_touching?
assert
@owner
.
no_touching?
@developer
.
touch
end
assert
!
@developer
.
no_touching?
assert
!
@owner
.
no_touching?
assert_equal
@previously_updated_at
,
@developer
.
updated_at
end
def
test_no_touching_threadsafe
Thread
.
new
do
Developer
.
no_touching
do
assert
@developer
.
no_touching?
sleep
(
1
)
end
end
assert
!
@developer
.
no_touching?
end
def
test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
pet
=
Pet
.
first
owner
=
pet
.
owner
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录