Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e4826960
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,发现更多精彩内容 >>
提交
e4826960
编写于
4月 15, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14765 from byroot/refactor-counter-cache-create-and-destroy
Refactor counter cache create and destroy
上级
3870ce06
7af987cf
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
44 addition
and
34 deletion
+44
-34
activerecord/lib/active_record/associations/belongs_to_association.rb
.../lib/active_record/associations/belongs_to_association.rb
+14
-4
activerecord/lib/active_record/associations/builder/belongs_to.rb
...cord/lib/active_record/associations/builder/belongs_to.rb
+1
-29
activerecord/lib/active_record/counter_cache.rb
activerecord/lib/active_record/counter_cache.rb
+29
-1
未找到文件。
activerecord/lib/active_record/associations/belongs_to_association.rb
浏览文件 @
e4826960
...
...
@@ -31,6 +31,14 @@ def updated?
@updated
end
def
decrement_counters
# :nodoc:
with_cache_name
{
|
name
|
decrement_counter
name
}
end
def
increment_counters
# :nodoc:
with_cache_name
{
|
name
|
increment_counter
name
}
end
private
def
find_target?
...
...
@@ -51,13 +59,15 @@ def update_counters(record)
end
end
def
decrement_counters
with_cache_name
{
|
name
|
decrement_counter
name
}
def
decrement_counter
(
counter_cache_name
)
if
foreign_key_present?
klass
.
decrement_counter
(
counter_cache_name
,
target_id
)
end
end
def
decrement_counter
counter_cache_name
def
increment_counter
(
counter_cache_name
)
if
foreign_key_present?
klass
.
de
crement_counter
(
counter_cache_name
,
target_id
)
klass
.
in
crement_counter
(
counter_cache_name
,
target_id
)
end
end
...
...
activerecord/lib/active_record/associations/builder/belongs_to.rb
浏览文件 @
e4826960
...
...
@@ -26,29 +26,9 @@ def self.define_accessors(mixin, reflection)
private
def
self
.
add_counter_cache_methods
(
mixin
)
return
if
mixin
.
method_defined?
:belongs_to_counter_cache_after_
cre
ate
return
if
mixin
.
method_defined?
:belongs_to_counter_cache_after_
upd
ate
mixin
.
class_eval
do
def
belongs_to_counter_cache_after_create
(
reflection
)
if
record
=
send
(
reflection
.
name
)
cache_column
=
reflection
.
counter_cache_column
record
.
class
.
increment_counter
(
cache_column
,
record
.
id
)
@_after_create_counter_called
=
true
end
end
def
belongs_to_counter_cache_after_destroy
(
reflection
)
foreign_key
=
reflection
.
foreign_key
.
to_sym
unless
destroyed_by_association
&&
destroyed_by_association
.
foreign_key
.
to_sym
==
foreign_key
record
=
send
reflection
.
name
if
record
&&
self
.
actually_destroyed?
cache_column
=
reflection
.
counter_cache_column
record
.
class
.
decrement_counter
(
cache_column
,
record
.
id
)
self
.
clear_destroy_state
end
end
end
def
belongs_to_counter_cache_after_update
(
reflection
)
foreign_key
=
reflection
.
foreign_key
cache_column
=
reflection
.
counter_cache_column
...
...
@@ -74,14 +54,6 @@ def belongs_to_counter_cache_after_update(reflection)
def
self
.
add_counter_cache_callbacks
(
model
,
reflection
)
cache_column
=
reflection
.
counter_cache_column
model
.
after_create
lambda
{
|
record
|
record
.
belongs_to_counter_cache_after_create
(
reflection
)
}
model
.
after_destroy
lambda
{
|
record
|
record
.
belongs_to_counter_cache_after_destroy
(
reflection
)
}
model
.
after_update
lambda
{
|
record
|
record
.
belongs_to_counter_cache_after_update
(
reflection
)
}
...
...
activerecord/lib/active_record/counter_cache.rb
浏览文件 @
e4826960
...
...
@@ -131,13 +131,41 @@ def clear_destroy_state
private
def
_create_record
(
*
)
id
=
super
each_counter_cached_associations
do
|
association
|
if
send
(
association
.
reflection
.
name
)
association
.
increment_counters
@_after_create_counter_called
=
true
end
end
id
end
def
destroy_row
affected_rows
=
super
@_actually_destroyed
=
affected_rows
>
0
if
affected_rows
>
0
each_counter_cached_associations
do
|
association
|
foreign_key
=
association
.
reflection
.
foreign_key
.
to_sym
unless
destroyed_by_association
&&
destroyed_by_association
.
foreign_key
.
to_sym
==
foreign_key
if
send
(
association
.
reflection
.
name
)
association
.
decrement_counters
end
end
end
end
affected_rows
end
def
each_counter_cached_associations
reflections
.
each
do
|
name
,
reflection
|
yield
association
(
name
)
if
reflection
.
belongs_to?
&&
reflection
.
counter_cache_column
end
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录