私がするInstallGenerator
ことは、Rails サイト自体にマイグレーションを追加する を追加することです。あなたが言及したものとまったく同じ動作ではありませんが、今のところ、私にとっては十分です。
ちょっとしたハウツー:
まず、フォルダーを作成し、そのフォルダー内に次のコードでlib\generators\<your-gem-name>\install
呼び出されるファイルを作成します。install_generator.rb
require 'rails/generators/migration'
module YourGemName
module Generators
class InstallGenerator < ::Rails::Generators::Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
desc "add the migrations"
def self.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
def copy_migrations
migration_template "create_something.rb", "db/migrate/create_something.rb"
migration_template "create_something_else.rb", "db/migrate/create_something_else.rb"
end
end
end
end
その中にlib/generators/<your-gem-name>/install/templates
、移行を含む 2 つのファイルを追加します。たとえば、次の名前のファイルを使用しますcreate_something.rb
。
class CreateAbilities < ActiveRecord::Migration
def self.up
create_table :abilities do |t|
t.string :name
t.string :description
t.boolean :needs_extent
t.timestamps
end
end
def self.down
drop_table :abilities
end
end
次に、宝石がアプリに追加されたら、次のことができます
rails g <your_gem_name>:install
これで移行が追加されますrake db:migrate
。
お役に立てれば。