Rails templates used for new applications

# for new apps
rails new blog -m

# for existing apps
rails app:template LOCATION=./template.rb

Basic api example

# template.rb

# you can use run instead rails generator and commands
generate(:scaffold, "person name:string")
generate(:migration, 'add_company_to_users company:references')


# add route entry
route "root to: 'people#index'"

# adding a gem
gem 'nokogiri'

after_bundle do
  git :init
  git add: "."
  git commit: %Q{ -m 'Initial commit' }

# config
environment 'config.action_mailer.default_url_options = {host: ""}', env: 'production'

# file, add force: true to overwrite existing content
file 'app/components/foo.rb', <<~RUBY
  class Foo

# run any command like backtick
run "rm README.rdoc"

# adding npm package
run 'yarn add bootstrap jquery popper.js'

# append to a file
run 'echo import \"bootstrap\" >> app/javascript/packs/application.js'

# gsub file
gsub_file 'app/views/layouts/application.html.erb', /.*yield.*/, <<-ERB
    <div class='container'>
      <%= yield %>

# insert into layout template file using after: "Base\n", or before: "  end"
insert_into_file 'app/views/layouts/application.html.erb', <<-ERB, before: '  </head>'
    <%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>

Example of templates