开发者

Forcing override of file with template() in Rails generator?

开发者 https://www.devze.com 2023-04-11 01:26 出处:网络
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 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/mode开发者_开发问答ls/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.)


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

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


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.)

0

精彩评论

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