Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2ea49a85
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,发现更多精彩内容 >>
提交
2ea49a85
编写于
6月 19, 2018
作者:
B
Bogdan Gusiev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Removed useless utility classes inside HABTM association builder
上级
433a3122
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
16 addition
and
37 deletion
+16
-37
activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
...ve_record/associations/builder/has_and_belongs_to_many.rb
+16
-37
未找到文件。
activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
浏览文件 @
2ea49a85
...
...
@@ -2,39 +2,6 @@
module
ActiveRecord::Associations::Builder
# :nodoc:
class
HasAndBelongsToMany
# :nodoc:
class
JoinTableResolver
# :nodoc:
KnownTable
=
Struct
.
new
:join_table
class
KnownClass
# :nodoc:
def
initialize
(
lhs_class
,
rhs_class_name
)
@lhs_class
=
lhs_class
@rhs_class_name
=
rhs_class_name
@join_table
=
nil
end
def
join_table
@join_table
||=
[
@lhs_class
.
table_name
,
klass
.
table_name
].
sort
.
join
(
"
\0
"
).
gsub
(
/^(.*[._])(.+)\0\1(.+)/
,
'\1\2_\3'
).
tr
(
"
\0
"
,
"_"
)
end
private
def
klass
@lhs_class
.
send
(
:compute_type
,
@rhs_class_name
)
end
end
def
self
.
build
(
lhs_class
,
name
,
options
)
if
options
[
:join_table
]
KnownTable
.
new
options
[
:join_table
].
to_s
else
class_name
=
options
.
fetch
(
:class_name
)
{
name
.
to_s
.
camelize
.
singularize
}
KnownClass
.
new
lhs_class
,
class_name
.
to_s
end
end
end
attr_reader
:lhs_model
,
:association_name
,
:options
def
initialize
(
association_name
,
lhs_model
,
options
)
...
...
@@ -44,8 +11,6 @@ def initialize(association_name, lhs_model, options)
end
def
through_model
habtm
=
JoinTableResolver
.
build
lhs_model
,
association_name
,
options
join_model
=
Class
.
new
(
ActiveRecord
::
Base
)
{
class
<<
self
attr_accessor
:left_model
...
...
@@ -56,7 +21,9 @@ class << self
end
def
self
.
table_name
table_name_resolver
.
join_table
# Table name needs to be resolved lazily
# because RHS class might not have been loaded
@table_name
||=
table_name_resolver
.
call
end
def
self
.
compute_type
(
class_name
)
...
...
@@ -86,7 +53,7 @@ def self.suppress_composite_primary_key(pk)
}
join_model
.
name
=
"HABTM_
#{
association_name
.
to_s
.
camelize
}
"
join_model
.
table_name_resolver
=
habtm
join_model
.
table_name_resolver
=
->
{
table_name
}
join_model
.
left_model
=
lhs_model
join_model
.
add_left_association
:left_side
,
anonymous_class:
lhs_model
...
...
@@ -117,6 +84,18 @@ def middle_options(join_model)
middle_options
end
def
table_name
if
options
[
:join_table
]
options
[
:join_table
].
to_s
else
class_name
=
options
.
fetch
(
:class_name
)
{
association_name
.
to_s
.
camelize
.
singularize
}
klass
=
lhs_model
.
send
(
:compute_type
,
class_name
.
to_s
)
[
lhs_model
.
table_name
,
klass
.
table_name
].
sort
.
join
(
"
\0
"
).
gsub
(
/^(.*[._])(.+)\0\1(.+)/
,
'\1\2_\3'
).
tr
(
"
\0
"
,
"_"
)
end
end
def
belongs_to_options
(
options
)
rhs_options
=
{}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录