diff options
author | Yves Senn <yves.senn@gmail.com> | 2012-12-26 15:16:09 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2012-12-26 16:38:16 +0100 |
commit | 5203b6dd50dd88904c57c001ed49c157b3afe536 (patch) | |
tree | 59773c8496352442f5ea67b817ab18d38e91fb63 | |
parent | 885f59f6852cce670b48680fa0a1b6a4b0998291 (diff) | |
download | rails-5203b6dd50dd88904c57c001ed49c157b3afe536.tar.gz rails-5203b6dd50dd88904c57c001ed49c157b3afe536.tar.bz2 rails-5203b6dd50dd88904c57c001ed49c157b3afe536.zip |
backport #8616, quote column names in generated fixture files
Conflicts:
railties/CHANGELOG.md
railties/lib/rails/generators/test_unit/model/model_generator.rb
railties/lib/rails/generators/test_unit/model/templates/fixtures.yml
railties/test/generators/model_generator_test.rb
4 files changed, 40 insertions, 4 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 9110fdc673..0fed14c14e 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,5 +1,12 @@ ## Rails 3.2.9 (Nov 12, 2012) ## +* Quote column names in generates fixture files. This prevents + conflicts with reserved YAML keywords such as 'yes' and 'no' + Fix #8612. + Backport #8616. + + *Yves Senn* + * Engines with a dummy app include the rake tasks of dependencies in the app namespace. [Backport: #8262] Fix #8229 diff --git a/railties/lib/rails/generators/test_unit/model/model_generator.rb b/railties/lib/rails/generators/test_unit/model/model_generator.rb index c1dd535dd3..9749a6b133 100644 --- a/railties/lib/rails/generators/test_unit/model/model_generator.rb +++ b/railties/lib/rails/generators/test_unit/model/model_generator.rb @@ -3,6 +3,9 @@ require 'rails/generators/test_unit' module TestUnit module Generators class ModelGenerator < Base + + 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 @@ module TestUnit 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 diff --git a/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml b/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml index 5c8780aa64..2c33766418 100644 --- a/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml +++ b/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml @@ -3,12 +3,12 @@ <% unless attributes.empty? -%> one: <% attributes.each do |attribute| -%> - <%= attribute.name %>: <%= attribute.default %> + <%= yaml_key_value(attribute.name, attribute.default) %> <% end -%> two: <% attributes.each do |attribute| -%> - <%= attribute.name %>: <%= attribute.default %> + <%= yaml_key_value(attribute.name, attribute.default) %> <% end -%> <% else -%> # This model initially had no columns defined. If you add columns to the diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index f64abc1016..b96c591450 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -157,7 +157,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_match(/create_table :products/, up) assert_match(/t\.string :name/, up) assert_match(/t\.integer :supplier_id/, up) - + assert_match(/add_index :products, :name/, up) assert_match(/add_index :products, :supplier_id/, up) assert_no_match(/add_index :products, :year/, up) @@ -182,7 +182,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_match(/add_index :products, :discount, :unique => true/, content) end end - + def test_migration_without_timestamps ActiveRecord::Base.timestamped_migrations = false run_generator ["account"] @@ -261,7 +261,17 @@ class ModelGeneratorTest < Rails::Generators::TestCase def test_invokes_default_test_framework run_generator assert_file "test/unit/account_test.rb", /class AccountTest < ActiveSupport::TestCase/ + 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_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 @@ -329,4 +339,11 @@ class ModelGeneratorTest < Rails::Generators::TestCase run_generator ["Account"] assert_file 'app/models/account.rb', /# attr_accessible :title, :body/ 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 |