Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6bb8afb4
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,发现更多精彩内容 >>
提交
6bb8afb4
编写于
8月 10, 2012
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
DRY up handling of dependent option
上级
825c05d4
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
22 addition
and
63 deletion
+22
-63
activerecord/lib/active_record/associations/builder/association.rb
...ord/lib/active_record/associations/builder/association.rb
+15
-3
activerecord/lib/active_record/associations/builder/belongs_to.rb
...cord/lib/active_record/associations/builder/belongs_to.rb
+2
-18
activerecord/lib/active_record/associations/builder/collection_association.rb
...ive_record/associations/builder/collection_association.rb
+0
-1
activerecord/lib/active_record/associations/builder/has_many.rb
...record/lib/active_record/associations/builder/has_many.rb
+2
-22
activerecord/lib/active_record/associations/builder/has_one.rb
...erecord/lib/active_record/associations/builder/has_one.rb
+3
-19
未找到文件。
activerecord/lib/active_record/associations/builder/association.rb
浏览文件 @
6bb8afb4
...
...
@@ -39,6 +39,7 @@ def mixin
def
build
validate_options
define_accessors
configure_dependency
if
options
[
:dependent
]
@reflection
=
model
.
create_reflection
(
macro
,
name
,
scope
,
options
,
model
)
super
# provides an extension point
@reflection
...
...
@@ -75,9 +76,9 @@ def define_writers
end
end
def
validate_dependent_option
(
valid_options
)
unless
valid_options
.
include?
options
[
:dependent
]
raise
ArgumentError
,
"The :dependent option must be one of
#{
valid_options
}
, but is :
#{
options
[
:dependent
]
}
"
def
configure_dependency
unless
valid_
dependent_
options
.
include?
options
[
:dependent
]
raise
ArgumentError
,
"The :dependent option must be one of
#{
valid_
dependent_
options
}
, but is :
#{
options
[
:dependent
]
}
"
end
if
options
[
:dependent
]
==
:restrict
...
...
@@ -86,6 +87,17 @@ def validate_dependent_option(valid_options)
"provides the same functionality."
)
end
name
=
self
.
name
mixin
.
redefine_method
(
"
#{
macro
}
_dependent_for_
#{
name
}
"
)
do
association
(
name
).
handle_dependency
end
model
.
before_destroy
"
#{
macro
}
_dependent_for_
#{
name
}
"
end
def
valid_dependent_options
raise
NotImplementedError
end
end
end
activerecord/lib/active_record/associations/builder/belongs_to.rb
浏览文件 @
6bb8afb4
module
ActiveRecord::Associations::Builder
class
BelongsTo
<
SingularAssociation
#:nodoc:
def
macro
...
...
@@ -17,7 +16,6 @@ def build
reflection
=
super
add_counter_cache_callbacks
(
reflection
)
if
options
[
:counter_cache
]
add_touch_callbacks
(
reflection
)
if
options
[
:touch
]
configure_dependency
reflection
end
...
...
@@ -68,22 +66,8 @@ def add_touch_callbacks(reflection)
model
.
after_destroy
(
method_name
)
end
def
configure_dependency
if
dependent
=
options
[
:dependent
]
validate_dependent_option
[
:destroy
,
:delete
]
model
.
send
(
:class_eval
,
<<-
eoruby
,
__FILE__
,
__LINE__
+
1
)
def
#{
dependency_method_name
}
association(:
#{
name
}
).handle_dependency
end
eoruby
model
.
after_destroy
dependency_method_name
end
end
def
dependency_method_name
"belongs_to_dependent_for_
#{
name
}
"
def
valid_dependent_options
[
:destroy
,
:delete
]
end
end
end
activerecord/lib/active_record/associations/builder/collection_association.rb
浏览文件 @
6bb8afb4
module
ActiveRecord::Associations::Builder
class
CollectionAssociation
<
Association
#:nodoc:
CALLBACKS
=
[
:before_add
,
:after_add
,
:before_remove
,
:after_remove
]
...
...
activerecord/lib/active_record/associations/builder/has_many.rb
浏览文件 @
6bb8afb4
module
ActiveRecord::Associations::Builder
class
HasMany
<
CollectionAssociation
#:nodoc:
def
macro
...
...
@@ -9,27 +8,8 @@ def valid_options
super
+
[
:primary_key
,
:dependent
,
:as
,
:through
,
:source
,
:source_type
,
:inverse_of
]
end
def
build
reflection
=
super
configure_dependency
reflection
end
def
configure_dependency
if
dependent
=
options
[
:dependent
]
validate_dependent_option
[
:destroy
,
:delete_all
,
:nullify
,
:restrict
,
:restrict_with_error
,
:restrict_with_exception
]
name
=
self
.
name
mixin
.
redefine_method
(
dependency_method_name
)
do
association
(
name
).
handle_dependency
end
model
.
before_destroy
dependency_method_name
end
end
def
dependency_method_name
"has_many_dependent_for_
#{
name
}
"
def
valid_dependent_options
[
:destroy
,
:delete_all
,
:nullify
,
:restrict
,
:restrict_with_error
,
:restrict_with_exception
]
end
end
end
activerecord/lib/active_record/associations/builder/has_one.rb
浏览文件 @
6bb8afb4
module
ActiveRecord::Associations::Builder
class
HasOne
<
SingularAssociation
#:nodoc:
def
macro
...
...
@@ -15,27 +14,12 @@ def constructable?
!
options
[
:through
]
end
def
build
reflection
=
super
configure_dependency
unless
options
[
:through
]
reflection
end
def
configure_dependency
if
dependent
=
options
[
:dependent
]
validate_dependent_option
[
:destroy
,
:delete
,
:nullify
,
:restrict
,
:restrict_with_error
,
:restrict_with_exception
]
name
=
self
.
name
mixin
.
redefine_method
(
dependency_method_name
)
do
association
(
name
).
handle_dependency
end
model
.
before_destroy
dependency_method_name
end
super
unless
options
[
:through
]
end
def
dependency_method_name
"has_one_dependent_for_
#{
name
}
"
def
valid_dependent_options
[
:destroy
,
:delete
,
:nullify
,
:restrict
,
:restrict_with_error
,
:restrict_with_exception
]
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录