Rails Application Configurations

I find myself repeating my configuration all the time when creating a new rails application. I decided to give rails templates a try after watching this railscast on app generators. In it Ryan Bates goes over having a railsrc file, application templates, and application builders. One problem I did run into while trying out the strategies from the railscast was the application builder, I’m using rails 4 and it seems like they have gotten rid of this option, application templates still exist.

railsrc

The first thing I did was create a .railsrc file. This is pretty straightforward, when you run rails new my_app it will use the options defined in the .railsrc file.

-d mysql --skip-test-unit > ~/.railsrc

Here I’m stating that I want to use mysql for my database and to skip the generation of the test unit directory.

Application Template

The application template is a way to define your configuration.

To run the template you just have to do:

rails new my_app -m /path/to/app_template.rb

Here is an example of my application template.

# change README to markdown
remove_file 'README.rdoc'
create_file 'README.md', 'TODO'
 
# set up additional application folders
keep_file 'app/services'
keep_file 'app/presenters'
keep_file 'app/forms'
 
# Set up gems
gem 'jquery-ui-rails'
gem 'haml'
gem 'will_paginate'
gem 'has_scope'
gem 'carrierwave'
gem 'cocoon'
gem 'cancan'
gem 'american_date'
 
# test and development gems
gem_group :test, :development do
  gem 'debugger'
  gem 'rspec-rails'
  gem 'capybara'
  gem 'poltergeist'
  gem 'database_cleaner'
  gem 'shoulda'
  gem 'factory_girl_rails'
  gem 'timecop'
end
    
# simple_form configuration
simple_form_bootstrap = false
if install_simple_form  = yes?('Install Simple Form?')
  gem 'simple_form'
  simple_form_bootstrap = yes?('Use bootstrap configuration for simple form?')
end
 
# devise confirguration
devise_model_name = 'User'
if install_devise = yes?('Install Devise?')
  gem 'devise'
  if no?('Create default devise User model?')
    devise_model_name = ask('Devise model name?')
  end
end
 
# run the bundle command
run 'bundle install'
 
# install rspec
generate 'rspec:install'
keep_file 'spec/acceptance'
 
# install simple form
if install_simple_form
  simple_form_command = 'simple_form:install'
  simple_form_command += ' --bootstrap' if simple_form_bootstrap
  generate simple_form_command
end
 
# install devise
if install_devise
  generate 'devise:install'
  generate 'devise', devise_model_name
end
 
# create the ruby version and gemset files
run 'rvm list'
rvm_ruby_version = ask('Ruby Version?')
 
run 'rvm gemset list'
rvm_ruby_gemset = ask('Ruby Gemset?')
 
create_file '.ruby-version', rvm_ruby_version
create_file '.ruby-gemset', rvm_ruby_gemset
 
# git initialization
git :init
append_file '.gitignore', 'config/database.yml'
append_file '.gitignore', 'public/uploads'
run 'cp config/database.yml config/database.yml.example'
git add: '.', commit: '-m "initial commit"'

I’ve created a github repo for my rails configurations which you can find here.