DEV Community

loading...

Rails quick tips #5: Create apps from templates

citizen428 profile image Michael Kohl Originally published at citizen428.net on ・2 min read

If you often create new Rails apps, application templates can be a real timesaver.

They have a very simple API, which allows developers to make changes to the Gemfile, execute Rails or Git commands, manage files and more.

Here's a simple template I use to generate a new Rails app with Devise already set up:

gem 'devise'

after_bundle do
  rails_command 'generate devise:install'
  rails_command 'generate devise User'
end
Enter fullscreen mode Exit fullscreen mode

The first line adds Devise to the Gemfile and the after_bundle hook runs two generators provided by the gem after the app is done bundling.

To generate an app from this template we need to add the -m option to rails new:

$ rails new my_app -m ~/src/rails-templates/devise.rb
# lots of output
Enter fullscreen mode Exit fullscreen mode

After the app is done, let's verify that Devise was installed and the generators ran correctly:

$ rg devise Gemfile*
Gemfile
56:gem 'devise'

Gemfile.lock
78:    devise (4.7.1)
219:  devise

$ head -n5 config/initializers/devise.rb
# frozen_string_literal: true

# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|

$ cat app/models/user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end
Enter fullscreen mode Exit fullscreen mode

Note that we can also execute a template in the context of an already generated app:

$ rails app:template LOCATION=~/src/rails-templates/devise.rb
Enter fullscreen mode Exit fullscreen mode

Making it interactive

The template API allows for some interactivity via the ask and yes?/no? methods:

if yes?("Add devise?")
  gem 'devise'

  after_bundle do
    rails_command 'generate devise:install'
    rails_command "generate devise #{model}"
  end
end
Enter fullscreen mode Exit fullscreen mode

This will interrupt app generation to ask for user input:

...
       apply  /Users/michi/template.rb
  Add devise? y
     gemfile    devise
         run  bundle install
...
Enter fullscreen mode Exit fullscreen mode

Summary

For anyone creating lots of Rails apps, custom templates can be a real time-saver. I favor an approach of having many smaller for dedicated tasks and adding them an already created project as needed.

Discussion (4)

pic
Editor guide
Collapse
chrisdrit profile image
ChrisDrit

Great stuff @michael !

I too love capitalizing on this so much so that I created a full-blown, ready made, SaaS app via Application Templates found here:

github.com/rails-boilerplate-code/...

Collapse
citizen428 profile image
Michael Kohl Author

You tagged the wrong user 😉 I’m @citizen428 . Nice template though!

Collapse
chrisdrit profile image
ChrisDrit

ha! my bad 😄

Collapse
alec profile image
Alexey Poimtsev (lord/emperor)

hey @citizen428 !
thanks a lot for the interactivity hack, i'll add some options to my template github.com/alec-c4/kickstart :) hope you'll find it useful, but there's no readme :)