Skip to content

Instantly share code, notes, and snippets.

@Chris927
Created August 28, 2011 19:27
Show Gist options
  • Select an option

  • Save Chris927/1177096 to your computer and use it in GitHub Desktop.

Select an option

Save Chris927/1177096 to your computer and use it in GitHub Desktop.
Adding a second generator to a gem
require 'rails/generators/migration'
# must be placed in the gem's dir 'lib/generators/my_gem/second/second_generator.rb'
module MyGem
# TODO: this module tries to DRY up a bit, should be elsewhere in
# its own file
module MigrationGenerator
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def next_migration_number(path)
unless @prev_migration_nr
@prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
else
@prev_migration_nr += 1
end
@prev_migration_nr.to_s
end
end
end
module Generators
class SecondGenerator < ::Rails::Generators::Base
include Rails::Generators::Migration
include MyGem::MigrationGenerator
source_root File.expand_path('../templates', __FILE__)
desc "do the second thing"
def copy_migrations
migration_template "create_things.rb", "db/migrate/create_things.rb"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment