Skip to content

Commit

Permalink
Merge pull request #8618 from senny/backport_8612
Browse files Browse the repository at this point in the history
backport #8616, quote column names in generated fixture files
  • Loading branch information
rafaelfranca committed Dec 26, 2012
2 parents 885f59f + 5203b6d commit 8c938dd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
7 changes: 7 additions & 0 deletions railties/CHANGELOG.md
@@ -1,5 +1,12 @@
## Rails 3.2.9 (Nov 12, 2012) ## ## 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] * Engines with a dummy app include the rake tasks of dependencies in the app namespace. [Backport: #8262]
Fix #8229 Fix #8229


Expand Down
12 changes: 12 additions & 0 deletions railties/lib/rails/generators/test_unit/model/model_generator.rb
Expand Up @@ -3,6 +3,9 @@
module TestUnit module TestUnit
module Generators module Generators
class ModelGenerator < Base 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" argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
class_option :fixture, :type => :boolean class_option :fixture, :type => :boolean


Expand All @@ -19,6 +22,15 @@ def create_fixture_file
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
Expand Up @@ -3,12 +3,12 @@
<% unless attributes.empty? -%> <% unless attributes.empty? -%>
one: one:
<% attributes.each do |attribute| -%> <% attributes.each do |attribute| -%>
<%= attribute.name %>: <%= attribute.default %> <%= yaml_key_value(attribute.name, attribute.default) %>
<% end -%> <% end -%>


two: two:
<% attributes.each do |attribute| -%> <% attributes.each do |attribute| -%>
<%= attribute.name %>: <%= attribute.default %> <%= yaml_key_value(attribute.name, attribute.default) %>
<% end -%> <% end -%>
<% else -%> <% else -%>
# This model initially had no columns defined. If you add columns to the # This model initially had no columns defined. If you add columns to the
Expand Down
21 changes: 19 additions & 2 deletions railties/test/generators/model_generator_test.rb
Expand Up @@ -157,7 +157,7 @@ def test_migration_with_missing_attribute_type_and_with_index
assert_match(/create_table :products/, up) assert_match(/create_table :products/, up)
assert_match(/t\.string :name/, up) assert_match(/t\.string :name/, up)
assert_match(/t\.integer :supplier_id/, up) assert_match(/t\.integer :supplier_id/, up)

assert_match(/add_index :products, :name/, up) assert_match(/add_index :products, :name/, up)
assert_match(/add_index :products, :supplier_id/, up) assert_match(/add_index :products, :supplier_id/, up)
assert_no_match(/add_index :products, :year/, up) assert_no_match(/add_index :products, :year/, up)
Expand All @@ -182,7 +182,7 @@ def test_add_migration_with_attributes_index_declaration_and_attribute_options
assert_match(/add_index :products, :discount, :unique => true/, content) assert_match(/add_index :products, :discount, :unique => true/, content)
end end
end end

def test_migration_without_timestamps def test_migration_without_timestamps
ActiveRecord::Base.timestamped_migrations = false ActiveRecord::Base.timestamped_migrations = false
run_generator ["account"] run_generator ["account"]
Expand Down Expand Up @@ -261,7 +261,17 @@ def test_existing_migration_is_removed_on_force
def test_invokes_default_test_framework def test_invokes_default_test_framework
run_generator run_generator
assert_file "test/unit/account_test.rb", /class AccountTest < ActiveSupport::TestCase/ assert_file "test/unit/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_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
Expand Down Expand Up @@ -329,4 +339,11 @@ def test_attr_accessible_added_with_comments_when_no_attributes_present
run_generator ["Account"] run_generator ["Account"]
assert_file 'app/models/account.rb', /# attr_accessible :title, :body/ assert_file 'app/models/account.rb', /# attr_accessible :title, :body/
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

0 comments on commit 8c938dd

Please sign in to comment.