Skip to content

Instantly share code, notes, and snippets.

@subelsky
Created April 12, 2012 17:49
Show Gist options
  • Save subelsky/2369608 to your computer and use it in GitHub Desktop.
Save subelsky/2369608 to your computer and use it in GitHub Desktop.
Open Rails migrations in an editor automatically after creation
# As soon as I create a Rails migration I always want to open that file in an editor.
# I'd like to submit the following patch to Rails to create an --editor argument
# for the migration generator but I can't quite figure out the Thor way of doing this.
require 'rails/generators/active_record'
module ActiveRecord
module Generators
class MigrationGenerator < Base
argument :editor, :type => :string, :lazy_default => ENV['EDITOR'], :required => false, :banner => "/path/to/your/editor", :desc => "Open migration using specified editor (defaults to EDITOR)"
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
def create_migration_file
set_local_assigns!
path = migration_template "migration.rb", "db/migrate/#{file_name}.rb"
# here's the part where I'd like to use the cmdline argument
# instead of EDITOR
run("#{ENV['EDITOR']} #{path}")
end
protected
attr_reader :migration_action
def set_local_assigns!
if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
@migration_action = $1
@table_name = $2.pluralize
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment