Redmine Refactor #111: Convert ProjectEnumerations to resource

Now that ProjectsController was refactored to a resource, I can start to refactor the other routes to be nested resources. Today I started with ProjectEnumerations since I refactored it to a RESTful design back in refactor #105 and refactor #106.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# config/routes.rb
ActionController::Routing::Routes.draw do |map|
  map.resources :projects, :member => {
    :copy => [:get, :post],
    :settings => :get,
    :modules => :post,
    :archive => :post,
    :unarchive => :post
  }
 
  # Destroy uses a get request to prompt the user before the actual DELETE request
  map.project_destroy_confirm 'projects/:id/destroy', :controller => 'projects', :action => 'destroy', :conditions => {:method => :get}
 
  # TODO: port to be part of the resources route(s)
  map.with_options :controller => 'projects' do |project_mapper|
    project_mapper.with_options :conditions => {:method => :get} do |project_views|
      project_views.connect 'projects/:id/files', :controller => 'files', :action => 'index'
      project_views.connect 'projects/:id/files/new', :controller => 'files', :action => 'new'
      project_views.connect 'projects/:id/settings/:tab', :controller => 'projects', :action => 'settings'
      project_views.connect 'projects/:project_id/issues/:copy_from/copy', :controller => 'issues', :action => 'new'
    end
 
    project_mapper.with_options :conditions => {:method => :post} do |project_actions|
      project_actions.connect 'projects/:id/files/new', :controller => 'files', :action => 'new'
      project_actions.connect 'projects/:id/activities/save', :controller => 'project_enumerations', :action => 'save'
    end
 
    project_mapper.with_options :conditions => {:method => :delete} do |project_actions|
      project_actions.conditions 'projects/:id/reset_activities', :controller => 'project_enumerations', :action => 'destroy'
    end
 
  end
end
1
2
3
class ProjectEnumerationsController  'projects', :action => 'settings', :tab => 'activities', :id => @project
  end
end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# config/routes.rb
ActionController::Routing::Routes.draw do |map|
  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]
  end
 
  # Destroy uses a get request to prompt the user before the actual DELETE request
  map.project_destroy_confirm 'projects/:id/destroy', :controller => 'projects', :action => 'destroy', :conditions => {:method => :get}
 
  # TODO: port to be part of the resources route(s)
  map.with_options :controller => 'projects' do |project_mapper|
    project_mapper.with_options :conditions => {:method => :get} do |project_views|
      project_views.connect 'projects/:id/files', :controller => 'files', :action => 'index'
      project_views.connect 'projects/:id/files/new', :controller => 'files', :action => 'new'
      project_views.connect 'projects/:id/settings/:tab', :controller => 'projects', :action => 'settings'
      project_views.connect 'projects/:project_id/issues/:copy_from/copy', :controller => 'issues', :action => 'new'
    end
 
    project_mapper.with_options :conditions => {:method => :post} do |project_actions|
      project_actions.connect 'projects/:id/files/new', :controller => 'files', :action => 'new'
    end
  end
end
1
2
3
class ProjectEnumerationsController  'projects', :action => 'settings', :tab => 'activities', :id => @project
  end
end

This refactoring caused a few changes.

First, it nested ProjectEnumerationsController under the projects resource as a singular resource. Since the project enumerations are edited in bulk, a singular resource works best here.

Second in order to work with the singular resource, #save was renamed to #update and request.post? was changed to request.put?.

Reference commit