提交 d7927372 编写于 作者: R Rafael Mendonça França

Merge pull request #8616 from senny/8612_respect_yaml_keywords

quote column names in generated fixture files
## 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*
......
......@@ -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
......@@ -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 -%>
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册