From 557014d45aeed82ed1e79b83ab59697392a1a5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 24 Dec 2011 10:38:19 +0100 Subject: Tidy up migration types. --- .../generators/active_record/migration/migration_generator.rb | 2 +- .../generators/active_record/migration/templates/migration.rb | 4 ++-- .../lib/rails/generators/active_record/model/model_generator.rb | 6 +++++- .../rails/generators/active_record/model/templates/migration.rb | 9 ++------- 4 files changed, 10 insertions(+), 11 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb index 2db950fefb..1509e34473 100644 --- a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb +++ b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb @@ -3,7 +3,7 @@ require 'rails/generators/active_record' module ActiveRecord module Generators class MigrationGenerator < Base - argument :attributes, :type => :array, :default => [], :banner => "field:type field:type field:type:index" + argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" def create_migration_file set_local_assigns! diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb index 53018ffc9a..d084a00ed7 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -4,7 +4,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration <% attributes.each do |attribute| -%> add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> <%- if attribute.has_index? -%> - add_index :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_index_options %> + add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> <%- end %> <%- end -%> end @@ -14,7 +14,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration <%- if migration_action -%> <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %> <% if attribute.has_index? && migration_action == 'add' %> - add_index :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_index_options %> + add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> <% end -%> <%- end -%> <%- end -%> diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index c0527e231f..99a022461e 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -3,7 +3,7 @@ require 'rails/generators/active_record' module ActiveRecord module Generators class ModelGenerator < Base - argument :attributes, :type => :array, :default => [], :banner => "field:type field:type field:type:index" + argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" check_class_collision @@ -26,6 +26,10 @@ module ActiveRecord template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke end + def attributes_with_index + attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) } + end + hook_for :test_framework protected diff --git a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb index 0715d0262b..3a3cf86d73 100644 --- a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb @@ -8,13 +8,8 @@ class <%= migration_class_name %> < ActiveRecord::Migration t.timestamps <% end -%> end -<% if options[:indexes] -%> -<% attributes.select {|attr| attr.reference? }.each do |attribute| -%> - add_index :<%= table_name %>, :<%= attribute.name %>_id<%= attribute.inject_index_options %> -<% end -%> -<% end -%> -<% attributes.select {|attr| attr.has_index? }.each do |attribute| -%> - add_index :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_index_options %> +<% attributes_with_index.each do |attribute| -%> + add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> <% end -%> end end -- cgit v1.2.3