Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d7927372
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,发现更多精彩内容 >>
提交
d7927372
编写于
12月 26, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #8616 from senny/8612_respect_yaml_keywords
quote column names in generated fixture files
上级
5d80da81
edae4777
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
48 addition
and
7 deletion
+48
-7
railties/CHANGELOG.md
railties/CHANGELOG.md
+6
-0
railties/lib/rails/generators/test_unit/model/model_generator.rb
...s/lib/rails/generators/test_unit/model/model_generator.rb
+12
-0
railties/lib/rails/generators/test_unit/model/templates/fixtures.yml
...b/rails/generators/test_unit/model/templates/fixtures.yml
+4
-4
railties/test/generators/model_generator_test.rb
railties/test/generators/model_generator_test.rb
+26
-3
未找到文件。
railties/CHANGELOG.md
浏览文件 @
d7927372
## Rails 4.0.0 (unreleased) ##
*
Quote column names in generates fixture files. This prevents
conflicts with reserved YAML keywords such as 'yes' and 'no'
Fix #8612
*Yves Senn*
*
Explicit options have precedence over
`~/.railsrc`
on the
`rails new`
command.
*Rafael Mendonça França*
...
...
railties/lib/rails/generators/test_unit/model/model_generator.rb
浏览文件 @
d7927372
...
...
@@ -3,6 +3,9 @@
module
TestUnit
# :nodoc:
module
Generators
# :nodoc:
class
ModelGenerator
<
Base
# :nodoc:
RESERVED_YAML_KEYWORDS
=
%w(y yes n no true false on off null)
argument
:attributes
,
type: :array
,
default:
[],
banner:
"field:type field:type"
class_option
:fixture
,
type: :boolean
...
...
@@ -19,6 +22,15 @@ def create_fixture_file
template
'fixtures.yml'
,
File
.
join
(
'test/fixtures'
,
class_path
,
"
#{
plural_file_name
}
.yml"
)
end
end
private
def
yaml_key_value
(
key
,
value
)
if
RESERVED_YAML_KEYWORDS
.
include?
(
key
.
downcase
)
"'
#{
key
}
':
#{
value
}
"
else
"
#{
key
}
:
#{
value
}
"
end
end
end
end
end
railties/lib/rails/generators/test_unit/model/templates/fixtures.yml
浏览文件 @
d7927372
...
...
@@ -3,17 +3,17 @@
<% unless attributes.empty? -%>
one
:
<% attributes.each do |attribute| -%>
<%=
attribute.column_name %>
:
<%= attribute.default
%>
<%=
yaml_key_value(attribute.column_name, attribute.default)
%>
<%- if attribute.polymorphic? -%>
<%=
"#{attribute.name}_type
:
#{attribute.human_name}"
%>
<%=
yaml_key_value("#{attribute.name}_type", attribute.human_name)
%>
<%- end -%>
<% end -%>
two
:
<% attributes.each do |attribute| -%>
<%=
attribute.column_name %>
:
<%= attribute.default
%>
<%=
yaml_key_value(attribute.column_name, attribute.default)
%>
<%- if attribute.polymorphic? -%>
<%=
"#{attribute.name}_type
:
#{attribute.human_name}"
%>
<%=
yaml_key_value("#{attribute.name}_type", attribute.human_name)
%>
<%- end -%>
<% end -%>
<% else -%>
...
...
railties/test/generators/model_generator_test.rb
浏览文件 @
d7927372
...
...
@@ -270,17 +270,34 @@ def test_existing_migration_is_removed_on_force
def
test_invokes_default_test_framework
run_generator
assert_file
"test/models/account_test.rb"
,
/class AccountTest < ActiveSupport::TestCase/
assert_file
"test/fixtures/accounts.yml"
,
/name: MyString/
,
/age: 1/
assert_file
'test/fixtures/accounts.yml'
,
/name: MyString/
,
/age: 1/
assert_generated_fixture
(
'test/fixtures/accounts.yml'
,
{
'one'
=>
{
'name'
=>
'MyString'
,
'age'
=>
1
},
'two'
=>
{
'name'
=>
'MyString'
,
'age'
=>
1
}})
end
def
test_fixtures_use_the_references_ids
run_generator
[
"LineItem"
,
"product:references"
,
"cart:belongs_to"
]
assert_file
"test/fixtures/line_items.yml"
,
/product_id: \n cart_id: /
assert_file
'test/fixtures/line_items.yml'
,
/product_id: \n cart_id: /
assert_generated_fixture
(
'test/fixtures/line_items.yml'
,
{
"one"
=>
{
"product_id"
=>
nil
,
"cart_id"
=>
nil
},
"two"
=>
{
"product_id"
=>
nil
,
"cart_id"
=>
nil
}})
end
def
test_fixtures_use_the_references_ids_and_type
run_generator
[
"LineItem"
,
"product:references{polymorphic}"
,
"cart:belongs_to"
]
assert_file
"test/fixtures/line_items.yml"
,
/product_id: \n product_type: Product\n cart_id: /
assert_file
'test/fixtures/line_items.yml'
,
/product_id: \n product_type: Product\n cart_id: /
assert_generated_fixture
(
'test/fixtures/line_items.yml'
,
{
"one"
=>
{
"product_id"
=>
nil
,
"product_type"
=>
"Product"
,
"cart_id"
=>
nil
},
"two"
=>
{
"product_id"
=>
nil
,
"product_type"
=>
"Product"
,
"cart_id"
=>
nil
}})
end
def
test_fixtures_respect_reserved_yml_keywords
run_generator
[
"LineItem"
,
"no:integer"
,
"Off:boolean"
,
'ON:boolean'
]
assert_generated_fixture
(
'test/fixtures/line_items.yml'
,
{
"one"
=>
{
"no"
=>
1
,
"Off"
=>
false
,
"ON"
=>
false
},
"two"
=>
{
"no"
=>
1
,
"Off"
=>
false
,
"ON"
=>
false
}})
end
def
test_fixture_is_skipped
...
...
@@ -338,4 +355,10 @@ def test_index_is_skipped_for_references_association
end
end
end
private
def
assert_generated_fixture
(
path
,
parsed_contents
)
fixture_file
=
File
.
new
File
.
expand_path
(
path
,
destination_root
)
assert_equal
(
parsed_contents
,
YAML
.
load
(
fixture_file
))
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录