Skip to content

Instantly share code, notes, and snippets.

@gilesbowkett
Created June 13, 2011 05:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilesbowkett/1022355 to your computer and use it in GitHub Desktop.
Save gilesbowkett/1022355 to your computer and use it in GitHub Desktop.
Rails migrations which automatically put the new filename in your paste buffer
# this monkey-patches code defined in railties-3.1.0.rc1/lib/rails/generators/migration.rb
# (obviously, this might differ very slightly in the latest release candidate)
module Rails
module Generators
module Migration
module ClassMethods
# differs from the "real" version by only one line
def migration_template(source, destination=nil, config={})
destination = File.expand_path(destination || source, self.destination_root)
migration_dir = File.dirname(destination)
@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 = self.class.migration_exists?(migration_dir, @migration_file_name)
if !(destination && options[:skip]) && behavior == :invoke
if destination && options.force?
remove_file(destination)
elsif destination
raise Error, "Another migration is already named #{@migration_file_name}: #{destination}"
end
destination = File.join(migration_dir, "#{@migration_number}_#{@migration_file_name}.rb")
system("printf #{destination} | pbcopy") # this is that one line
end
template(source, destination, config)
end
end
end
end
# note that if you change the line above, reversing the order of migration_number and
# migration_file_name, you can also get migration filenames which are not, from the perspective
# of experienced Unix users, completely fucking insane in their incomprehensible disregard
# for the incredible usefulness of tab completion (a baffling flaw also shared by Rails's
# config.ru default rackup file name).
# HOWEVER, making that change very likely breaks rake db:migrate, so I'm not going to advocate that
# unless I find time to make sure it works. :-)
@codeodor
Copy link

Oops. Yeah, I just figured since the other places needed it, it'd be needed there. In fact it's not, so nice catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment