Created
June 13, 2011 05:38
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. :-) |
merged. before I forget: I think the method_added comment got copy/pasted to a file which doesn't actually use method_added. I'll take that out later tonight or on the weekend, and then see if I have any other tweaks.
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
Forked (the project, not the gist =)) and sent you a pull request. Let me know what you think, how it can be improved, etc.