开发者

Writing own scaffold generator, exporting as a gem

I have created my own scaffold generator, now it's sitting in my ~/.rails/generator/scaffold directory. How can I make a gem in order to use my generator along other machines I'm using, without the need of copying the ~/.rails folder on every box ? I will push later the gem 开发者_高级运维on Github


From the documentation:

For a RubyGem, put your generator class and templates in the lib directory. For a Rails plugin, make a generators directory at the root of your plugin.

However that's a little vague.

From the Rails::Generator::Lookup source:

  1. Rails application. If RAILS_ROOT is defined we know we're generating in the context of a Rails application, so search RAILS_ROOT/generators.

  2. Look in plugins, either for generators/ or rails_generators/ directories within each plugin

  3. User home directory. Search ~/.rails/generators.

  4. RubyGems. Search for gems named *_generator, and look for generators within any RubyGem's /rails_generators/_generator.rb file.

  5. Builtins. Model, controller, mailer, scaffold, and so on.

A single generator gem should be structured like this:

  • my_scaffold_generator/
    • lib/
    • my_scaffold_generator.rb
    • required_file.rb
    • templates/
    • ...

Gems that contains multiple generators should be structured like this:

  • my_cool_generators/
    • rails_generators/
    • cool_scaffold/
    • cool_scaffold_generator.rb
    • lib/
      • required_file.rb
      • ...
    • templates/
      • ...
    • cool_controller/
    • cool_controller_generator.rb
    • lib/
      • required_files.rb
      • ...
    • templates/
      • ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜