mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Put migration methods into a separate folder and let just ActiveRecord dependency in ActiveRecord models.
This commit is contained in:
@@ -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
|
||||
|
||||
56
railties/lib/generators/migration.rb
Normal file
56
railties/lib/generators/migration.rb
Normal file
@@ -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
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user