Skip to content

Commit

Permalink
Refactor: convert VersionsController to a REST resource.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4097 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information
edavis10 committed Sep 17, 2010
1 parent eb1f58f commit bd193a0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def edit
end

def update
if request.post? && params[:version]
if request.put? && params[:version]
attributes = params[:version].dup
attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing'])
if @version.update_attributes(attributes)
Expand All @@ -114,7 +114,7 @@ def update
end

def close_completed
if request.post?
if request.put?
@project.close_completed_versions
end
redirect_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
Expand Down
4 changes: 2 additions & 2 deletions app/views/projects/settings/_versions.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<td class="buttons">
<% if version.project == @project %>
<%= link_to_if_authorized l(:button_edit), {:controller => 'versions', :action => 'edit', :id => version }, :class => 'icon icon-edit' %>
<%= link_to_if_authorized l(:button_delete), {:controller => 'versions', :action => 'destroy', :id => version}, :confirm => l(:text_are_you_sure), :method => :post, :class => 'icon icon-del' %>
<%= link_to_if_authorized l(:button_delete), {:controller => 'versions', :action => 'destroy', :id => version}, :confirm => l(:text_are_you_sure), :method => :delete, :class => 'icon icon-del' %>
<% end %>
</td>
</tr>
Expand All @@ -34,7 +34,7 @@

<div class="contextual">
<% if @project.versions.any? %>
<%= link_to l(:label_close_versions), {:controller => 'versions', :action => 'close_completed', :project_id => @project}, :method => :post %>
<%= link_to l(:label_close_versions), close_completed_project_versions_path(@project), :method => :put %>
<% end %>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/versions/edit.rhtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h2><%=l(:label_version)%></h2>

<% labelled_tabular_form_for :version, @version, :url => { :action => 'update', :id => @version } do |f| %>
<% labelled_tabular_form_for :version, @version, :url => project_version_path(@project, @version), :html => {:method => :put} do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<%= submit_tag l(:button_save) %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/versions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h2><%=l(:label_version_new)%></h2>

<% labelled_tabular_form_for :version, @version, :url => { :action => 'create', :project_id => @project } do |f| %>
<% labelled_tabular_form_for :version, @version, :url => project_versions_path(@project) do |f| %>
<%= render :partial => 'versions/form', :locals => { :f => f } %>
<%= submit_tag l(:button_create) %>
<% end %>
17 changes: 5 additions & 12 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@
end
end

# 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,
Expand All @@ -182,6 +185,7 @@
} 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

# Destroy uses a get request to prompt the user before the actual DELETE request
Expand All @@ -201,19 +205,8 @@
activity.connect 'activity', :id => nil
activity.connect 'activity.:format', :id => nil
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

map.with_options :controller => 'issue_categories' do |categories|
categories.connect 'projects/:project_id/issue_categories/new', :action => 'new'
end
Expand Down
6 changes: 3 additions & 3 deletions test/functional/versions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ def test_get_edit
def test_close_completed
Version.update_all("status = 'open'")
@request.session[:user_id] = 2
post :close_completed, :project_id => 'ecookbook'
put :close_completed, :project_id => 'ecookbook'
assert_redirected_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => 'ecookbook'
assert_not_nil Version.find_by_status('closed')
end

def test_post_update
@request.session[:user_id] = 2
post :update, :id => 2,
put :update, :id => 2,
:version => { :name => 'New version name',
:effective_date => Date.today.strftime("%Y-%m-%d")}
assert_redirected_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => 'ecookbook'
Expand All @@ -126,7 +126,7 @@ def test_post_update

def test_destroy
@request.session[:user_id] = 2
post :destroy, :id => 3
delete :destroy, :id => 3
assert_redirected_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => 'ecookbook'
assert_nil Version.find_by_id(3)
end
Expand Down

0 comments on commit bd193a0

Please sign in to comment.