From 63f188ceb01fa02487946e21ef623218f5074264 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 25 Feb 2006 19:13:04 +0000 Subject: [PATCH] Added that script/generate model will now automatically create a migration file for the model created. This can be turned off by calling the generator with --skip-migration [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3644 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/CHANGELOG | 2 ++ .../generators/components/model/USAGE | 4 +++- .../components/model/model_generator.rb | 16 ++++++++++++++++ .../components/model/templates/migration.rb | 11 +++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 railties/lib/rails_generator/generators/components/model/templates/migration.rb diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 5380a90935..e63f68f355 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added that script/generate model will now automatically create a migration file for the model created. This can be turned off by calling the generator with --skip-migration [DHH] + * Added -d/--database option to the rails command, so you can do "rails --database=sqlite2 myapp" to start a new application preconfigured to use SQLite2 as the database. Removed the configuration examples from SQLite and PostgreSQL from the default MySQL configuration [DHH] * Allow script/server -c /path/to/lighttpd.conf [Jeremy Kemper] diff --git a/railties/lib/rails_generator/generators/components/model/USAGE b/railties/lib/rails_generator/generators/components/model/USAGE index 9d5a2fd749..57156ea535 100644 --- a/railties/lib/rails_generator/generators/components/model/USAGE +++ b/railties/lib/rails_generator/generators/components/model/USAGE @@ -5,7 +5,8 @@ Description: given in CamelCase or under_score and should not be suffixed with 'Model'. The generator creates a model class in app/models, a test suite in - test/unit, and test fixtures in test/fixtures/singular_name.yml. + test/unit, test fixtures in test/fixtures/singular_name.yml, and a migration + in db/migrate. Example: ./script/generate model Account @@ -14,4 +15,5 @@ Example: Model: app/models/account.rb Test: test/unit/account_test.rb Fixtures: test/fixtures/accounts.yml + Migration: db/migrate/XXX_add_accounts.rb diff --git a/railties/lib/rails_generator/generators/components/model/model_generator.rb b/railties/lib/rails_generator/generators/components/model/model_generator.rb index a978b96b1c..5e9aa882f3 100644 --- a/railties/lib/rails_generator/generators/components/model/model_generator.rb +++ b/railties/lib/rails_generator/generators/components/model/model_generator.rb @@ -1,4 +1,6 @@ class ModelGenerator < Rails::Generator::NamedBase + default_options :skip_migration => false + def manifest record do |m| # Check for class naming collisions. @@ -13,6 +15,20 @@ class ModelGenerator < Rails::Generator::NamedBase m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb") m.template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb") m.template 'fixtures.yml', File.join('test/fixtures', class_path, "#{table_name}.yml") + + unless options[:skip_migration] + m.migration_template 'migration.rb', 'db/migrate', :assigns => { + :migration_name => "Add#{class_name.pluralize}" + } + end end end + + protected + def add_options!(opt) + opt.separator '' + opt.separator 'Options:' + opt.on("--skip-migration", + "Don't generate a migration file for this model") { |options[:skip_migration]| } + end end diff --git a/railties/lib/rails_generator/generators/components/model/templates/migration.rb b/railties/lib/rails_generator/generators/components/model/templates/migration.rb new file mode 100644 index 0000000000..023c07d2f6 --- /dev/null +++ b/railties/lib/rails_generator/generators/components/model/templates/migration.rb @@ -0,0 +1,11 @@ +class <%= migration_name %> < ActiveRecord::Migration + def self.up + create_table "<%= table_name %>" do |t| + # t.column "name", :string + end + end + + def self.down + drop_table "<%= table_name %>" + end +end