From 17bee0dd2fcce2d040bd6edda3e19cb11c5813d9 Mon Sep 17 00:00:00 2001 From: Szymon Nowak Date: Sat, 30 Jan 2010 17:38:42 +0100 Subject: Change Rails::Generators::Migration protected instance methods to class methods. [#3820 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- railties/lib/rails/generators/migration.rb | 50 ++++++++++++++++-------------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'railties/lib/rails/generators/migration.rb') diff --git a/railties/lib/rails/generators/migration.rb b/railties/lib/rails/generators/migration.rb index 0a9151ecdf..957d695d3a 100644 --- a/railties/lib/rails/generators/migration.rb +++ b/railties/lib/rails/generators/migration.rb @@ -6,11 +6,36 @@ module Rails # module Migration def self.included(base) #:nodoc: + base.extend ClassMethods base.send :attr_reader, :migration_number, :migration_file_name, :migration_class_name end + module ClassMethods + 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 do |file| + File.basename(file).split("_").first.to_i + end.max.to_i + end + + def next_migration_number(dirname) #:nodoc: + orm = Rails.configuration.generators.options[:rails][:orm] + require "generators/#{orm}" + "#{orm.to_s.camelize}::Generators::Base".constantize.next_migration_number(dirname) + rescue + raise NotImplementedError + end + end + # 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. @@ -26,11 +51,11 @@ module Rails destination = File.expand_path(destination || source, self.destination_root) migration_dir = File.dirname(destination) - @migration_number = next_migration_number(migration_dir) + @migration_number = self.class.next_migration_number(migration_dir) @migration_file_name = File.basename(destination).sub(/\.rb$/, '') @migration_class_name = @migration_file_name.camelize - destination = migration_exists?(migration_dir, @migration_file_name) + destination = self.class.migration_exists?(migration_dir, @migration_file_name) if behavior == :invoke raise Error, "Another migration is already named #{@migration_file_name}: #{destination}" if destination @@ -39,27 +64,6 @@ module Rails template(source, destination, config) 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 do |file| - File.basename(file).split("_").first.to_i - end.max.to_i - end - - def next_migration_number(dirname) #:nodoc: - raise NotImplementError - end - end end end -- cgit v1.2.3