Redmine Refactor #116: Convert VersionsController to resource

Now that I’ve split the actions in VersionsController, it’s ready to be refactoring to a full RESTful resource.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# config/routes.rb
  map.resources :projects, :member => {
    :copy => [:get, :post],
    :settings => :get,
    :modules => :post,
    :archive => :post,
    :unarchive => :post
  } do |project|
    project.resource :project_enumerations, :as => 'enumerations', :only => [:update, :destroy]
    project.resources :files, :only => [:index, :new, :create]
  end
 
  map.with_options :controller => 'versions' do |versions|
    versions.connect 'projects/:project_id/versions/new', :action => 'new'
    versions.connect 'projects/:project_id/roadmap', :action => 'index'
    versions.connect 'versions/:action/:id', :conditions => {:method => :get}
 
    versions.with_options :conditions => {:method => :post} do |version_actions|
      version_actions.connect 'projects/:project_id/versions', :action => 'create'
      version_actions.connect 'versions/update/:id', :action => 'update'
      version_actions.connect 'projects/:project_id/versions/close_completed', :action => 'close_completed'
    end
  end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# config/routes.rb
  # For nice "roadmap" in the url for the index action
  map.connect 'projects/:project_id/roadmap', :controller => 'versions', :action => 'index'
 
  map.resources :projects, :member => {
    :copy => [:get, :post],
    :settings => :get,
    :modules => :post,
    :archive => :post,
    :unarchive => :post
  } do |project|
    project.resource :project_enumerations, :as => 'enumerations', :only => [:update, :destroy]
    project.resources :files, :only => [:index, :new, :create]
    project.resources :versions, :collection => {:close_completed => :put}
  end

Since I’ve already refactored VersionsController to match the REST style this week, doing the final conversion to REST was easy. One non-standard change I did was to define a roadmap route for the #index action. This will keep the url backwards compatible and consistent with the other urls in Redmine.

Reference commit