diff options
author | Sammy Larbi <sam@codeodor.com> | 2012-12-26 13:02:28 -0600 |
---|---|---|
committer | Sammy Larbi <sam@codeodor.com> | 2013-03-01 06:13:30 -0600 |
commit | 20e041579f5fe9df51bdc40048cc1ef78915e116 (patch) | |
tree | dadb0c23877b488daef2e5896c4193213a5b91a0 /activerecord/lib/rails | |
parent | bb9f8312e7fe31566b898f87be149b13daf4dc57 (diff) | |
download | rails-20e041579f5fe9df51bdc40048cc1ef78915e116.tar.gz rails-20e041579f5fe9df51bdc40048cc1ef78915e116.tar.bz2 rails-20e041579f5fe9df51bdc40048cc1ef78915e116.zip |
Support creating a table migration generator
Sometimes you want to create a table without an associated model and
test, which is also not a join table. With this commit, you can now
do that.
Example:
rails g migration create_posts title:string
or
rails g migration CreatePosts title:string
This commit also moves the template the model generator uses for the
migration to the migration templates folder, as it seems a more
sensible place for it now that it is shared code.
Diffstat (limited to 'activerecord/lib/rails')
-rw-r--r-- | activerecord/lib/rails/generators/active_record/migration/migration_generator.rb | 11 | ||||
-rw-r--r-- | activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb (renamed from activerecord/lib/rails/generators/active_record/model/templates/migration.rb) | 0 | ||||
-rw-r--r-- | activerecord/lib/rails/generators/active_record/model/model_generator.rb | 2 |
3 files changed, 10 insertions, 3 deletions
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 5f1dbe36d6..b967bb6e0f 100644 --- a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb +++ b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb @@ -8,13 +8,14 @@ module ActiveRecord def create_migration_file set_local_assigns! validate_file_name! - migration_template "migration.rb", "db/migrate/#{file_name}.rb" + migration_template @migration_template, "db/migrate/#{file_name}.rb" end protected attr_reader :migration_action, :join_tables def set_local_assigns! + @migration_template = "migration.rb" case file_name when /^(add|remove)_.*_(?:to|from)_(.*)/ @migration_action = $1 @@ -26,6 +27,9 @@ module ActiveRecord set_index_names end + when /^create_(.+)/ + @table_name = $1.pluralize + @migration_template = "create_table_migration.rb" end end @@ -44,7 +48,10 @@ module ActiveRecord end private - + def attributes_with_index + attributes.select { |a| !a.reference? && a.has_index? } + end + def validate_file_name! unless file_name =~ /^[_a-z0-9]+$/ raise IllegalMigrationNameError.new(file_name) diff --git a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb index 3a3cf86d73..3a3cf86d73 100644 --- a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb 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 5f36181694..40e134e626 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -15,7 +15,7 @@ module ActiveRecord def create_migration_file return unless options[:migration] && options[:parent].nil? attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false - migration_template "migration.rb", "db/migrate/create_#{table_name}.rb" + migration_template "../../migration/templates/create_table_migration.rb", "db/migrate/create_#{table_name}.rb" end def create_model_file |