Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
06121631
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,发现更多精彩内容 >>
提交
06121631
编写于
10月 05, 2018
作者:
G
Gannon McGibbon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce FixtureSet::TableRows and FixtureSet::TableRow
上级
ee5ab222
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
183 addition
and
88 deletion
+183
-88
activerecord/lib/active_record/fixture_set/table_row.rb
activerecord/lib/active_record/fixture_set/table_row.rb
+79
-0
activerecord/lib/active_record/fixture_set/table_rows.rb
activerecord/lib/active_record/fixture_set/table_rows.rb
+97
-0
activerecord/lib/active_record/fixtures.rb
activerecord/lib/active_record/fixtures.rb
+7
-88
未找到文件。
activerecord/lib/active_record/fixture_set/table_row.rb
0 → 100644
浏览文件 @
06121631
# frozen_string_literal: true
module
ActiveRecord
class
FixtureSet
class
TableRow
# :nodoc:
def
initialize
(
fixture
,
table_rows
:,
label
:,
now
:)
@table_rows
=
table_rows
@label
=
label
@now
=
now
@row
=
fixture
.
to_hash
fill_row_model_attributes
end
def
to_hash
@row
end
private
def
model_metadata
@table_rows
.
model_metadata
end
def
model_class
@table_rows
.
model_class
end
def
fill_row_model_attributes
return
unless
model_class
fill_timestamps
interpolate_label
generate_primary_key
resolve_enums
@table_rows
.
resolve_sti_reflections
(
@row
)
end
def
reflection_class
@reflection_class
||=
if
@row
.
include?
(
model_metadata
.
inheritance_column_name
)
@row
[
model_metadata
.
inheritance_column_name
].
constantize
rescue
model_class
else
model_class
end
end
def
fill_timestamps
# fill in timestamp columns if they aren't specified and the model is set to record_timestamps
if
model_class
.
record_timestamps
model_metadata
.
timestamp_column_names
.
each
do
|
c_name
|
@row
[
c_name
]
=
@now
unless
@row
.
key?
(
c_name
)
end
end
end
def
interpolate_label
# interpolate the fixture label
@row
.
each
do
|
key
,
value
|
@row
[
key
]
=
value
.
gsub
(
"$LABEL"
,
@label
.
to_s
)
if
value
.
is_a?
(
String
)
end
end
def
generate_primary_key
# generate a primary key if necessary
if
model_metadata
.
has_primary_key_column?
&&
!
@row
.
include?
(
model_metadata
.
primary_key_name
)
@row
[
model_metadata
.
primary_key_name
]
=
ActiveRecord
::
FixtureSet
.
identify
(
@label
,
model_metadata
.
primary_key_type
)
end
end
def
resolve_enums
model_class
.
defined_enums
.
each
do
|
name
,
values
|
if
@row
.
include?
(
name
)
@row
[
name
]
=
values
.
fetch
(
@row
[
name
],
@row
[
name
])
end
end
end
end
end
end
activerecord/lib/active_record/fixture_set/table_rows.rb
0 → 100644
浏览文件 @
06121631
# frozen_string_literal: true
require
"active_record/fixture_set/table_row"
require
"active_record/fixture_set/model_metadata"
module
ActiveRecord
class
FixtureSet
class
TableRows
# :nodoc:
def
initialize
(
table_name
,
model_class
:,
fixtures
:,
config
:)
@table_name
=
table_name
@model_class
=
model_class
# track any join tables we need to insert later
@tables
=
Hash
.
new
{
|
h
,
table
|
h
[
table
]
=
[]
}
build_table_rows_from
(
fixtures
,
config
)
end
attr_reader
:table_name
,
:model_class
def
to_hash
@tables
.
transform_values
{
|
rows
|
rows
.
map
(
&
:to_hash
)
}
end
def
model_metadata
@model_metadata
||=
ModelMetadata
.
new
(
model_class
,
table_name
)
end
def
resolve_sti_reflections
(
row
)
# If STI is used, find the correct subclass for association reflection
reflection_class
=
reflection_class_for
(
row
)
reflection_class
.
_reflections
.
each_value
do
|
association
|
case
association
.
macro
when
:belongs_to
# Do not replace association name with association foreign key if they are named the same
fk_name
=
(
association
.
options
[
:foreign_key
]
||
"
#{
association
.
name
}
_id"
).
to_s
if
association
.
name
.
to_s
!=
fk_name
&&
value
=
row
.
delete
(
association
.
name
.
to_s
)
if
association
.
polymorphic?
&&
value
.
sub!
(
/\s*\(([^\)]*)\)\s*$/
,
""
)
# support polymorphic belongs_to as "label (Type)"
row
[
association
.
foreign_type
]
=
$1
end
fk_type
=
reflection_class
.
type_for_attribute
(
fk_name
).
type
row
[
fk_name
]
=
ActiveRecord
::
FixtureSet
.
identify
(
value
,
fk_type
)
end
when
:has_many
if
association
.
options
[
:through
]
add_join_records
(
row
,
HasManyThroughProxy
.
new
(
association
))
end
end
end
end
private
def
build_table_rows_from
(
fixtures
,
config
)
now
=
config
.
default_timezone
==
:utc
?
Time
.
now
.
utc
:
Time
.
now
@tables
[
table_name
]
=
fixtures
.
map
do
|
label
,
fixture
|
TableRow
.
new
(
fixture
,
table_rows:
self
,
label:
label
,
now:
now
,
)
end
end
def
reflection_class_for
(
row
)
if
row
.
include?
(
model_metadata
.
inheritance_column_name
)
row
[
model_metadata
.
inheritance_column_name
].
constantize
rescue
model_class
else
model_class
end
end
def
add_join_records
(
row
,
association
)
# This is the case when the join table has no fixtures file
if
(
targets
=
row
.
delete
(
association
.
name
.
to_s
))
table_name
=
association
.
join_table
column_type
=
association
.
primary_key_type
lhs_key
=
association
.
lhs_key
rhs_key
=
association
.
rhs_key
targets
=
targets
.
is_a?
(
Array
)
?
targets
:
targets
.
split
(
/\s*,\s*/
)
joins
=
targets
.
map
do
|
target
|
{
lhs_key
=>
row
[
model_metadata
.
primary_key_name
],
rhs_key
=>
ActiveRecord
::
FixtureSet
.
identify
(
target
,
column_type
)
}
end
@tables
[
table_name
].
concat
(
joins
)
end
end
end
end
end
activerecord/lib/active_record/fixtures.rb
浏览文件 @
06121631
...
...
@@ -8,7 +8,7 @@
require
"active_support/core_ext/digest/uuid"
require
"active_record/fixture_set/file"
require
"active_record/fixture_set/render_context"
require
"active_record/fixture_set/
model_metadata
"
require
"active_record/fixture_set/
table_rows
"
require
"active_record/test_fixtures"
require
"active_record/errors"
...
...
@@ -656,76 +656,15 @@ def size
# Returns a hash of rows to be inserted. The key is the table, the value is
# a list of rows to insert to that table.
def
table_rows
now
=
config
.
default_timezone
==
:utc
?
Time
.
now
.
utc
:
Time
.
now
# allow a standard key to be used for doing defaults in YAML
fixtures
.
delete
(
"DEFAULTS"
)
# track any join tables we need to insert later
rows
=
Hash
.
new
{
|
h
,
table
|
h
[
table
]
=
[]
}
rows
[
table_name
]
=
fixtures
.
map
do
|
label
,
fixture
|
row
=
fixture
.
to_hash
if
model_class
# fill in timestamp columns if they aren't specified and the model is set to record_timestamps
if
model_class
.
record_timestamps
model_metadata
.
timestamp_column_names
.
each
do
|
c_name
|
row
[
c_name
]
=
now
unless
row
.
key?
(
c_name
)
end
end
# interpolate the fixture label
row
.
each
do
|
key
,
value
|
row
[
key
]
=
value
.
gsub
(
"$LABEL"
,
label
.
to_s
)
if
value
.
is_a?
(
String
)
end
# generate a primary key if necessary
if
model_metadata
.
has_primary_key_column?
&&
!
row
.
include?
(
model_metadata
.
primary_key_name
)
row
[
model_metadata
.
primary_key_name
]
=
ActiveRecord
::
FixtureSet
.
identify
(
label
,
model_metadata
.
primary_key_type
)
end
# Resolve enums
model_class
.
defined_enums
.
each
do
|
name
,
values
|
if
row
.
include?
(
name
)
row
[
name
]
=
values
.
fetch
(
row
[
name
],
row
[
name
])
end
end
# If STI is used, find the correct subclass for association reflection
reflection_class
=
if
row
.
include?
(
model_metadata
.
inheritance_column_name
)
row
[
model_metadata
.
inheritance_column_name
].
constantize
rescue
model_class
else
model_class
end
reflection_class
.
_reflections
.
each_value
do
|
association
|
case
association
.
macro
when
:belongs_to
# Do not replace association name with association foreign key if they are named the same
fk_name
=
(
association
.
options
[
:foreign_key
]
||
"
#{
association
.
name
}
_id"
).
to_s
if
association
.
name
.
to_s
!=
fk_name
&&
value
=
row
.
delete
(
association
.
name
.
to_s
)
if
association
.
polymorphic?
&&
value
.
sub!
(
/\s*\(([^\)]*)\)\s*$/
,
""
)
# support polymorphic belongs_to as "label (Type)"
row
[
association
.
foreign_type
]
=
$1
end
fk_type
=
reflection_class
.
type_for_attribute
(
fk_name
).
type
row
[
fk_name
]
=
ActiveRecord
::
FixtureSet
.
identify
(
value
,
fk_type
)
end
when
:has_many
if
association
.
options
[
:through
]
add_join_records
(
rows
,
row
,
HasManyThroughProxy
.
new
(
association
))
end
end
end
end
row
end
rows
TableRows
.
new
(
table_name
,
model_class:
model_class
,
fixtures:
fixtures
,
config:
config
,
).
to_hash
end
class
ReflectionProxy
# :nodoc:
...
...
@@ -762,26 +701,6 @@ def join_table
private
def
add_join_records
(
rows
,
row
,
association
)
# This is the case when the join table has no fixtures file
if
(
targets
=
row
.
delete
(
association
.
name
.
to_s
))
table_name
=
association
.
join_table
column_type
=
association
.
primary_key_type
lhs_key
=
association
.
lhs_key
rhs_key
=
association
.
rhs_key
targets
=
targets
.
is_a?
(
Array
)
?
targets
:
targets
.
split
(
/\s*,\s*/
)
rows
[
table_name
].
concat
targets
.
map
{
|
target
|
{
lhs_key
=>
row
[
model_metadata
.
primary_key_name
],
rhs_key
=>
ActiveRecord
::
FixtureSet
.
identify
(
target
,
column_type
)
}
}
end
end
def
model_metadata
@model_metadata
||=
ModelMetadata
.
new
(
model_class
,
table_name
)
end
def
model_class
=
(
class_name
)
if
class_name
.
is_a?
(
Class
)
# TODO: Should be an AR::Base type class, or any?
@model_class
=
class_name
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录