2

I am trying to customise rails default scaffold generators. For views I can do that by simply adding files under : lib/templates/erb/scaffold/

Here I have added index.html.erb and customized, but I want to change model that is generated by this command:

rails g scaffold model 

I have tried adding files to lib/templates/rails/model/model_generator.rb

with codes like this :

 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

But it is doing nothing I need help in this regard what file I need to override and where do I need to place.

AnkitG
  • 6,438
  • 7
  • 44
  • 72
Nilay Singh
  • 2,201
  • 6
  • 31
  • 61
  • What do u want to change within the model generated ? – AnkitG Oct 17 '17 at 06:49
  • need to add some values I want to put some custom validation inside model during rails g scaffold model test:string I need to put this test thing inside model with my codes simple I need to find a way to edit model and controller if possible – Nilay Singh Oct 17 '17 at 06:51
  • how about manually creating a model file instead ? – sa77 Oct 17 '17 at 07:11
  • I did not get your point I need to automate just want to feed everything in scaffold and and it should do the magic I have many modules same thing so this will help me increase development speed – Nilay Singh Oct 17 '17 at 07:14

2 Answers2

6

Here is the Activerecord template. You need to put it in lib/templates/active_record/model/model.rb as

 ~/D/p/p/generator_test> tree lib/
lib/
├── assets
├── tasks
└── templates #<========
    └── active_record
        └── model
            └── model.rb

Here is my custom template

<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>

   #custom method start
   before_save :my_custom_method

   # my method
   def my_custom_method

   end
   #custom method end

<% attributes.select(&:reference?).each do |attribute| -%>
  belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
<% end -%>
<% attributes.select(&:token?).each do |attribute| -%>
  has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
<% end -%>
<% if attributes.any?(&:password_digest?) -%>
  has_secure_password
<% end -%>
end
<% end -%>

Running scaffold

rails g scaffold property

File created

class Property < ApplicationRecord

   before_save :my_custom_method

   # my method
   def my_custom_method

   end

end
KAC
  • 13
  • 1
  • 5
AnkitG
  • 6,438
  • 7
  • 44
  • 72
3

To simplify, you can copy all the ActiveRecord model templates to your current Rails project with this command:

mkdir -p lib/templates/active_record/model && \
cp $(bundle info activerecord --path)/lib/rails/generators/active_record/model/templates/* lib/templates/active_record/model
Tim Krins
  • 3,134
  • 1
  • 23
  • 25
  • Really will that work suppose if I have to change a generated route and I want to add something to resource – Nilay Singh Jun 07 '18 at 04:20
  • The code for scaffolding routes will be in the Railties gem - under `lib/rails/generators/rails/resource_route/resource_route_generator.rb`. You could copy it to `lib/templates/resource_route/` and customise it. – Tim Krins Jun 07 '18 at 08:20
  • If its not updating, make sure to restart `spring` or use `DISABLE_SPRING=true rails g model ...` – Tim Krins Jul 17 '19 at 12:04