Call task more than once in Rails 3 generator

流过昼夜 提交于 2019-11-30 07:07:04

One more thought, this way it is also possible to run multiple model generator without migration

Rails::Generators.invoke("active_record:model", ["foo", "--no-migration" ])
Rails::Generators.invoke("active_record:model", ["bar", "--no-migration" ])

With Thor, if you want to invoke a task withOUT dependency management, you just call it directly:

model(foo)
model(bar

In case you want to run a generator that subclasses from Thor::Group, i.e. not just a single Thor task, you can invoke an entire generator from any different file.

Rails::Generators.invoke("my_generator", my_generator_args)

The generators module generators.rb in railties/rails seems to create a new instance, so it doesn't think that the task has already been called. This means you can repeat the above line as many times as you want and it will run each time.

I don't know of an elegant way to do that. In this talk, I gave an example of a custom generator that invokes the controller generator twice - check out slide 43.

The inelegant way is to go into Thor's @_invocations array and delete the first run's tasks before running it again.

there is a macro method called "behavior" can help(using bonyiii's example):

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

or just:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!