From 6fa82490c970c557f16a048c4d12eae2284dc310 Mon Sep 17 00:00:00 2001
From: ilya <ilya@laptop.(none)>
Date: Wed, 20 Oct 2010 17:34:52 -0700
Subject: [PATCH] remove friend from aspect box, with js call to remove the
 person from the aspect

---
 app/controllers/aspects_controller.rb       | 11 ++++++++++-
 app/models/user.rb                          |  2 +-
 app/views/aspects/manage.html.haml          |  7 ++++++-
 config/routes.rb                            |  1 +
 public/javascripts/aspect-edit.js           | 21 +++++++++++++++++++++
 public/stylesheets/sass/application.sass    |  3 ++-
 spec/controllers/aspects_controller_spec.rb | 10 ++++++++++
 7 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb
index c87220ce25..a683b09367 100644
--- a/app/controllers/aspects_controller.rb
+++ b/app/controllers/aspects_controller.rb
@@ -96,12 +96,21 @@ class AspectsController < ApplicationController
     if current_user.add_person_to_aspect( params[:friend_id], params[:aspect_id])
       flash[:notice] =  I18n.t 'aspects.add_to_aspect.success'
     else 
-      flash[:notice] =  I18n.t 'aspects.add_to_aspect.success'
+      flash[:error] =  I18n.t 'aspects.add_to_aspect.failure'
     end
 
     redirect_to aspects_path(params[:aspect_id])
   end
 
+  def remove_from_aspect
+    if current_user.delete_person_from_aspect( params[:friend_id], params[:aspect_id])
+      flash[:notice] =  I18n.t 'aspects.remove_from_aspect.success'
+    else 
+      flash[:error] =  I18n.t 'aspects.remove_from_aspect.failure'
+    end
+    redirect_to aspects_manage_path
+  end
+
   private
   def clean_hash(params)
     return {
diff --git a/app/models/user.rb b/app/models/user.rb
index 8858c971f6..6f8219765b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -130,7 +130,7 @@ class User
 
   def delete_person_from_aspect(person_id, aspect_id, opts = {})
     raise "Can not delete a person from an aspect you do not own" unless aspect = self.aspects.find_by_id(aspect_id)
-    aspect.person_ids.delete(person_id)
+    aspect.person_ids.delete(person_id.to_id)
     opts[:posts] ||= aspect.posts.all(:person_id => person_id)
     aspect.posts -= opts[:posts]
     aspect.save
diff --git a/app/views/aspects/manage.html.haml b/app/views/aspects/manage.html.haml
index 6cd67166d5..824814f89a 100644
--- a/app/views/aspects/manage.html.haml
+++ b/app/views/aspects/manage.html.haml
@@ -25,8 +25,13 @@
             = person_image_tag(request.person)
             .name
               = request.person.real_name
-  %h3=t('.ignore_remove')
 
+  %h3 Remove from Aspect
+  .aspect_remove
+    %ul.dropzone
+      %li.grey Drag to remove person from aspect
+
+  %h3=t('.ignore_remove')
   .remove
     %ul.dropzone
       %li.grey Drag to ignore/remove
diff --git a/config/routes.rb b/config/routes.rb
index 166e1492d7..7364f90161 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -22,6 +22,7 @@ Diaspora::Application.routes.draw do
 
   match 'aspects/move_friend',  :to => 'aspects#move_friend', :as => 'move_friend'
   match 'aspects/add_to_aspect',:to => 'aspects#add_to_aspect', :as => 'add_to_aspect'
+  match 'aspects/remove_from_aspect',:to => 'aspects#remove_from_aspect', :as => 'remove_from_aspect'
   match 'aspects/manage',       :to => 'aspects#manage'
   match 'aspects/public',       :to => 'aspects#public'
   resources :aspects,           :except => [:edit]
diff --git a/public/javascripts/aspect-edit.js b/public/javascripts/aspect-edit.js
index 77d9907939..dc0d1cd39f 100644
--- a/public/javascripts/aspect-edit.js
+++ b/public/javascripts/aspect-edit.js
@@ -93,6 +93,27 @@ $(function() {
     }
   });
 
+  $(".aspect_remove ul").droppable({
+    hoverClass: 'active',
+    drop: function(event, ui) {
+
+      if (!$(ui.draggable[0]).hasClass('requested_person')){
+        var aspect = ui.draggable[0].getAttribute('from_aspect_id')
+        var person_id =  ui.draggable[0].id
+        $.ajax({
+          type: "POST",
+          url: "/aspects/remove_from_aspect",
+          data:{
+                'friend_id' : person_id,
+                'aspect_id' : aspect
+                }
+        });
+      }
+
+      $(ui.draggable[0]).fadeOut('slow'); // ui.draggable.fadeOut('slow')
+    }
+  });
+
 
   $(".aspect h3").live( 'focus', function() {
 
diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass
index 5249cb1294..87f7c9893f 100644
--- a/public/stylesheets/sass/application.sass
+++ b/public/stylesheets/sass/application.sass
@@ -871,7 +871,8 @@ h1.big_text
 
 .aspect,
 .requests,
-.remove
+.remove,
+.aspect_remove
   :list
     :style none
 
diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb
index 2e50b66798..425d3de4b0 100644
--- a/spec/controllers/aspects_controller_spec.rb
+++ b/spec/controllers/aspects_controller_spec.rb
@@ -80,5 +80,15 @@ describe AspectsController do
       @aspect1.reload
       @aspect1.people.include?(@user2.person).should be true
     end
+  end 
+  
+  describe "#remove_from_aspect" do
+    it 'adds the users to the aspect' do
+      @aspect.reload
+      @aspect.people.include?(@user2.person).should be true
+      post 'remove_from_aspect', {:friend_id => @user2.person.id, :aspect_id => @aspect1.id }
+      @aspect1.reload
+      @aspect1.people.include?(@user2.person).should be false
+    end
   end
 end
-- 
GitLab