Redmine Refactor #142: Convert WikiController#destroy to use HTTP DELETE

The code in WikiController is still a mess but I need to keep moving on in order to get it converted to a REST resource. Today I decided to convert the #destroy action to use HTTP DELETE.

Before

1
2
class WikiController  :post, :only => [:destroy, :protect], :redirect_to => { :action => :show }
end
1
2
3
4
5
6
7
8
9
# config/routes.rb
  map.with_options :controller => 'wiki' do |wiki_routes|
    # ...
    wiki_routes.connect 'projects/:project_id/wiki/:page/:action', 
      :action => /rename|destroy|preview|protect|add_attachment/,
      :conditions => {:method => :post}
 
    wiki_routes.connect 'projects/:project_id/wiki/:page/edit', :action => 'update', :conditions => {:method => :post}
  end

After

1
2
3
class WikiController  :post, :only => [:protect], :redirect_to => { :action => :show }
  verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
end
1
2
3
4
5
6
7
8
9
10
11
# config/routes.rb
  map.with_options :controller => 'wiki' do |wiki_routes|
    # ...
    wiki_routes.connect 'projects/:project_id/wiki/:page/:action', 
      :action => /rename|preview|protect|add_attachment/,
      :conditions => {:method => :post}
 
    wiki_routes.connect 'projects/:project_id/wiki/:page/edit', :action => 'update', :conditions => {:method => :post}
 
    wiki_routes.connect 'projects/:project_id/wiki/:page', :action => 'destroy', :conditions => {:method => :delete}
  end

There were very little changes in the controller code for this refactoring, only the verify needed to be updated. The bulk of the changes are in the routing. Before the refactoring, this route would be used to delete a wiki page:

POST /project/a_project_name/wiki/a_wiki_page/destroy

Now I’ve removed the action name and switched to using HTTP DELETE:

DELETE /project/a_project_name/wiki/a_wiki_page

Next I need to decide if I want to try to remove, consolidate, and refactor the rest of the methods in WikiController or if I want to convert WikiController to a resource and then refactor those methods later. Both options will be very painful…

Reference commit