Skip to content

Instantly share code, notes, and snippets.

@billychan
Last active January 2, 2016 00:19
Show Gist options
  • Save billychan/8222471 to your computer and use it in GitHub Desktop.
Save billychan/8222471 to your computer and use it in GitHub Desktop.
Rails gem generator pattern
# Simple pattern to generate both migration file and copy
# other files
require 'rails/generators'
require 'rails/generators/migration'
require 'rails/generators/active_record'
class FooGemGenerator < Rails::Generators::Base
include Rails::Generators::Migration
desc "some descrption"
self.source_paths << File.join(File.dirname(__FILE__), 'templates')
def self.next_migration_number(path)
ActiveRecord::Generators::Base.next_migration_number(path)
end
def create_migration_file
migration_template "create_foo.rb", "db/migrate/create_foo.rb"
end
def copy_other_file
copy_file "foo.yml", "app/models/foo.yml"
end
end
# Usage
# $ rails g foo_gem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment