From b05b77be9caf2617f35d93786d9b40c17e07bf64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 9 Dec 2012 15:20:32 -0300 Subject: Deal with polymorphic attributes correctly in the generators --- .../generators/test_unit/model/templates/fixtures.yml | 2 ++ .../generators/test_unit/scaffold/scaffold_generator.rb | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'railties/lib/rails/generators/test_unit') 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 71f4f99d02..7f29932ceb 100644 --- a/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml +++ b/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml @@ -4,11 +4,13 @@ one: <% attributes.each do |attribute| -%> <%= attribute.reference? ? "#{attribute.name}_id" : attribute.name %>: <%= attribute.default %> + <%= "#{attribute.name}_type: #{attribute.human_name}" if attribute.polymorphic? %> <% end -%> two: <% attributes.each do |attribute| -%> <%= attribute.reference? ? "#{attribute.name}_id" : attribute.name %>: <%= attribute.default %> + <%= "#{attribute.name}_type: #{attribute.human_name}" if attribute.polymorphic? %> <% end -%> <% else -%> # This model initially had no columns defined. If you add columns to the diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index 8939813304..08bf50dfe2 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -20,11 +20,17 @@ module TestUnit # :nodoc: def attributes_hash return if attributes.empty? - attributes.map do |a| - name = a.name - name = "#{name}_id" if a.reference? - "#{name}: @#{singular_table_name}.#{name}" - end.sort.join(', ') + hash_values = [] + attributes.each do |a| + hash_values << hash_value(a.reference? ? "#{a.name}_id" : a.name) + hash_values << hash_value("#{a.name}_type") if a.polymorphic? + end + + hash_values.sort.join(', ') + end + + def hash_value(name) + "#{name}: @#{singular_table_name}.#{name}" end end end -- cgit v1.2.3