1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

quote column names in generated fixture files

This commit is contained in:
Yves Senn 2012-12-26 15:16:09 +01:00
parent 95fa0e69de
commit edae4777ad
4 changed files with 48 additions and 7 deletions

View file

@ -1,5 +1,11 @@
## Rails 4.0.0 (unreleased) ## ## 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. * Explicit options have precedence over `~/.railsrc` on the `rails new` command.
*Rafael Mendonça França* *Rafael Mendonça França*

View file

@ -3,6 +3,9 @@ require 'rails/generators/test_unit'
module TestUnit # :nodoc: module TestUnit # :nodoc:
module Generators # :nodoc: module Generators # :nodoc:
class ModelGenerator < Base # :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" argument :attributes, type: :array, default: [], banner: "field:type field:type"
class_option :fixture, type: :boolean class_option :fixture, type: :boolean
@ -19,6 +22,15 @@ module TestUnit # :nodoc:
template 'fixtures.yml', File.join('test/fixtures', class_path, "#{plural_file_name}.yml") template 'fixtures.yml', File.join('test/fixtures', class_path, "#{plural_file_name}.yml")
end end
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 end
end end

View file

@ -3,17 +3,17 @@
<% unless attributes.empty? -%> <% unless attributes.empty? -%>
one: one:
<% attributes.each do |attribute| -%> <% attributes.each do |attribute| -%>
<%= attribute.column_name %>: <%= attribute.default %> <%= yaml_key_value(attribute.column_name, attribute.default) %>
<%- if attribute.polymorphic? -%> <%- if attribute.polymorphic? -%>
<%= "#{attribute.name}_type: #{attribute.human_name}" %> <%= yaml_key_value("#{attribute.name}_type", attribute.human_name) %>
<%- end -%> <%- end -%>
<% end -%> <% end -%>
two: two:
<% attributes.each do |attribute| -%> <% attributes.each do |attribute| -%>
<%= attribute.column_name %>: <%= attribute.default %> <%= yaml_key_value(attribute.column_name, attribute.default) %>
<%- if attribute.polymorphic? -%> <%- if attribute.polymorphic? -%>
<%= "#{attribute.name}_type: #{attribute.human_name}" %> <%= yaml_key_value("#{attribute.name}_type", attribute.human_name) %>
<%- end -%> <%- end -%>
<% end -%> <% end -%>
<% else -%> <% else -%>

View file

@ -270,17 +270,34 @@ class ModelGeneratorTest < Rails::Generators::TestCase
def test_invokes_default_test_framework def test_invokes_default_test_framework
run_generator run_generator
assert_file "test/models/account_test.rb", /class AccountTest < ActiveSupport::TestCase/ 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 end
def test_fixtures_use_the_references_ids def test_fixtures_use_the_references_ids
run_generator ["LineItem", "product:references", "cart:belongs_to"] 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 end
def test_fixtures_use_the_references_ids_and_type def test_fixtures_use_the_references_ids_and_type
run_generator ["LineItem", "product:references{polymorphic}", "cart:belongs_to"] 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 end
def test_fixture_is_skipped def test_fixture_is_skipped
@ -338,4 +355,10 @@ class ModelGeneratorTest < Rails::Generators::TestCase
end end
end 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 end