Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8616 from senny/8612_respect_yaml_keywords

quote column names in generated fixture files
  • Loading branch information...
commit d792737207b53807bf124b6a6d72453871678a90 2 parents 5d80da8 + edae477
@rafaelfranca rafaelfranca authored
View
6 railties/CHANGELOG.md
@@ -1,5 +1,11 @@
## 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*
View
12 railties/lib/rails/generators/test_unit/model/model_generator.rb
@@ -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
View
8 railties/lib/rails/generators/test_unit/model/templates/fixtures.yml
@@ -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 -%>
View
29 railties/test/generators/model_generator_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.