Skip to content

Commit

Permalink
Fix javascript Ajax when adding a user to the team
Browse files Browse the repository at this point in the history
  • Loading branch information
mlainez committed Apr 3, 2011
1 parent 7c9a5a9 commit eb75945
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
8 changes: 6 additions & 2 deletions app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ def add_user
# delete /organizations/:organization_id/teams/:id/users/:user_id
def remove_user
@team = @organization.teams.find(params[:id])
@team.users.delete(@user)
render :json => '', :status => :ok
if @team.users.include?(@user)
@team.users.delete(@user)
render :json => '', :status => :ok
else
render :json => ['user', 'has already been removed'], :status => :precondition_failed
end
end

private
Expand Down
57 changes: 35 additions & 22 deletions public/javascripts/helpers/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ Application.Helpers.Teams = {
},

addUser: function(user){
user.fade({duration: 0.2});
var organizationId = user.readAttribute('data-organization-id');
var teamId = user.readAttribute('data-team-id');
var userId = user.readAttribute('data-user-id');
Expand All @@ -112,20 +111,29 @@ Application.Helpers.Teams = {
// Add the picture
var avatar = new Element('img', { src : userAvatar});
var avatarContainer = new Element('div', {'class' : 'avatar'});
avatarContainer.insert({bottom: avatar})
userContainer.insert({bottom: avatarContainer});

// Add the nametag
var nametagContainer = new Element('div', {'class' : 'name'}).update(userNametag);
userContainer.insert({bottom: nametagContainer});

// Add Remove Button
userContainer.insert({bottom: new Element('div', {'class': 'remove', 'title' : 'Remove From Team'})})

userContainer.hide();
$('team_users').insert({bottom: userContainer});
userContainer.appear();
new Ajax.Request('/organizations/'+organizationId+'/teams/'+teamId+'/users/'+userId, {method: 'post'});
new Ajax.Request('/organizations/'+organizationId+'/teams/'+teamId+'/users/'+userId,
{method: 'post',
onSuccess: function(transport){
if(200 == transport.status){
user.fade({duration: 0.2});
avatarContainer.insert({bottom: avatar})
userContainer.insert({bottom: avatarContainer});

// Add the nametag
userContainer.insert({bottom: nametagContainer});

// Add Remove Button
userContainer.insert({bottom: new Element('div', {'class': 'remove', 'title' : 'Remove From Team'})})

userContainer.hide();
$('team_users').insert({bottom: userContainer});
userContainer.appear();
}
}
}
);
},

removeUser: function(user){
Expand All @@ -140,16 +148,21 @@ Application.Helpers.Teams = {

// Add the name
var nameContainer = new Element('div', {'class' : 'name'}).update(userName);
userContainer.insert({bottom: nameContainer});

// Add Add Button
userContainer.insert({bottom: new Element('div', {'class': 'add'}).update('add to team')})

userContainer.hide();
$('organization_users').insert({bottom: userContainer});
userContainer.appear();

new Ajax.Request('/organizations/'+organizationId+'/teams/'+teamId+'/users/'+userId, {method: 'delete'});
new Ajax.Request('/organizations/'+organizationId+'/teams/'+teamId+'/users/'+userId, {
method: 'delete',
onSuccess: function(transport){
if(200 == transport.status){
userContainer.insert({bottom: nameContainer});

// Add Add Button
userContainer.insert({bottom: new Element('div', {'class': 'add'}).update('add to team')})

userContainer.hide();
$('organization_users').insert({bottom: userContainer});
userContainer.appear();
}
}});
},

_setupListeners: function(){
Expand Down
9 changes: 9 additions & 0 deletions test/functional/teams_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ class TeamsControllerTest < ActionController::TestCase
should "remove the user from the team" do
assert !@team.users.include?(@user2)
end
context "and remove it again" do
setup do
delete :remove_user, :id => @team.to_param, :organization_id => @organization.to_param, :user_id => @user2.to_param
end
should_respond_with :precondition_failed
should "give an error" do
assert_match 'has already been removed', @response.body
end
end

end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def should_require_belong_to_project_or_admin_on(actions = [])
context "on #{method.to_s.upcase} to :#{action}" do
setup do
if need_ids.include?(action)
send(method, action, :project_id => @project.to_param, :id => 1)
send(method, action, :project_id => @project.to_param, :id => Project.first.id)
else
send(method, action, :project_id => @project.to_param)
end
Expand Down Expand Up @@ -59,7 +59,7 @@ def should_require_belong_to_project_or_admin_on(actions = [])
context "on #{method.to_s.upcase} to :#{action}" do
setup do
if need_ids.include?(action)
send(method, action, :project_id => @project.to_param, :id => 1)
send(method, action, :project_id => @project.to_param, :id => Project.first.id)
else
send(method, action, :project_id => @project.to_param)
end
Expand Down

0 comments on commit eb75945

Please sign in to comment.