Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ba3ecf53
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,发现更多精彩内容 >>
提交
ba3ecf53
编写于
9月 02, 2008
作者:
C
Clemens Kofler
提交者:
Jeremy Kemper
9月 03, 2008
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Some performance goodness for AR associations.
Signed-off-by:
N
Jeremy Kemper
<
jeremy@bitsweat.net
>
上级
1646e8c3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
32 addition
and
32 deletion
+32
-32
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations.rb
+32
-32
未找到文件。
activerecord/lib/active_record/associations.rb
浏览文件 @
ba3ecf53
...
...
@@ -878,10 +878,10 @@ def has_one(association_id, options = {})
method_name
=
"has_one_after_save_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
instance_variable_get
(
"
#{
ivar
}
"
)
if
instance_variable_defined?
(
"
#{
ivar
}
"
)
association
=
instance_variable_get
(
ivar
)
if
instance_variable_defined?
(
ivar
)
if
!
association
.
nil?
&&
(
new_record?
||
association
.
new_record?
||
association
[
"
#{
reflection
.
primary_key_name
}
"
]
!=
id
)
association
[
"
#{
reflection
.
primary_key_name
}
"
]
=
id
if
!
association
.
nil?
&&
(
new_record?
||
association
.
new_record?
||
association
[
reflection
.
primary_key_name
]
!=
id
)
association
[
reflection
.
primary_key_name
]
=
id
association
.
save
(
true
)
end
end
...
...
@@ -994,7 +994,7 @@ def belongs_to(association_id, options = {})
method_name
=
"polymorphic_belongs_to_before_save_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
instance_variable_get
(
"
#{
ivar
}
"
)
if
instance_variable_defined?
(
"
#{
ivar
}
"
)
association
=
instance_variable_get
(
ivar
)
if
instance_variable_defined?
(
ivar
)
if
association
&&
association
.
target
if
association
.
new_record?
...
...
@@ -1002,8 +1002,8 @@ def belongs_to(association_id, options = {})
end
if
association
.
updated?
self
[
"
#{
reflection
.
primary_key_name
}
"
]
=
association
.
id
self
[
"
#{
reflection
.
options
[
:foreign_type
]
}
"
]
=
association
.
class
.
base_class
.
name
.
to_s
self
[
reflection
.
primary_key_name
]
=
association
.
id
self
[
reflection
.
options
[
:foreign_type
]
]
=
association
.
class
.
base_class
.
name
.
to_s
end
end
end
...
...
@@ -1015,7 +1015,7 @@ def belongs_to(association_id, options = {})
method_name
=
"belongs_to_before_save_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
instance_variable_get
(
"
#{
ivar
}
"
)
if
instance_variable_defined?
(
"
#{
ivar
}
"
)
association
=
instance_variable_get
(
ivar
)
if
instance_variable_defined?
(
ivar
)
if
!
association
.
nil?
if
association
.
new_record?
...
...
@@ -1023,7 +1023,7 @@ def belongs_to(association_id, options = {})
end
if
association
.
updated?
self
[
"
#{
reflection
.
primary_key_name
}
"
]
=
association
.
id
self
[
reflection
.
primary_key_name
]
=
association
.
id
end
end
end
...
...
@@ -1038,15 +1038,15 @@ def belongs_to(association_id, options = {})
method_name
=
"belongs_to_counter_cache_after_create_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
send
(
"
#{
reflection
.
name
}
"
)
association
.
class
.
increment_counter
(
"
#{
cache_column
}
"
,
send
(
"
#{
reflection
.
primary_key_name
}
"
))
unless
association
.
nil?
association
=
send
(
reflection
.
name
)
association
.
class
.
increment_counter
(
cache_column
,
send
(
reflection
.
primary_key_name
))
unless
association
.
nil?
end
after_create
method_name
method_name
=
"belongs_to_counter_cache_before_destroy_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
send
(
"
#{
reflection
.
name
}
"
)
association
.
class
.
decrement_counter
(
"
#{
cache_column
}
"
,
send
(
"
#{
reflection
.
primary_key_name
}
"
))
unless
association
.
nil?
association
=
send
(
reflection
.
name
)
association
.
class
.
decrement_counter
(
cache_column
,
send
(
reflection
.
primary_key_name
))
unless
association
.
nil?
end
before_destroy
method_name
...
...
@@ -1329,19 +1329,19 @@ def collection_accessor_methods(reflection, association_proxy_class, writer = tr
end
end
end
def
add_single_associated_validation_callbacks
(
association_name
)
method_name
=
"validate_associated_records_for_
#{
association_name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
instance_variable_get
(
"@
#{
association_name
}
"
)
if
!
association
.
nil?
errors
.
add
"
#{
association_name
}
"
unless
association
.
target
.
nil?
||
association
.
valid?
errors
.
add
association_name
unless
association
.
target
.
nil?
||
association
.
valid?
end
end
validate
method_name
end
def
add_multiple_associated_validation_callbacks
(
association_name
)
method_name
=
"validate_associated_records_for_
#{
association_name
}
"
.
to_sym
ivar
=
"@
#{
association_name
}
"
...
...
@@ -1357,7 +1357,7 @@ def add_multiple_associated_validation_callbacks(association_name)
else
association
.
target
.
select
{
|
record
|
record
.
new_record?
}
end
.
each
do
|
record
|
errors
.
add
"
#{
association_name
}
"
unless
record
.
valid?
errors
.
add
association_name
unless
record
.
valid?
end
end
end
...
...
@@ -1377,7 +1377,7 @@ def add_multiple_associated_save_callbacks(association_name)
method_name
=
"after_create_or_update_associated_records_for_
#{
association_name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
instance_variable_get
(
"
#{
ivar
}
"
)
if
instance_variable_defined?
(
"
#{
ivar
}
"
)
association
=
instance_variable_get
(
ivar
)
if
instance_variable_defined?
(
ivar
)
records_to_save
=
if
@new_record_before_save
association
...
...
@@ -1444,7 +1444,7 @@ def configure_dependency_for_has_many(reflection)
when
:destroy
method_name
=
"has_many_dependent_destroy_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
send
(
"
#{
reflection
.
name
}
"
).
each
{
|
o
|
o
.
destroy
}
send
(
reflection
.
name
).
each
{
|
o
|
o
.
destroy
}
end
before_destroy
method_name
when
:delete_all
...
...
@@ -1463,22 +1463,22 @@ def configure_dependency_for_has_one(reflection)
when
:destroy
method_name
=
"has_one_dependent_destroy_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
send
(
"
#{
reflection
.
name
}
"
)
association
=
send
(
reflection
.
name
)
association
.
destroy
unless
association
.
nil?
end
before_destroy
method_name
when
:delete
method_name
=
"has_one_dependent_delete_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
send
(
"
#{
reflection
.
name
}
"
)
association
=
send
(
reflection
.
name
)
association
.
class
.
delete
(
association
.
id
)
unless
association
.
nil?
end
before_destroy
method_name
when
:nullify
method_name
=
"has_one_dependent_nullify_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
send
(
"
#{
reflection
.
name
}
"
)
association
.
update_attribute
(
"
#{
reflection
.
primary_key_name
}
"
,
nil
)
unless
association
.
nil?
association
=
send
(
reflection
.
name
)
association
.
update_attribute
(
reflection
.
primary_key_name
,
nil
)
unless
association
.
nil?
end
before_destroy
method_name
else
...
...
@@ -1493,14 +1493,14 @@ def configure_dependency_for_belongs_to(reflection)
when
:destroy
method_name
=
"belongs_to_dependent_destroy_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
send
(
"
#{
reflection
.
name
}
"
)
association
=
send
(
reflection
.
name
)
association
.
destroy
unless
association
.
nil?
end
before_destroy
method_name
when
:delete
method_name
=
"belongs_to_dependent_delete_for_
#{
reflection
.
name
}
"
.
to_sym
define_method
(
method_name
)
do
association
=
send
(
"
#{
reflection
.
name
}
"
)
association
=
send
(
reflection
.
name
)
association
.
class
.
delete
(
association
.
id
)
unless
association
.
nil?
end
before_destroy
method_name
...
...
@@ -1535,7 +1535,7 @@ def create_has_one_reflection(association_id, options)
create_reflection
(
:has_one
,
association_id
,
options
,
self
)
end
def
create_has_one_through_reflection
(
association_id
,
options
)
options
.
assert_valid_keys
(
:class_name
,
:foreign_key
,
:remote
,
:select
,
:conditions
,
:order
,
:include
,
:dependent
,
:counter_cache
,
:extend
,
:as
,
:through
,
:source
,
:source_type
,
:validate
...
...
@@ -1927,7 +1927,7 @@ def aliased_prefix
end
def
aliased_primary_key
"
#{
aliased_prefix
}
_r0"
"
#{
aliased_prefix
}
_r0"
end
def
aliased_table_name
...
...
@@ -1939,7 +1939,7 @@ def column_names_with_alias
@column_names_with_alias
=
[]
([
primary_key
]
+
(
column_names
-
[
primary_key
])).
each_with_index
do
|
column_name
,
i
|
@column_names_with_alias
<<
[
column_name
,
"
#{
aliased_prefix
}
_r
#{
i
}
"
]
@column_names_with_alias
<<
[
column_name
,
"
#{
aliased_prefix
}
_r
#{
i
}
"
]
end
end
...
...
@@ -1976,11 +1976,11 @@ def initialize(reflection, join_dependency, parent = nil)
@aliased_prefix
=
"t
#{
join_dependency
.
joins
.
size
}
"
@parent_table_name
=
parent
.
active_record
.
table_name
@aliased_table_name
=
aliased_table_name_for
(
table_name
)
if
reflection
.
macro
==
:has_and_belongs_to_many
@aliased_join_table_name
=
aliased_table_name_for
(
reflection
.
options
[
:join_table
],
"_join"
)
end
if
[
:has_many
,
:has_one
].
include?
(
reflection
.
macro
)
&&
reflection
.
options
[
:through
]
@aliased_join_table_name
=
aliased_table_name_for
(
reflection
.
through_reflection
.
klass
.
table_name
,
"_join"
)
end
...
...
@@ -2117,7 +2117,7 @@ def association_join
end
protected
def
aliased_table_name_for
(
name
,
suffix
=
nil
)
if
!
parent
.
table_joins
.
blank?
&&
parent
.
table_joins
.
to_s
.
downcase
=~
%r{join(
\s
+
\w
+)?
\s
+
#{
name
.
downcase
}
\s
on}
@join_dependency
.
table_aliases
[
name
]
+=
1
...
...
@@ -2135,7 +2135,7 @@ def aliased_table_name_for(name, suffix = nil)
name
end
def
pluralize
(
table_name
)
ActiveRecord
::
Base
.
pluralize_table_names
?
table_name
.
to_s
.
pluralize
:
table_name
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录