From b4b8c26b6e3568525b974889e60bfd2a025f0114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 6 Dec 2012 17:48:11 -0200 Subject: Use the references and belongs_id ids in the scaffold_controller generator --- .../rails/generators/rails/scaffold_controller/templates/controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib/rails') 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..5fc93efc6d 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -94,7 +94,7 @@ class <%= controller_class_name %>Controller < ApplicationController <%- if attributes.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.map { |a| ":#{a.index_name}" }.join(', ') %>) <%- end -%> end end -- cgit v1.2.3 From 3d8fcdd3bf21482e3e297a78bfdf3d02ec71eda4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 6 Dec 2012 18:00:41 -0200 Subject: Put the reference and belongs_to attributes in the scaffold tests. --- .../rails/generators/test_unit/scaffold/scaffold_generator.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'railties/lib/rails') 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..8939813304 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,14 @@ module TestUnit # :nodoc: private def attributes_hash - return if accessible_attributes.empty? + return if attributes.empty? - accessible_attributes.map do |a| + attributes.map do |a| name = a.name + name = "#{name}_id" if a.reference? "#{name}: @#{singular_table_name}.#{name}" end.sort.join(', ') end - - def accessible_attributes - attributes.reject(&:reference?) - end end end end -- cgit v1.2.3 From 4a487f94b9812964e644716f301ab287dea8cddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 9 Dec 2012 14:34:26 -0300 Subject: Make references and belongs_to attributes to generate the _id column in fixtures --- railties/lib/rails/generators/test_unit/model/templates/fixtures.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'railties/lib/rails') 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..71f4f99d02 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 %> + <%= attribute.reference? ? "#{attribute.name}_id" : attribute.name %>: <%= attribute.default %> <% end -%> two: <% attributes.each do |attribute| -%> - <%= attribute.name %>: <%= attribute.default %> + <%= attribute.reference? ? "#{attribute.name}_id" : attribute.name %>: <%= attribute.default %> <% end -%> <% else -%> # This model initially had no columns defined. If you add columns to the -- cgit v1.2.3 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 --- .../scaffold_controller/scaffold_controller_generator.rb | 9 +++++++++ .../rails/scaffold_controller/templates/controller.rb | 4 ++-- .../generators/test_unit/model/templates/fixtures.yml | 2 ++ .../generators/test_unit/scaffold/scaffold_generator.rb | 16 +++++++++++----- 4 files changed, 24 insertions(+), 7 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb index 4f36b612ae..3cfd2233e2 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -22,6 +22,15 @@ module Rails hook_for :helper, as: :scaffold do |invoked| invoke invoked, [ controller_name ] end + + private + + def attributes_names + attributes.each_with_object([]) do |attribute, names| + names << (attribute.reference? ? ":#{attribute.name}_id" : ":#{attribute.name}") + names << ":#{attribute.name}_type" if attribute.polymorphic? + end + end end end 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 5fc93efc6d..078ca3a996 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.index_name}" }.join(', ') %>) + params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.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 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 From 80735ff4c059d634e5eb3a83ce6a0bb8bda5cd8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 9 Dec 2012 15:53:23 -0300 Subject: Extract a attributes_names method to avoid duplication --- railties/lib/rails/generators/named_base.rb | 7 +++++++ .../scaffold_controller/scaffold_controller_generator.rb | 9 --------- .../rails/scaffold_controller/templates/controller.rb | 2 +- .../generators/test_unit/scaffold/scaffold_generator.rb | 16 ++++------------ 4 files changed, 12 insertions(+), 22 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index cc10fd9177..25d3f5518f 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.reference? ? "#{a.name}_id" : a.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/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb index 3cfd2233e2..4f36b612ae 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -22,15 +22,6 @@ module Rails hook_for :helper, as: :scaffold do |invoked| invoke invoked, [ controller_name ] end - - private - - def attributes_names - attributes.each_with_object([]) do |attribute, names| - names << (attribute.reference? ? ":#{attribute.name}_id" : ":#{attribute.name}") - names << ":#{attribute.name}_type" if attribute.polymorphic? - end - end end end 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 078ca3a996..4d08b01e60 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -94,7 +94,7 @@ class <%= controller_class_name %>Controller < ApplicationController <%- if attributes_names.empty? -%> params[<%= ":#{singular_table_name}" %>] <%- else -%> - params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.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/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index 08bf50dfe2..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,19 +18,11 @@ module TestUnit # :nodoc: private def attributes_hash - return if attributes.empty? + return if attributes_names.empty? - 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}" + attributes_names.map do |name| + "#{name}: @#{singular_table_name}.#{name}" + end.sort.join(', ') end end end -- cgit v1.2.3 From 95ccbd847da149b194cbf423e04e3d19a812dce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 9 Dec 2012 17:01:03 -0300 Subject: Add GeneratedAttribute#column_name to get the name of the column in the database --- railties/lib/rails/generators/generated_attribute.rb | 10 +++++++--- railties/lib/rails/generators/named_base.rb | 2 +- .../rails/generators/test_unit/model/templates/fixtures.yml | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'railties/lib/rails') 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 25d3f5518f..9965db98de 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -162,7 +162,7 @@ module Rails def attributes_names @attributes_names ||= attributes.each_with_object([]) do |a, names| - names << (a.reference? ? "#{a.name}_id" : a.name) + names << a.column_name names << "#{a.name}_type" if a.polymorphic? 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 7f29932ceb..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,13 +3,13 @@ <% unless attributes.empty? -%> one: <% attributes.each do |attribute| -%> - <%= attribute.reference? ? "#{attribute.name}_id" : 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.reference? ? "#{attribute.name}_id" : attribute.name %>: <%= attribute.default %> + <%= attribute.column_name %>: <%= attribute.default %> <%= "#{attribute.name}_type: #{attribute.human_name}" if attribute.polymorphic? %> <% end -%> <% else -%> -- cgit v1.2.3