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:
/config/application.rb
12345
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:
/config.ru
1234
# 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:
/Rakefile
1234567
#!/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.
/config/environments.rb
12
# 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:
/config/environments/*.rb
1234
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:
/config/initializers/secret_token.rb
123456
# 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`
/config/initializers/session_store.rb
123456789
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:
/config/routes.rb
123
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
/config/database.yml
1234567891011121314151617181920212223
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.
Done!
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.