diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-12-10 00:36:31 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-12-10 00:36:31 -0300 |
commit | 9e42cd86869dcc158b0321b2d2dfcc185a7d0282 (patch) | |
tree | a8d67fd8202716b35c3552c21db17ecc356f2ffe /railties/lib | |
parent | 05e6123653b7ceb98960403ec40788b3f34e3efd (diff) | |
parent | 190f638d398edf70fef0c32a389ed84a449bccb8 (diff) | |
download | rails-9e42cd86869dcc158b0321b2d2dfcc185a7d0282.tar.gz rails-9e42cd86869dcc158b0321b2d2dfcc185a7d0282.tar.bz2 rails-9e42cd86869dcc158b0321b2d2dfcc185a7d0282.zip |
Merge branch 'scaffold_fixes'
Diffstat (limited to 'railties/lib')
5 files changed, 22 insertions, 14 deletions
diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb index d8a4f15b4b..4ae8756ed0 100644 --- a/railties/lib/rails/generators/generated_attribute.rb +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -99,13 +99,17 @@ module Rails end def index_name - @index_name ||= if reference? - polymorphic? ? %w(id type).map { |t| "#{name}_#{t}" } : "#{name}_id" + @index_name ||= if polymorphic? + %w(id type).map { |t| "#{name}_#{t}" } else - name + column_name end end + def column_name + @column_name ||= reference? ? "#{name}_id" : name + end + def foreign_key? !!(name =~ /_id$/) end diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index cc10fd9177..9965db98de 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -160,6 +160,13 @@ module Rails end end + def attributes_names + @attributes_names ||= attributes.each_with_object([]) do |a, names| + names << a.column_name + names << "#{a.name}_type" if a.polymorphic? + end + end + def pluralize_table_names? !defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names end diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb index 08fe8a08e3..4d08b01e60 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -91,10 +91,10 @@ class <%= controller_class_name %>Controller < ApplicationController # Use this method to whitelist the permissible parameters. Example: params.require(:person).permit(:name, :age) # Also, you can specialize this method with per-user checking of permissible attributes. def <%= "#{singular_table_name}_params" %> - <%- if attributes.empty? -%> + <%- if attributes_names.empty? -%> params[<%= ":#{singular_table_name}" %>] <%- else -%> - params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes.map {|a| ":#{a.name}" }.join(', ') %>) + params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>) <%- 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..b2bcaf63be 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,14 @@ <% unless attributes.empty? -%> one: <% attributes.each do |attribute| -%> - <%= attribute.name %>: <%= attribute.default %> + <%= attribute.column_name %>: <%= attribute.default %> + <%= "#{attribute.name}_type: #{attribute.human_name}" if attribute.polymorphic? %> <% end -%> two: <% attributes.each do |attribute| -%> - <%= attribute.name %>: <%= attribute.default %> + <%= attribute.column_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 3b4fec2e83..8f3ecaadea 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -18,17 +18,12 @@ module TestUnit # :nodoc: private def attributes_hash - return if accessible_attributes.empty? + return if attributes_names.empty? - accessible_attributes.map do |a| - name = a.name + attributes_names.map do |name| "#{name}: @#{singular_table_name}.#{name}" end.sort.join(', ') end - - def accessible_attributes - attributes.reject(&:reference?) - end end end end |