aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-07-01 11:50:06 +0200
committerJosé Valim <jose.valim@gmail.com>2009-07-02 10:27:39 +0200
commit10a9205c583a95ef35704aca93079b1700dd35f8 (patch)
tree554b0a0cfdb88fc5b00ec33c9bbf8b66fa327856
parentb77a7dcf4d1b072775e4f276ca59e18eaccc84ef (diff)
downloadrails-10a9205c583a95ef35704aca93079b1700dd35f8.tar.gz
rails-10a9205c583a95ef35704aca93079b1700dd35f8.tar.bz2
rails-10a9205c583a95ef35704aca93079b1700dd35f8.zip
Put migration methods into a separate folder and let just ActiveRecord dependency in ActiveRecord models.
-rw-r--r--railties/lib/generators/active_record.rb44
-rw-r--r--railties/lib/generators/migration.rb56
-rw-r--r--railties/lib/generators/named_base.rb2
-rw-r--r--railties/lib/generators/rails/scaffold_controller/USAGE16
-rw-r--r--railties/lib/generators/test_unit.rb1
-rw-r--r--railties/lib/generators/test_unit/mailer/mailer_generator.rb1
-rw-r--r--railties/lib/generators/test_unit/observer/observer_generator.rb2
-rw-r--r--railties/lib/generators/test_unit/plugin/plugin_generator.rb2
8 files changed, 76 insertions, 48 deletions
diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb
index 4e585a11e7..70f2421ac2 100644
--- a/railties/lib/generators/active_record.rb
+++ b/railties/lib/generators/active_record.rb
@@ -1,47 +1,16 @@
require 'generators/named_base'
+require 'generators/migration'
require 'active_record'
module ActiveRecord
module Generators
- module Migration
-
- # Creates a migration template at the given destination. The difference
- # to the default template method is that the migration number is appended
- # to the destination file name.
- #
- # The migration number, migration file name, migration class name are
- # available as instance variables in the template to be rendered.
- #
- # ==== Examples
- #
- # 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)
-
- migration_dir = File.dirname(destination)
- @migration_number = next_migration_number(migration_dir)
- @migration_file_name = File.basename(destination).sub(/\.rb$/, '')
- @migration_class_name = @migration_file_name.camelize
-
- if existing = migration_exists?(migration_dir, @migration_file_name)
- raise Rails::Generators::Error, "Another migration is already named #{@migration_file_name}: #{existing}"
- end
-
- destination = File.join(migration_dir, "#{@migration_number}_#{@migration_file_name}.rb")
- template(source, destination, log_status)
- end
+ class Base < Rails::Generators::NamedBase
+ include Rails::Generators::Migration
protected
- def migration_exists?(dirname, file_name) #:nodoc:
- Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
- end
-
- def current_migration_number(dirname) #:nodoc:
- Dir.glob("#{dirname}/[0-9]*_*.rb").collect{ |f| f.split("_").first.to_i }.max
- end
-
+ # Implement the required interface for Rails::Generators::Migration.
+ #
def next_migration_number(dirname) #:nodoc:
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
@@ -49,10 +18,7 @@ module ActiveRecord
"%.3d" % (current_migration_number(dirname) + 1)
end
end
- end
- class Base < Rails::Generators::NamedBase
- include Migration
end
end
end
diff --git a/railties/lib/generators/migration.rb b/railties/lib/generators/migration.rb
new file mode 100644
index 0000000000..1981ea6a59
--- /dev/null
+++ b/railties/lib/generators/migration.rb
@@ -0,0 +1,56 @@
+module Rails
+ module Generators
+ # Holds common methods for migrations. It assumes that migrations has the
+ # [0-9]*_name format and can be used by another frameworks (like Sequel)
+ # just by implementing the next migration number method.
+ #
+ module Migration
+
+ # Creates a migration template at the given destination. The difference
+ # to the default template method is that the migration number is appended
+ # to the destination file name.
+ #
+ # The migration number, migration file name, migration class name are
+ # available as instance variables in the template to be rendered.
+ #
+ # ==== Examples
+ #
+ # 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)
+
+ migration_dir = File.dirname(destination)
+ @migration_number = next_migration_number(migration_dir)
+ @migration_file_name = File.basename(destination).sub(/\.rb$/, '')
+ @migration_class_name = @migration_file_name.camelize
+
+ if existing = migration_exists?(migration_dir, @migration_file_name)
+ raise Rails::Generators::Error, "Another migration is already named #{@migration_file_name}: #{existing}"
+ end
+
+ destination = File.join(migration_dir, "#{@migration_number}_#{@migration_file_name}.rb")
+ template(source, destination, log_status)
+ end
+
+ protected
+
+ def migration_lookup_at(dirname) #:nodoc:
+ Dir.glob("#{dirname}/[0-9]*_*.rb")
+ end
+
+ def migration_exists?(dirname, file_name) #:nodoc:
+ migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first
+ end
+
+ def current_migration_number(dirname) #:nodoc:
+ migration_lookup_at(dirname).collect{ |f| f.split("_").first.to_i }.max
+ end
+
+ def next_migration_number(dirname) #:nodoc:
+ raise NotImplementError
+ end
+
+ end
+ end
+end
diff --git a/railties/lib/generators/named_base.rb b/railties/lib/generators/named_base.rb
index 553850fec5..54f832a9bd 100644
--- a/railties/lib/generators/named_base.rb
+++ b/railties/lib/generators/named_base.rb
@@ -92,7 +92,7 @@ module Rails
end
end
- # Deal with controller named base on scaffold
+ # Deal with controller named base on scaffold.
#
module ControllerNamedBase
def self.included(base) #:nodoc:
diff --git a/railties/lib/generators/rails/scaffold_controller/USAGE b/railties/lib/generators/rails/scaffold_controller/USAGE
index 2570da2e85..d60a3c3680 100644
--- a/railties/lib/generators/rails/scaffold_controller/USAGE
+++ b/railties/lib/generators/rails/scaffold_controller/USAGE
@@ -1,18 +1,20 @@
Description:
- Stubs out a scaffolded controller and its views. Pass the controller name,
+ Stubs out a scaffolded controller and its views. Pass the model name,
either CamelCased or under_scored, and a list of views as arguments.
+ The controller name is retrieved as a pluralized version of the model
+ name.
- To create a controller within a module, specify the controller name as a
+ To create a controller within a module, specify the model name as a
path like 'parent_module/controller_name'.
This generates a controller class in app/controllers and invokes helper,
template engine and test framework generators.
Example:
- `./script/generate scaffold_controller CreditCard open debit credit close`
+ `./script/generate scaffold_controller CreditCard`
Credit card controller with URLs like /credit_card/debit.
- Controller: app/controllers/credit_card_controller.rb
- Functional Test: test/functional/credit_card_controller_test.rb
- Views: app/views/credit_card/debit.html.erb [...]
- Helper: app/helpers/credit_card_helper.rb
+ Controller: app/controllers/credit_cards_controller.rb
+ Functional Test: test/functional/credit_cards_controller_test.rb
+ Views: app/views/credit_cards/index.html.erb [...]
+ Helper: app/helpers/credit_cards_helper.rb
diff --git a/railties/lib/generators/test_unit.rb b/railties/lib/generators/test_unit.rb
index c4b8a8702d..16d097c3c8 100644
--- a/railties/lib/generators/test_unit.rb
+++ b/railties/lib/generators/test_unit.rb
@@ -3,7 +3,6 @@ require 'generators/named_base'
module TestUnit
module Generators
class Base < Rails::Generators::NamedBase
- check_class_collision :suffix => "Test"
end
end
end
diff --git a/railties/lib/generators/test_unit/mailer/mailer_generator.rb b/railties/lib/generators/test_unit/mailer/mailer_generator.rb
index bd1906516c..559668d504 100644
--- a/railties/lib/generators/test_unit/mailer/mailer_generator.rb
+++ b/railties/lib/generators/test_unit/mailer/mailer_generator.rb
@@ -4,6 +4,7 @@ module TestUnit
module Generators
class MailerGenerator < Base
argument :actions, :type => :array, :default => [], :banner => "method method"
+ check_class_collision :suffix => "Test"
def create_test_files
template "unit_test.rb", File.join('test', 'unit', class_path, "#{file_name}_test.rb")
diff --git a/railties/lib/generators/test_unit/observer/observer_generator.rb b/railties/lib/generators/test_unit/observer/observer_generator.rb
index 48742f4b70..a2ed812e58 100644
--- a/railties/lib/generators/test_unit/observer/observer_generator.rb
+++ b/railties/lib/generators/test_unit/observer/observer_generator.rb
@@ -3,6 +3,8 @@ require 'generators/test_unit'
module TestUnit
module Generators
class ObserverGenerator < Base
+ check_class_collision :suffix => "ObserverTest"
+
def create_test_files
template 'unit_test.rb', File.join('test', 'unit', class_path, "#{file_name}_observer_test.rb")
end
diff --git a/railties/lib/generators/test_unit/plugin/plugin_generator.rb b/railties/lib/generators/test_unit/plugin/plugin_generator.rb
index 9c08dc4d50..293342c5d8 100644
--- a/railties/lib/generators/test_unit/plugin/plugin_generator.rb
+++ b/railties/lib/generators/test_unit/plugin/plugin_generator.rb
@@ -3,6 +3,8 @@ require 'generators/test_unit'
module TestUnit
module Generators
class PluginGenerator < Base
+ check_class_collision :suffix => "Test"
+
def create_test_files
directory 'test'
end