4

If I want to change the default controller template created by scaffold in Rails it's dead easy - after Googling around I found I can just put a controller.rb file in lib/templates/rails/controller for the generator to pick up on instead of the default.

I cannot find any explanation of where I can do the same for a model file. I don't want to build a separate generator, I just want:

> rails generate model foo 

to create foo.rb model file based on a model.rb template I make.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Richard Jordan
  • 8,066
  • 3
  • 39
  • 45

1 Answers1

2

Not sure if this is a great idea but you can find the generator code here: https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/model/model_generator.rb

module Rails
  module Generators
    class ModelGenerator < NamedBase #metagenerator
      argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
      hook_for :orm, :required => true
    end
  end
end

According to this post though you should be able to put a template in lib/templates/rails/model though you may need to specify where it is located via a rake task according to this

ScottJShea
  • 7,041
  • 11
  • 44
  • 67
  • Yeah, I did try that and it didn't seem to pick any changes up that I made to the model_generator.rb file... maybe I made a mistake somewhere, because that was my thought of how to do this too... or maybe I didn't figure out correctly how to make that generator use my template file (still pretty new with generators). I will take a fresh look at this solution though, as you're suggesting it. – Richard Jordan Mar 10 '12 at 00:42
  • Okay... so I took a look at what you reference and the links are to 1) building your own generator and using that, 2) what I mentioned in the original post, which is to place files in the right lib folders for the other stuff. So, neither helps. The question is what can I do for pulling model from a template like I can do with controller and view templates. I see the generator code - I had a replicated copy for changing in the lib/templates/rails/model folder already - I cannot figure out what changes can be made to this to let it pull a template in, or if that's at all possible. – Richard Jordan Mar 10 '12 at 01:15