「振る舞い」と呼ばれるマクロメソッドが役立ちます(bonyiiiの例を使用):
def generate_model
if behavior == :invoke
Rails::Generators.invoke("active_record:model", ["foo", "--no-migration"], behavior: behavior)
Rails::Generators.invoke("active_record:model", ["bar", "--no-migration"], behavior: behavior)
else # behavior == :revoke
Rails::Generators.invoke("active_record:model", ["foo", "--no-migration"], behavior: :revoke)
Rails::Generators.invoke("active_record:model", ["bar", "--no-migration"], behavior: :revoke)
end
end
あるいは単に:
def generate_model
Rails::Generators.invoke("active_record:model", ["foo", "--no-migration"], behavior: behavior)
Rails::Generators.invoke("active_record:model", ["bar", "--no-migration"], behavior: behavior)
end