在 Rails 生成器中强制使用 template() 覆盖文件?

发布于 2024-12-07 18:25:04 字数 854 浏览 0 评论 0原文

我自己有一个生成器,作为一组其他操作的一部分,需要设置一个包含一堆 mixins、默认值、注释等的模型类。

我想使用相同的rails g 模型.. . 代码(我从我的生成器调用 invoke),但问题是存在冲突,因为我的模板和模型生成器的模板试图互相冲突:


$ be rails g entry_form karaoke events full_name:string group_name:string
      create  app/controllers/karaokes_controller.rb
      create  app/views/karaokes/show.html.erb
      create  app/views/karaokes/thanks.html.erb
       route  resource :karaoke
      create  app/models/karaoke_entry.rb
      invoke  active_record
      create    db/migrate/20111004004008_create_karaoke_entries.rb
    conflict    app/models/karaoke_entry.rb
  Overwrite app/models/karaoke_entry.rb?
  (enter "h" for help) [Ynaqdh]

有什么建议如何解决这个问题吗?

(我想出的最好办法就是将我的模型文件创建移到底部,并找到某种方法来强制 template / copy_file 继续并覆盖文件而不打扰用户,但我看不到任何预先存在的方法。)

I have myself a generator that, as part of a set of other operations, needs to set up a model class with a bunch of mixins, defaults, comments, etc.

I want to be using the same rails g model ... code (I'm calling invoke from my generator), but the problem is that there's a conflict because my template and the model generator's template are trying to splat each other:


$ be rails g entry_form karaoke events full_name:string group_name:string
      create  app/controllers/karaokes_controller.rb
      create  app/views/karaokes/show.html.erb
      create  app/views/karaokes/thanks.html.erb
       route  resource :karaoke
      create  app/models/karaoke_entry.rb
      invoke  active_record
      create    db/migrate/20111004004008_create_karaoke_entries.rb
    conflict    app/models/karaoke_entry.rb
  Overwrite app/models/karaoke_entry.rb?
  (enter "h" for help) [Ynaqdh]

Any recommendations how to get around this?

(The best I've come up with is to maybe move my model file creation to the bottom, and find some way to force template / copy_file to go ahead and overwrite the file without bothering the user, but I can't see any pre-existing way of doing this.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-12-14 18:25:04

force: trueskip: true 附加到模板调用中:

template "model.rb", "app/models/#{model}.rb", force: true

Append force: true or skip: true to the template call:

template "model.rb", "app/models/#{model}.rb", force: true
浮世清欢 2024-12-14 18:25:04

一些小组集思广益找到了解决这个问题的方法。

您无法覆盖(据我所知),但您可以告诉模型生成器跳过已经存在的文件。这有效:(


# Create the model definition from a template:
template "model.rb", "app/models/#{model}.rb"

# ... Later, get Rails to create everything else:
Rails::Generators.invoke("model", ["Example", "title:string", "--skip"])

我仍然欢迎一种允许 template 覆盖文件的方法。)

Some group brainstorming turned up a way to work around this problem.

You can't override (in so far as I can tell), but you can tell the model generator to skip files that already exist. This works:


# Create the model definition from a template:
template "model.rb", "app/models/#{model}.rb"

# ... Later, get Rails to create everything else:
Rails::Generators.invoke("model", ["Example", "title:string", "--skip"])

(I would still welcome a way to allow template to override files.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文