REST urls for user memberships.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8146 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-12-10 08:37:48 +00:00
parent 532a76f78c
commit 3dcd71a082
5 changed files with 33 additions and 26 deletions

View File

@ -186,9 +186,10 @@ class UsersController < ApplicationController
end
end
verify :method => [:post, :put], :only => :edit_membership, :render => {:nothing => true, :status => :method_not_allowed }
def edit_membership
@membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
@membership.save if request.post?
@membership.save
respond_to do |format|
if @membership.valid?
format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
@ -208,9 +209,10 @@ class UsersController < ApplicationController
end
end
verify :method => :delete, :only => :destroy_membership, :render => {:nothing => true, :status => :method_not_allowed }
def destroy_membership
@membership = Member.find(params[:membership_id])
if request.post? && @membership.deletable?
if @membership.deletable?
@membership.destroy
end
respond_to do |format|

View File

@ -19,7 +19,7 @@
</td>
<td class="roles">
<span id="member-<%= membership.id %>-roles"><%=h membership.roles.sort.collect(&:to_s).join(', ') %></span>
<% remote_form_for(:membership, :url => { :action => 'edit_membership', :id => @user, :membership_id => membership },
<% remote_form_for(:membership, :url => user_membership_path(@user, membership), :method => :put,
:html => { :id => "member-#{membership.id}-roles-form", :style => 'display:none;'}) do %>
<p><% roles.each do |role| %>
<label><%= check_box_tag 'membership[role_ids][]', role.id, membership.roles.include?(role),
@ -32,8 +32,7 @@
</td>
<td class="buttons">
<%= link_to_function l(:button_edit), "$('member-#{membership.id}-roles').hide(); $('member-#{membership.id}-roles-form').show(); return false;", :class => 'icon icon-edit' %>
<%= link_to_remote(l(:button_delete), { :url => { :controller => 'users', :action => 'destroy_membership', :id => @user, :membership_id => membership },
:method => :post },
<%= link_to_remote(l(:button_delete), { :url => user_membership_path(@user, membership), :method => :delete },
:class => 'icon icon-del') if membership.deletable? %>
</td>
<%= call_hook(:view_users_memberships_table_row, :user => @user, :membership => membership, :roles => roles, :projects => projects )%>
@ -49,7 +48,7 @@
<div class="splitcontentright">
<% if projects.any? %>
<fieldset><legend><%=l(:label_project_new)%></legend>
<% remote_form_for(:membership, :url => { :action => 'edit_membership', :id => @user }) do %>
<% remote_form_for(:membership, :url => user_memberships_path(@user)) do %>
<%= select_tag 'membership[project_id]', options_for_membership_project_select(@user, projects) %>
<p><%= l(:label_role_plural) %>:
<% roles.each do |role| %>

View File

@ -72,21 +72,13 @@ ActionController::Routing::Routes.draw do |map|
map.connect 'projects/:id/members/new', :controller => 'members', :action => 'new'
map.resources :users
map.with_options :controller => 'users' do |users|
users.connect 'users/:id/edit/:tab', :action => 'edit', :tab => nil, :conditions => {:method => :get}
users.with_options :conditions => {:method => :post} do |user_actions|
user_actions.connect 'users/:id/memberships', :action => 'edit_membership'
user_actions.connect 'users/:id/memberships/:membership_id', :action => 'edit_membership'
user_actions.connect 'users/:id/memberships/:membership_id/destroy', :action => 'destroy_membership'
end
users.user_memberships 'users/:id/memberships', :action => 'edit_membership', :conditions => {:method => :post}
users.user_membership 'users/:id/memberships/:membership_id', :action => 'edit_membership', :conditions => {:method => :put}
users.connect 'users/:id/memberships/:membership_id', :action => 'destroy_membership', :conditions => {:method => :delete}
end
map.resources :users, :member => {
:edit_membership => :post,
:destroy_membership => :post
}
# For nice "roadmap" in the url for the index action
map.connect 'projects/:project_id/roadmap', :controller => 'versions', :action => 'index'

View File

@ -305,15 +305,29 @@ class UsersControllerTest < ActionController::TestCase
assert_response 403
end
def test_edit_membership
post :edit_membership, :id => 2, :membership_id => 1,
:membership => { :role_ids => [2]}
def test_create_membership
assert_difference 'Member.count' do
post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]}
end
assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships'
member = Member.first(:order => 'id DESC')
assert_equal User.find(7), member.principal
assert_equal [2], member.role_ids
assert_equal 3, member.project_id
end
def test_update_membership
assert_no_difference 'Member.count' do
put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]}
end
assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
assert_equal [2], Member.find(1).role_ids
end
def test_destroy_membership
post :destroy_membership, :id => 2, :membership_id => 1
assert_difference 'Member.count', -1 do
delete :destroy_membership, :id => 2, :membership_id => 1
end
assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
assert_nil Member.find_by_id(1)
end

View File

@ -364,19 +364,19 @@ class RoutingTest < ActionController::IntegrationTest
should_route :get, "/users/current.xml", :controller => 'users', :action => 'show', :id => 'current', :format => 'xml'
should_route :get, "/users/new", :controller => 'users', :action => 'new'
should_route :get, "/users/444/edit", :controller => 'users', :action => 'edit', :id => '444'
should_route :get, "/users/222/edit/membership", :controller => 'users', :action => 'edit', :id => '222', :tab => 'membership'
should_route :post, "/users", :controller => 'users', :action => 'create'
should_route :post, "/users.xml", :controller => 'users', :action => 'create', :format => 'xml'
should_route :post, "/users/123/memberships", :controller => 'users', :action => 'edit_membership', :id => '123'
should_route :post, "/users/123/memberships/55", :controller => 'users', :action => 'edit_membership', :id => '123', :membership_id => '55'
should_route :post, "/users/567/memberships/12/destroy", :controller => 'users', :action => 'destroy_membership', :id => '567', :membership_id => '12'
should_route :put, "/users/444", :controller => 'users', :action => 'update', :id => '444'
should_route :put, "/users/444.xml", :controller => 'users', :action => 'update', :id => '444', :format => 'xml'
should_route :delete, "/users/44", :controller => 'users', :action => 'destroy', :id => '44'
should_route :delete, "/users/44.xml", :controller => 'users', :action => 'destroy', :id => '44', :format => 'xml'
should_route :post, "/users/123/memberships", :controller => 'users', :action => 'edit_membership', :id => '123'
should_route :put, "/users/123/memberships/55", :controller => 'users', :action => 'edit_membership', :id => '123', :membership_id => '55'
should_route :delete, "/users/123/memberships/55", :controller => 'users', :action => 'destroy_membership', :id => '123', :membership_id => '55'
end
context "versions" do