Redmine Refactor #108: Rename method ProjectsController#add to #new

During yesterday’s refactoring I noticed ProjectsController was using a method called #add instead of #new. By convention Rails (and RESTful Rails) uses the #new method to render the form to create a new record, so I used rename method today to fix that.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class ProjectsController  [ :index, :list, :add, :create, :copy ]
  before_filter :authorize, :except => [ :index, :list, :add, :create, :copy, :archive, :unarchive, :destroy]
  before_filter :authorize_global, :only => [:add, :create]
  before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
  accept_key_auth :index
 
  # Add a new project
  def add
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
    @trackers = Tracker.all
    @project = Project.new(params[:project])
 
    @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
    @project.trackers = Tracker.all
    @project.is_public = Setting.default_projects_public?
    @project.enabled_module_names = Setting.default_projects_modules
  end
 
end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ProjectsController  [ :index, :list, :new, :create, :copy ]
  before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
  before_filter :authorize_global, :only => [:new, :create]
  before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
  accept_key_auth :index
 
  def new
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
    @trackers = Tracker.all
    @project = Project.new(params[:project])
 
    @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
    @project.trackers = Tracker.all
    @project.is_public = Setting.default_projects_public?
    @project.enabled_module_names = Setting.default_projects_modules
  end
 
end

This was a simple refactoring but it makes ProjectsController much more constant with Rails standards. Now if a Rails developer who is new to Redmine looks at ProjectsController, they can easily understand what the #new action is used for. With #add you would have to read the entire method body to see that it’s just setting up a form.

Reference commit