Skip to content

Commit

Permalink
Change Rails::Generators::Migration protected instance methods to cla…
Browse files Browse the repository at this point in the history
…ss methods. [#3820 status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
szimek authored and josevalim committed Feb 2, 2010
1 parent c01014a commit 17bee0d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
17 changes: 8 additions & 9 deletions activerecord/lib/generators/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ def self.source_root
end
end

protected
# 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")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
# Implement the required interface for Rails::Generators::Migration.
#
def self.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
end
end
50 changes: 27 additions & 23 deletions railties/lib/rails/generators/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,36 @@ module Generators
#
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.
Expand All @@ -26,11 +51,11 @@ def migration_template(source, destination=nil, config={})
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
Expand All @@ -39,27 +64,6 @@ def migration_template(source, destination=nil, config={})

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

0 comments on commit 17bee0d

Please sign in to comment.