Renaming an ActiveRecord model
Refactoring is a practice that ThoughtWorks developers apply in their work everyday. One of the most common (and arguably, most useful) refactoring is "Rename": basically renaming a variable, a method, a class or any such code artifact to express intent more clearly, according to its evolving responsibility.
In most cases, renaming is pretty trivial, especially with the use of modern IDEs. However, renaming an ActiveRecord model in a Ruby on Rails application can turn into an extremely non-trivial task, mostly because of "convention over configuration" (not that I'm saying anything against it). Here are the things you may need to rename along with your ActiveRecord model:
- table names
- values in the type column of tables that implement Single Table Inheritence (STI) or polymorphic associations
- controller names
- helper module names
- named routes/resource declarations and usages
- instance variables in controllers
- field names in forms
- fixture filenames
Taking an example, let's rename a model that was called User to Subscriber:
- the class needs to be renamed to Subscriber and so does the filename for the same
- find usages of the older class name and change them to the new one: eg. User.find(:first) will be Subscriber.find(:first)
- a migration has to be added to rename the table (overriding the table name in the model probably should be avoided)
- if the original model class was part of STI, its corresponding type values in the STI table will need to be changed from "User" to "Subscriber" via a migration; ditto for tables that represent any polymorphic associations that the class is involved in
- if there was a UsersController, it should be renamed to SubscribersController (don't forget to rename that file too as well as the corresponding folder name under views)
- if the controller name changes, any helper module for that controller will need a name change too (eg: UsersHelper will become SubscribersHelper)
- if the controller name changes, any routes for it need to be modified as well as renamed (eg: edit_user should become edit_subscriber, and the :controller options value will become 'subscribers' instead of 'users')
- if the routes change, search through the entire project to find usages of the corresponding named routes (suffixed with either "_path" or "_url"): these could be in view templates, helpers, or other controllers, and possibly in custom scripts
- the controller probably had instance variables for holding the model or a collection of the same: rename @user to @subscriber and @users to @subscribers
- if the instance variables get renamed, make sure all usages in helpers and templates are also renamed
- in addition, form helpers typically refer to the model name via symbols, make sure those are renamed too: eg. form_for :user will become form_for :subscriber; ditto for fields_for
- if you use fixtures to load data in your test code, you'll need to rename the fixture filename from users.yml to subscribers.yml, and update the fixture declaration in the tests
There may be more places that aren't coming to my mind yet. But you get the gist.