Last active
January 2, 2016 00:19
-
-
Save billychan/8222471 to your computer and use it in GitHub Desktop.
Rails gem generator pattern
This file contains hidden or 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
# 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