diff options
13 files changed, 218 insertions, 3 deletions
diff --git a/railties/bin/gen b/railties/bin/gen index a6363edd54..9f105d2e19 100755 --- a/railties/bin/gen +++ b/railties/bin/gen @@ -26,8 +26,6 @@ if ARGV.size == 0 puts "Others: #{others.join(', ')}." unless others.empty? exit -elsif ARGV.size == 1 - ARGV << "--help" end Rails::Generators.builtin.each do |group, name| @@ -37,6 +35,7 @@ end name = ARGV.shift if klass = Rails::Generators.find_by_namespace(name, "rails") + ARGV << "--help" if klass.arguments.any? { |a| a.required? } && ARGV.empty? klass.start else puts "Could not find generator #{name}." diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index 2c4c3286d4..4e585a11e7 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -14,7 +14,7 @@ module ActiveRecord # # ==== Examples # - # migration_template "migrate.rb", "db/migrate/add_foo_to_bar" + # migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb" # def migration_template(source, destination=nil, log_status=true) destination = File.expand_path(destination || source, self.destination_root) diff --git a/railties/lib/generators/active_record/migration/migration_generator.rb b/railties/lib/generators/active_record/migration/migration_generator.rb new file mode 100644 index 0000000000..baf9650790 --- /dev/null +++ b/railties/lib/generators/active_record/migration/migration_generator.rb @@ -0,0 +1,25 @@ +require 'generators/active_record' + +module ActiveRecord + module Generators + class MigrationGenerator < Base + argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + + def create_migration_file + set_local_assigns! + migration_template "migration.rb", "db/migrate/#{file_name}.rb" + end + + protected + attr_reader :migration_action + + def set_local_assigns! + if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/ + @migration_action = $1 + @table_name = $2.pluralize + end + end + + end + end +end diff --git a/railties/lib/generators/active_record/migration/templates/migration.rb b/railties/lib/generators/active_record/migration/templates/migration.rb new file mode 100644 index 0000000000..2851f7cb42 --- /dev/null +++ b/railties/lib/generators/active_record/migration/templates/migration.rb @@ -0,0 +1,11 @@ +class <%= @migration_class_name %> < ActiveRecord::Migration + def self.up<% attributes.each do |attribute| %> + <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end -%> + <%- end %> + end + + def self.down<% attributes.reverse.each do |attribute| %> + <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end -%> + <%- end %> + end +end diff --git a/railties/lib/generators/active_record/session_migration/session_migration_generator.rb b/railties/lib/generators/active_record/session_migration/session_migration_generator.rb new file mode 100644 index 0000000000..d60da5c0a5 --- /dev/null +++ b/railties/lib/generators/active_record/session_migration/session_migration_generator.rb @@ -0,0 +1,20 @@ +require 'generators/active_record' + +module ActiveRecord + module Generators + class SessionMigrationGenerator < Base + argument :name, :type => :string, :default => "add_sessions_table" + + def create_migration_file + migration_template "migration.rb", "db/migrate/#{file_name}.rb" + end + + protected + + def session_table_name + ActiveRecord::Base.pluralize_table_names ? 'session'.pluralize : 'session' + end + + end + end +end diff --git a/railties/lib/generators/active_record/session_migration/templates/migration.rb b/railties/lib/generators/active_record/session_migration/templates/migration.rb new file mode 100644 index 0000000000..19811d9455 --- /dev/null +++ b/railties/lib/generators/active_record/session_migration/templates/migration.rb @@ -0,0 +1,16 @@ +class <%= @migration_class_name %> < ActiveRecord::Migration + def self.up + create_table :<%= session_table_name %> do |t| + t.string :session_id, :null => false + t.text :data + t.timestamps + end + + add_index :<%= session_table_name %>, :session_id + add_index :<%= session_table_name %>, :updated_at + end + + def self.down + drop_table :<%= session_table_name %> + end +end diff --git a/railties/lib/generators/rails/migration/USAGE b/railties/lib/generators/rails/migration/USAGE new file mode 100644 index 0000000000..d91127aac3 --- /dev/null +++ b/railties/lib/generators/rails/migration/USAGE @@ -0,0 +1,29 @@ +Description: + Stubs out a new database migration. Pass the migration name, either + CamelCased or under_scored, and an optional list of attribute pairs as arguments. + + A migration class is generated in db/migrate prefixed by a timestamp of the current date and time. + + You can name your migration in either of these formats to generate add/remove + column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable + +Example: + `./script/generate migration AddSslFlag` + + If the current date is May 14, 2008 and the current time 09:09:12, this creates the AddSslFlag migration + db/migrate/20080514090912_add_ssl_flag.rb + + `./script/generate migration AddTitleBodyToPost title:string body:text published:boolean` + + This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb with + this in the Up migration: + + add_column :posts, :title, :string + add_column :posts, :body, :text + add_column :posts, :published, :boolean + + And this in the Down migration: + + remove_column :posts, :published + remove_column :posts, :body + remove_column :posts, :title diff --git a/railties/lib/generators/rails/migration/migration_generator.rb b/railties/lib/generators/rails/migration/migration_generator.rb new file mode 100644 index 0000000000..6aefddd3fd --- /dev/null +++ b/railties/lib/generators/rails/migration/migration_generator.rb @@ -0,0 +1,8 @@ +module Rails + module Generators + class MigrationGenerator < NamedBase + argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + hook_for :orm + end + end +end diff --git a/railties/lib/generators/rails/session_migration/USAGE b/railties/lib/generators/rails/session_migration/USAGE new file mode 100644 index 0000000000..e106f6ecc8 --- /dev/null +++ b/railties/lib/generators/rails/session_migration/USAGE @@ -0,0 +1,8 @@ +Description: + Creates a migration to add the sessions table used by the ORM session store. + Pass the migration name, either CamelCased or under_scored, as an argument. + + Before invoking this generator, be sure that your ORM supports session stores. + +Example: + `./script/generate session_migration CreateSessionTable` diff --git a/railties/lib/generators/rails/session_migration/session_migration_generator.rb b/railties/lib/generators/rails/session_migration/session_migration_generator.rb new file mode 100644 index 0000000000..e7d988359c --- /dev/null +++ b/railties/lib/generators/rails/session_migration/session_migration_generator.rb @@ -0,0 +1,8 @@ +module Rails + module Generators + class SessionMigrationGenerator < NamedBase + argument :name, :type => :string, :default => "add_session_table" + hook_for :orm + end + end +end diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index 662646fbff..7c58c7b64e 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -73,4 +73,13 @@ class GeneratorsTestCase < Test::Unit::TestCase migration = Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first File.basename(migration) if migration end + + def assert_class_method_for(content, method, &block) + assert_instance_method_for content, "self.#{method}", &block + end + + def assert_instance_method_for(content, method) + assert_match /def #{method}(.*?)end/m, content + yield content.match(/def #{method}(.*?)end/m)[1] + end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb new file mode 100644 index 0000000000..90bc91344f --- /dev/null +++ b/railties/test/generators/migration_generator_test.rb @@ -0,0 +1,58 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record/migration/migration_generator' +require 'generators/rails/migration/migration_generator' + +class MigrationGeneratorTest < GeneratorsTestCase + + def test_migration + @migration = "change_title_body_from_posts" + run_generator + assert_migration "db/migrate/#{@migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/ + end + + def test_migration_with_class_name + @migration = "ChangeTitleBodyFromPosts" + run_generator + assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{@migration} < ActiveRecord::Migration/ + end + + def test_add_migration_with_attributes + @migration = "add_title_body_to_posts" + run_generator [@migration, "title:string", "body:text"] + content = assert_migration "db/migrate/#{@migration}.rb" + + assert_class_method_for content, :up do |up| + assert_match /add_column :posts, :title, :string/, up + assert_match /add_column :posts, :body, :text/, up + end + + assert_class_method_for content, :down do |down| + assert_match /remove_column :posts, :title/, down + assert_match /remove_column :posts, :body/, down + end + end + + def test_remove_migration_with_attributes + @migration = "remove_title_body_from_posts" + run_generator [@migration, "title:string", "body:text"] + content = assert_migration "db/migrate/#{@migration}.rb" + + assert_class_method_for content, :up do |up| + assert_match /remove_column :posts, :title/, up + assert_match /remove_column :posts, :body/, up + end + + assert_class_method_for content, :down do |down| + assert_match /add_column :posts, :title, :string/, down + assert_match /add_column :posts, :body, :text/, down + end + end + + protected + + def run_generator(args=[@migration]) + silence(:stdout) { Rails::Generators::MigrationGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/session_migration_generator_test.rb b/railties/test/generators/session_migration_generator_test.rb new file mode 100644 index 0000000000..05b51b1566 --- /dev/null +++ b/railties/test/generators/session_migration_generator_test.rb @@ -0,0 +1,24 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record/session_migration/session_migration_generator' +require 'generators/rails/session_migration/session_migration_generator' + +class SessionMigrationGeneratorTest < GeneratorsTestCase + + def test_session_migration_with_default_name + run_generator + assert_migration "db/migrate/add_sessions_table.rb", /class AddSessionsTable < ActiveRecord::Migration/ + end + + def test_session_migration_with_given_name + run_generator ["create_session_table"] + assert_migration "db/migrate/create_session_table.rb", /class CreateSessionTable < ActiveRecord::Migration/ + end + + protected + + def run_generator(args=[]) + silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args, :root => destination_root } + end + +end |