I’ve had to change Rails 3 project names a few times now. It’s too bad there’s no rake command for it. Here’s the next best thing: a step-by-step guide.
For the sake of this example, let’s say I’m changing my name name from “Teach” to “Learn”.
Update your app’s module name
In /config/application.rb, change the name of the module:
moduleLearn# Used to be `module Teach`classApplication<Rails::Application# ...endend
Update references to your app’s module name
Your app’s module name should appear in /config.ru:
# This file is used by Rack-based servers to start the application.require::File.expand_path('../config/environment',__FILE__)runLearn::Application# Used to be `run Teach::Application`
And in /Rakefile:
#!/usr/bin/env rake# Add your own tasks in files placed in lib/tasks ending in .rake,# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.requireFile.expand_path('../config/application',__FILE__)Learn::Application.load_tasks# Used to be `Teach::Application.load_tasks`
Your app’s module name should also appear in a bunch of files under the /config directory. It should appear in /config/environments.rb.
# Initialize the rails applicationLearn::Application.initialize!# Used to be `Teach::Application.initialize!`
It should also appear in all the environment-specific configurations in /config/environments/*.rb (which should be development.rb, production.rb, test.rb, and any other environments you have configured). Change them all:
Learn::Application.configuredo# Used to be `Teach::Application.configure do`# Settings specified here will take precedence over those in config/application.rb# ...end
And in your initializers (under /config/initializers/). At the very least, /config/initializers/secret_token.rb and /config/initializers/session_store.rb should have it:
# Your secret key for verifying the integrity of signed cookies.# If you change this key, all old signed cookies will become invalid!# Make sure the secret is at least 30 characters and all random,# no regular words or you'll be exposed to dictionary attacks.Learn::Application.config.secret_token='garbagestring'# Used to start with `Teach::Application`
Learn::Application.config.session_store:cookie_store,key:'_learn_session'# Use the database for sessions instead of the cookie-based default,# which shouldn't be used to store highly confidential information# (create the session table with "rails generate session_migration")# Learn::Application.config.session_store :active_record_store# For the sake of cleanliness, I changed it from `Teach::Application` in both the actual# line of code and the comment. Also, `:key` used to be '_teach_session'.
And in your /config/routes.rb file:
Learn::Application.routes.drawdo# Used to be Teach::Application.routes.draw# Routes in hereend
Finally, for consistency’s sake, if the names of your databases include your app name, you should update those in /config/database.yml
development:adapter:mysql2encoding:utf8reconnect:truedatabase:learn_development# Used to be teach_developmentusername:rootpassword:test:adapter:mysql2encoding:utf8reconnect:truedatabase:learn_test# Used to be teach_testusername:rootpassword:production:adapter:mysql2encoding:utf8reconnect:truedatabase:learn_production# Used to be teach_productionusername:rootpassword:
Search your app and lib folders
grep through your /app and /lib folders to find any mentions of your project name and change them. This might be a little tedious if you have a name that conflicts with a bunch of Ruby/Rails stuff (like “def” or something stupid like that), but you only have to do it once.
That’s it! Now you have a renamed Rails 3 app.
Note: I did this on Rails 3.2.1. It’s possible some things will be different on other versions.