Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 5203b6dd50dd88904c57c001ed49c157b3afe536 1 parent 885f59f
Yves Senn authored December 26, 2012
7  railties/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,12 @@
1 1
 ## Rails 3.2.9 (Nov 12, 2012) ##
2 2
 
  3
+*   Quote column names in generates fixture files. This prevents
  4
+    conflicts with reserved YAML keywords such as 'yes' and 'no'
  5
+    Fix #8612.
  6
+    Backport #8616.
  7
+
  8
+    *Yves Senn*
  9
+
3 10
 *   Engines with a dummy app include the rake tasks of dependencies in the app namespace. [Backport: #8262]
4 11
     Fix #8229
5 12
 
12  railties/lib/rails/generators/test_unit/model/model_generator.rb
@@ -3,6 +3,9 @@
3 3
 module TestUnit
4 4
   module Generators
5 5
     class ModelGenerator < Base
  6
+
  7
+      RESERVED_YAML_KEYWORDS = %w(y yes n no true false on off null)
  8
+
6 9
       argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7 10
       class_option :fixture, :type => :boolean
8 11
 
@@ -19,6 +22,15 @@ def create_fixture_file
19 22
           template 'fixtures.yml', File.join('test/fixtures', class_path, "#{plural_file_name}.yml")
20 23
         end
21 24
       end
  25
+
  26
+      private
  27
+        def yaml_key_value(key, value)
  28
+          if RESERVED_YAML_KEYWORDS.include?(key.downcase)
  29
+            "'#{key}': #{value}"
  30
+          else
  31
+            "#{key}: #{value}"
  32
+          end
  33
+        end
22 34
     end
23 35
   end
24 36
 end
4  railties/lib/rails/generators/test_unit/model/templates/fixtures.yml
@@ -3,12 +3,12 @@
3 3
 <% unless attributes.empty? -%>
4 4
 one:
5 5
 <% attributes.each do |attribute| -%>
6  
-  <%= attribute.name %>: <%= attribute.default %>
  6
+  <%= yaml_key_value(attribute.name, attribute.default) %>
7 7
 <% end -%>
8 8
 
9 9
 two:
10 10
 <% attributes.each do |attribute| -%>
11  
-  <%= attribute.name %>: <%= attribute.default %>
  11
+  <%= yaml_key_value(attribute.name, attribute.default) %>
12 12
 <% end -%>
13 13
 <% else -%>
14 14
 # This model initially had no columns defined.  If you add columns to the
21  railties/test/generators/model_generator_test.rb
@@ -157,7 +157,7 @@ def test_migration_with_missing_attribute_type_and_with_index
157 157
         assert_match(/create_table :products/, up)
158 158
         assert_match(/t\.string :name/, up)
159 159
         assert_match(/t\.integer :supplier_id/, up)
160  
-        
  160
+
161 161
         assert_match(/add_index :products, :name/, up)
162 162
         assert_match(/add_index :products, :supplier_id/, up)
163 163
         assert_no_match(/add_index :products, :year/, up)
@@ -182,7 +182,7 @@ def test_add_migration_with_attributes_index_declaration_and_attribute_options
182 182
       assert_match(/add_index :products, :discount, :unique => true/, content)
183 183
     end
184 184
   end
185  
-  
  185
+
186 186
   def test_migration_without_timestamps
187 187
     ActiveRecord::Base.timestamped_migrations = false
188 188
     run_generator ["account"]
@@ -261,7 +261,17 @@ def test_existing_migration_is_removed_on_force
261 261
   def test_invokes_default_test_framework
262 262
     run_generator
263 263
     assert_file "test/unit/account_test.rb", /class AccountTest < ActiveSupport::TestCase/
  264
+
264 265
     assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/
  266
+    assert_generated_fixture("test/fixtures/accounts.yml",
  267
+                             {"one"=>{"name"=>"MyString", "age"=>1}, "two"=>{"name"=>"MyString", "age"=>1}})
  268
+  end
  269
+
  270
+  def test_fixtures_respect_reserved_yml_keywords
  271
+    run_generator ["LineItem", "no:integer", "Off:boolean", "ON:boolean"]
  272
+
  273
+    assert_generated_fixture("test/fixtures/line_items.yml",
  274
+                             {"one"=>{"no"=>1, "Off"=>false, "ON"=>false}, "two"=>{"no"=>1, "Off"=>false, "ON"=>false}})
265 275
   end
266 276
 
267 277
   def test_fixture_is_skipped
@@ -329,4 +339,11 @@ def test_attr_accessible_added_with_comments_when_no_attributes_present
329 339
     run_generator ["Account"]
330 340
     assert_file 'app/models/account.rb', /# attr_accessible :title, :body/
331 341
   end
  342
+
  343
+  private
  344
+    def assert_generated_fixture(path, parsed_contents)
  345
+      fixture_file = File.new File.expand_path(path, destination_root)
  346
+      assert_equal(parsed_contents, YAML.load(fixture_file))
  347
+    end
  348
+
332 349
 end

0 notes on commit 5203b6d

Please sign in to comment.
Something went wrong with that request. Please try again.