diff options
Diffstat (limited to 'railties/lib')
14 files changed, 88 insertions, 19 deletions
diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb index f1b0d694af..fd7d3c9580 100644 --- a/railties/lib/generators.rb +++ b/railties/lib/generators.rb @@ -12,9 +12,6 @@ require 'rails/version' unless defined?(Rails::VERSION) require 'generators/base' require 'generators/named_base' -require 'generators/active_record' -require 'generators/erb' -require 'generators/test_unit' module Rails module Generators diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index be5d033868..2c4c3286d4 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -1,8 +1,58 @@ require 'generators/named_base' +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 "migrate.rb", "db/migrate/add_foo_to_bar" + # + 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_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 + + def next_migration_number(dirname) #:nodoc: + if ActiveRecord::Base.timestamped_migrations + Time.now.utc.strftime("%Y%m%d%H%M%S") + else + "%.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/active_record/model/model_generator.rb b/railties/lib/generators/active_record/model/model_generator.rb index 53592c222f..32a2ed15e4 100644 --- a/railties/lib/generators/active_record/model/model_generator.rb +++ b/railties/lib/generators/active_record/model/model_generator.rb @@ -1,3 +1,5 @@ +require 'generators/active_record' + module ActiveRecord module Generators class ModelGenerator < Base @@ -5,8 +7,7 @@ module ActiveRecord check_class_collision - conditional_class_option :timestamps - conditional_class_option :migration + conditional_class_options :migration, :timestamps class_option :parent, :type => :string, :desc => "The parent class for the generated model" @@ -15,12 +16,10 @@ module ActiveRecord template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb") end - # TODO Add migration support def create_migration_file if options[:migration] && options[:parent].nil? -# m.migration_template 'migration.rb', 'db/migrate', :assigns => { -# :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}" -# }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" + file_name = "create_#{file_path.gsub(/\//, '_').pluralize}" + migration_template "migration.rb", "db/migrate/#{file_name}.rb" end end diff --git a/railties/lib/generators/active_record/model/templates/migration.rb b/railties/lib/generators/active_record/model/templates/migration.rb index 382fd1156e..542e9db2fc 100644 --- a/railties/lib/generators/active_record/model/templates/migration.rb +++ b/railties/lib/generators/active_record/model/templates/migration.rb @@ -1,10 +1,10 @@ -class <%= migration_name %> < ActiveRecord::Migration +class <%= @migration_class_name %> < ActiveRecord::Migration def self.up create_table :<%= table_name %> do |t| <% for attribute in attributes -%> t.<%= attribute.type %> :<%= attribute.name %> <% end -%> -<% unless options[:skip_timestamps] %> +<% if options[:timestamps] %> t.timestamps <% end -%> end diff --git a/railties/lib/generators/active_record/observer/observer_generator.rb b/railties/lib/generators/active_record/observer/observer_generator.rb index 5bc9da34d1..fa51c23336 100644 --- a/railties/lib/generators/active_record/observer/observer_generator.rb +++ b/railties/lib/generators/active_record/observer/observer_generator.rb @@ -1,3 +1,5 @@ +require 'generators/active_record' + module ActiveRecord module Generators class ObserverGenerator < Base diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index 2bc6f7ec8a..386013c272 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -145,13 +145,13 @@ module Rails # "rails:generators:webrat", "webrat:generators:controller", "webrat" # def self.invoke_if(*names) - default_options = names.extract_options! - verbose = default_options.key?(:verbose) ? default_options[:verbose] : :blue + conditional_class_options(*names) + + options = names.extract_options! + verbose = options.fetch(:verbose, :blue) invocations.concat(names) names.each do |name| - conditional_class_option name, default_options.dup - class_eval <<-METHOD, __FILE__, __LINE__ def invoke_if_#{name} return unless options[#{name.inspect}] @@ -236,9 +236,14 @@ module Rails # Creates a conditional class option with type boolean, default value # lookup and default description. # - def self.conditional_class_option(name, options={}) - options[:desc] ||= "Indicates when to generate #{name.to_s.humanize.downcase}" - class_option name, options.merge!(:type => :boolean, :default => DEFAULTS[name] || false) + def self.conditional_class_options(*names) + default_options = names.extract_options! + + names.each do |name| + options = default_options.dup + options[:desc] ||= "Indicates when to generate #{name.to_s.humanize.downcase}" + class_option name, options.merge!(:type => :boolean, :default => DEFAULTS[name] || false) + end end # Overwrite class options help to allow invoked generators options to be diff --git a/railties/lib/generators/erb/controller/controller_generator.rb b/railties/lib/generators/erb/controller/controller_generator.rb index 171ec23b97..706c92cb91 100644 --- a/railties/lib/generators/erb/controller/controller_generator.rb +++ b/railties/lib/generators/erb/controller/controller_generator.rb @@ -1,3 +1,5 @@ +require 'generators/erb' + module Erb module Generators class ControllerGenerator < Base diff --git a/railties/lib/generators/erb/mailer/mailer_generator.rb b/railties/lib/generators/erb/mailer/mailer_generator.rb index 73db3d2484..398ab46dd8 100644 --- a/railties/lib/generators/erb/mailer/mailer_generator.rb +++ b/railties/lib/generators/erb/mailer/mailer_generator.rb @@ -1,3 +1,5 @@ +require 'generators/erb' + module Erb module Generators class MailerGenerator < Base diff --git a/railties/lib/generators/test_unit/controller/controller_generator.rb b/railties/lib/generators/test_unit/controller/controller_generator.rb index d0d12d79e9..a8cb5cbdbb 100644 --- a/railties/lib/generators/test_unit/controller/controller_generator.rb +++ b/railties/lib/generators/test_unit/controller/controller_generator.rb @@ -1,3 +1,5 @@ +require 'generators/test_unit' + module TestUnit module Generators class ControllerGenerator < Base diff --git a/railties/lib/generators/test_unit/helper/helper_generator.rb b/railties/lib/generators/test_unit/helper/helper_generator.rb index 866556f6a1..9ecfaa45ab 100644 --- a/railties/lib/generators/test_unit/helper/helper_generator.rb +++ b/railties/lib/generators/test_unit/helper/helper_generator.rb @@ -1,3 +1,5 @@ +require 'generators/test_unit' + module TestUnit module Generators class HelperGenerator < Base diff --git a/railties/lib/generators/test_unit/mailer/mailer_generator.rb b/railties/lib/generators/test_unit/mailer/mailer_generator.rb index 84e3024427..bd1906516c 100644 --- a/railties/lib/generators/test_unit/mailer/mailer_generator.rb +++ b/railties/lib/generators/test_unit/mailer/mailer_generator.rb @@ -1,3 +1,5 @@ +require 'generators/test_unit' + module TestUnit module Generators class MailerGenerator < Base diff --git a/railties/lib/generators/test_unit/model/model_generator.rb b/railties/lib/generators/test_unit/model/model_generator.rb index dc53c7cf2f..74440d93d3 100644 --- a/railties/lib/generators/test_unit/model/model_generator.rb +++ b/railties/lib/generators/test_unit/model/model_generator.rb @@ -1,10 +1,12 @@ +require 'generators/test_unit' + module TestUnit module Generators class ModelGenerator < Base argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" check_class_collision :suffix => "Test" - conditional_class_option :fixture + conditional_class_options :fixture def create_test_file 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 adfd90a086..48742f4b70 100644 --- a/railties/lib/generators/test_unit/observer/observer_generator.rb +++ b/railties/lib/generators/test_unit/observer/observer_generator.rb @@ -1,3 +1,5 @@ +require 'generators/test_unit' + module TestUnit module Generators class ObserverGenerator < Base diff --git a/railties/lib/generators/test_unit/plugin/plugin_generator.rb b/railties/lib/generators/test_unit/plugin/plugin_generator.rb index 427a7bd8f5..9c08dc4d50 100644 --- a/railties/lib/generators/test_unit/plugin/plugin_generator.rb +++ b/railties/lib/generators/test_unit/plugin/plugin_generator.rb @@ -1,3 +1,5 @@ +require 'generators/test_unit' + module TestUnit module Generators class PluginGenerator < Base |